Boost logo

Boost :

From: David Abrahams (abrahams_at_[hidden])
Date: 2000-10-16 17:40:36


----- Original Message -----
> > Alternatively, If the NTL gives you a C/C++ interface, you can also
write
>
> It does -- NTL is a C++ library for unlimited-precision numerics,
polynomial
> arithmetic, &c.
>
> > your own converter function:
> >
> > some_ntl_type& from_python(PyObject* p, py::Type<some_NTL_type&>)
> > {
> > // an Example implementation. Basically, you need
> > // to extract the NTL type from the PyObject*.
> > if (p->ob_type != NTL_long_type) {
> > PyErr_SetString(PyExc_TypeErr, "NTL long required");
> > throw py::ArgumentError();
> > }
> > return *static_cast<some_NTL_type*>(p);
> > }
> >
> > then the C++ functions you're wrapping can take a some_NTL_type&
parameter
> > directly.
>
> *blink* I must be slow (it's almost midnight here:-) but I don't get it.
>
> How would this help me expose to Python a function, that a Python
> program can call as, say,
> x=MyModule.MyFun(9876229292L)
> and returns an instance of some NTL class I've wrapped...? Thanks
> for any help!

You're not slow; it's just that I didn't understand what you were trying to
do. I thought that NTL was a library which was already wrapped in Python
before you came along. If you've already written the NTL class wrapper (e.g.
ClassWrapper<ZZ>), then you can write your function this way in C++:

  ZZ MyFun(PyObject*);

and expose it this way:

   MyModule.def(MyFun, "MyFun");

Of course, you'll have to go through the regular Python/C API to access the
contents of the Python Long object within the body of MyFun. I haven't
written any slick magic to help out with that.

Also, it looks like you'll be wanting support for the special member
functions for numeric types (e.g. __add__). That hasn't been implemented
yet. If you'd like to tackle the job (not hard, mostly lots of typing), that
would be great. Otherwise, yours will amount to the 2nd request for this
feature, so I'll bump up the priority.

Regards,
Dave


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk