PyCFunction_New() ?

Alex Martelli aleaxit at yahoo.com
Thu Sep 16 11:32:34 EDT 2004


Scott Deerwester <scott at p3international.org> wrote:

> Alex Martelli wrote:
> 
> > Scott Deerwester <scott at deerwester.org> wrote:
> > 
> >> Is it possible to create a Python-callable object, dynamically, in C/C++?
> >> I
> > 
> > Sure!  But I'm not clear on why you want to create it dynamically.  The
> > C++ code is there all the time, isn't it?  So why not the wrapping of it
> > into Python-callable terms...?
> 
> Because I'd like to have multiple instances of the class that has (or is
> somehow associated with) the C/C++ callback, and to be able to hand a
> corresponding Python object a Python-callable callback that ends up
> invoking the C++ callback for a particular C++ class instance...

Ah, you want to make "bound methods" from C++...?  That's a tall order
indeed.  In C++ itself, in fact, you couldn't.

Fortunately, you can exploit the 2nd argument of PyCFunction_New.  Place
the pointer to the C++ object there, wrapped into a PyObject (or cast
into a PyObject* if you feel frisky...!-).  Your C function unwraps it
or casts it back, and then can call the C++ method on the suitable
instance thus recovered.


Alex



More information about the Python-list mailing list