Calling methods in embedded python

Krzysztof Wasilewski removenospam.krzysztof.wasilewski at koti.soon.fi
Tue Nov 12 12:13:36 EST 2002


Hi,
I have a problem using objects of embedded python from within C++ code.
It works fine when I just want to call a function from a module:
1) I initialize python
2) I import the module I want to use
3) I get the dictionary of that module
4) From the dictionary I get the function I need using
PyDict_GetItemString()
5) I construct the parameter tuple
6) I execute the function using PyObject_CallObject()

And everything goes smoothly.

I tried a similar approach for calling a method of an object. I tried this
way:
1) I initialize python
2) I import the module I want to use
3) I get the dictionary of that module
4) From that dictionary I get the class I intend to utilise, using
PyDict_GetItemString() passing the name of the class; it appears to work up
to this point, since a valid C++ object seems to be created after this call.
5) Then I try to instantiate an object of this class using PyInstance_New(),
passing the class I created. Unfortunately, this always fails - NULL is
returned. Can anyone explain why? PyInstance_New() isn't too well documented
in the manual:
......................
PyObject* PyInstance_New(PyObject *class, PyObject *arg, PyObject *kw)

Return value: New reference.
Create a new instance of a specific class. The parameters arg and kw are
used as the positional and keyword parameters to the object's constructor
...........................

What should be passed for kw? I tried an empty tuple, an empty list, PyNone
and other things, but in vain. The constructor of the classes I want to use
take either no parameters or a single parameter without a key. What should I
do?

6) Once I get an answer to this one, I plan to call a method of the
instantiated object calling PyObject_CallMethod(). I hope that this is OK.

Thanks,
-Krzysztof





More information about the Python-list mailing list