[C++-sig] RE: passing Boost wrapped classes (refs and/or pointers) to/from Python and C++

David Abrahams david.abrahams at rcn.com
Tue Jun 11 00:28:28 CEST 2002


From: "Mike Dickheiser" <miked at sinistergames.com>


> I'm starting to feel a bit like Alice in Boostland ;)
>
> I entered the world of boost through
> http://www.boost.org/libs/python/doc/index.html, and read through all the
> documentation available on that page (items 1 - 15 in the table of
> contents). Nowehere in that documentation did I see discussion on how to
> call a Python function from C++, and so I assumed that I had to use the
> standard method of:
>
> PyObject* pValue = PyObject_CallObject(pPythonFunc, pArgs);
>
> What I wanted to know was how to pass/pack/put-in/convert a Vector3 into
> pArgs (assuming Vector3 has been wrapped by Boost v1).

Ah, very much clearer, thank you.

> I know how to put
> integers, floats, strings and other basic types into "pArgs", but not
custom
> types like my Vector3. I also needed to know how to get a Vector3 out of
> pValue, assuming that's what the called python function returns.

You don't want to do this, but:

The standard technique is to use from_python(py_obj, type<Vector3&>()) to
get the Vector3 reference /out/ of the PyObject* py_obj, and
to_python(vec3) to copy a Vector3 object vec3 into a new python object.

What you want to do is to use callback<> as described earlier; it takes
care of the to_/from_python stuff for you.

> I looked into the usual Py_BuildValue stuff and tuple-packing, but still
had
> no clue, which led me to seek answers at the SIG.
>
> Now I see in your response a reference to
>
> boost::python::callback<ResultType>::call(arg1, arg2, arg3...);
>
> This is something I haven't seen before at all in any of the documents at
> the previously mentioned address, but looks a lot like what I need.

It is a lot like what you need. However, you mentioned wanting to pass
pointers and references as argumets to Python functions; I assume you meant
without copying the pointee/referent. If you need to do that, you'll need
to use v2 of the library.

> Where can I find the documentation that includes this?

Nowhere :(. But, you can look at the section on overridable virtual
functions (http://www.boost.org/libs/python/doc/overriding.html) which uses
a similar construct

    boost::python::callback<ResultType>::call_method(...)

> I'm assuming I can find v1
> docs at the sourceforge.net address you gave, but if that's incorrect
please
> let me know.

That's incorrect. The sourceforge.net address I gave points you at the v2
documentation under development.
The v1 docs, such as they are, are all at www.boost.org/libs/python/doc
There's lots of undocumented stuff in v1 of Boost.Python; I'm correcting
that for v2.

HTH,
Dave







More information about the Cplusplus-sig mailing list