Boost logo

Boost :

From: David Abrahams (abrahams_at_[hidden])
Date: 2000-10-16 16:57:23


----- Original Message -----
From: "Alex Martelli" <aleaxit_at_[hidden]>
To: "David Abrahams" <abrahams_at_[hidden]>
Sent: Monday, October 16, 2000 3:20 PM
Subject: py_cpp tests on 2.0c1

> Hi David,
>
> Microsoft Visual C++ 6 sp4 and standard library is what I'm
> using for my tests. Sorry for not making it clear...!

Thanks.

> One thing I can't currently work out... how do I expose to
> Python a C++ function that takes a Python object that is
> an (unlimited-precision) Python "long" (or one that returns
> it as a result)? I need that to interface NTL, which provides
> its own flavour of unlimited-precision longs (with very nice
> and advanced algorithms for arithmetic on them, primality
> testing, etc).
>
> Thanks for any tip or example...!

It's not documented yet, but you should be able to use a raw PyObject* or a
py::Ptr as one parameter to your C++ function. Then you can manipulate it as
any other generic Python object.

Alternatively, If the NTL gives you a C/C++ interface, you can also write
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.

HTH,
Dave


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