Py_BuildValue

Jim jbublitzNO at SPAMnwinternet.com
Thu Dec 12 18:13:49 EST 2002


JB wrote:
> I should like to extend Python with a C++ function.

> Let us suppose I have apointer some C++ structure:

> CObject *p = new CObject(...)

> I should want to pass this pointer to Python and my C++ 
> extension module would provide functions that deal with the 
> object the pointer points to.

> Is calling
> Py_BuildValue("O",p)
> the right way of doing this?

No - because Py_BuildValue expects a PyObject * for
the value corresponding to an "O" flag. There is
no correspondence betwen a PyObject and a C/C++
object, unless you write code (see next paragraph)
to create a correspondence.

There are Py* functions that will return a C/C++
pointer to Python, but that still doesn't give
*Python* access to the C++ object's members in
any useful way. You need wrappers or bindings
to do that (SWIG, sip, boost, etc. or your own
methodology). That means you need to write code
to handle each individual C++ class member (pass
args, call and collect return value(s)), plus some
other code to instantiate/destroy the class as
well.


Jim




More information about the Python-list mailing list