Calling methods in embedded python

Martin v. Loewis martin at v.loewis.de
Tue Nov 12 11:29:39 EST 2002


"Krzysztof Wasilewski" <removenospam.krzysztof.wasilewski at koti.soon.fi> writes:

> 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()

Notice that these could be combined into a single function call

PyObject_CallMethod(moduleobj, "functionname", "format string", args);

since CallMethod is really the same as moduleobj.functionname(args);

> 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? 

No. Did you try printing the exception? As you know, a return value of
NULL *always* indicates that an exception was raised, which carries
more information about the nature of the problem.

> What should be passed for kw? I tried an empty tuple, an empty list, PyNone
> and other things, but in vain.

It's the dictionary of keyword arguments (**kw), so it should be a
dictionary; NULL would also be acceptable.

> 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.

That should work fine.

Regards,
Martin



More information about the Python-list mailing list