PyCFunction_New() ?
Scott Deerwester
scott at p3international.org
Thu Sep 16 10:24:28 EDT 2004
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...
That's a lot of words, but the intention is:
CObj1 = new SomeClass();
/* CObj1 constructor instantiates a Python SomePyClass object PObj1 */
/* CObj1 calls PObj1.setCallback(CObj1->someMethod) */
CObj2 = new SomeClass();
/* CObj2 constructor instantiates a Python SomePyClass object PObj2 */
/* CObj2 calls PObj2.setCallback(CObj1->someMethod) */
...
# PObj1 decides to call its callback, which calls CObj1->someMethod()
# PObj2 decides to call its callback, which calls CObj2->someMethod()
So the C++ function is (of course) not dynamic, but the Python object
that wraps it is.
> You can call PyCFunction_New, passing it a first argument that's a
> PyMethodDef struct pointer, and a 2nd argument that's a PyObject*
> (whatever you want the C function to receive as the first argument,
> self). PyMethodDef is, of course:
>
> struct PyMethodDef {
> char *ml_name;
> PyCFunction ml_meth;
> int ml_flags;
> char *ml_doc;
> };
> typedef struct PyMethodDef PyMethodDef;
>
> What problems is this giving you...?
I was getting confused between a PyCFunction (which isn't a PyObject,
is it?) and PyCFunction_New... which isn't in the API documentation.
I'll have at it with what you've given me. Thanks!
More information about the Python-list
mailing list