Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > PyObject_CallObject: difference between functions and class methods

Reply
Thread Tools

PyObject_CallObject: difference between functions and class methods

 
 
mauro
Guest
Posts: n/a
 
      09-27-2007
Hi all!

I am trying to call within a C extension a Python function provided as
an argument by the user with: PyObject_Call(). The C extension should
work also if the user supplies a class method, but in this case I am
getting an error. Do I need to explicitly pass 'self' as an argument
to PyObject_Call()? If so, how can I do that?

Now, I am using:

if ((tmp_args = PyTuple_New(1)) == NULL)
PyErr_SetString( PyExc_ReferenceError, "attempt to access a null-
pointer" );
PyTuple_SetItem(tmp_args, 0, paramlist);

to create the tuple required by PyObject_Call(), but I have no idea on
how to add a reference to 'self'.

Here is what I would like to obtain:

##
import mymodule

def myfunc(x):
# Do something
return [z]

class MyClass:
def mymethod(self, x):
# Do something
return z
def runme(self):
mymodule.main(myfunc) # This will work
mymodule.main(self.mymethod) # This will not work (Segmentation
fault)

x = MyClass()
x.runme()
##

Thanks in advance.

Mauro

 
Reply With Quote
 
 
 
 
Hrvoje Niksic
Guest
Posts: n/a
 
      09-27-2007
[ Note that there is now a mailing list dedicated to the C API:
http://mail.python.org/mailman/listinfo/capi-sig ]

mauro <(E-Mail Removed)> writes:

> I am trying to call within a C extension a Python function provided as
> an argument by the user with: PyObject_Call(). The C extension should
> work also if the user supplies a class method, but in this case I am
> getting an error. Do I need to explicitly pass 'self' as an argument
> to PyObject_Call()?


You don't. The reference to self will be added automatically when
invoking the function you receive as object.method.

> if ((tmp_args = PyTuple_New(1)) == NULL)
> PyErr_SetString( PyExc_ReferenceError, "attempt to access a null-
> pointer" );
> PyTuple_SetItem(tmp_args, 0, paramlist);


Maybe you are mismanaging the reference count -- PyTuple_SetItem
steals the refcount of its argument. Anyway, why not use
PyObject_CallFunction or PyObject_CallFunctionObjArgs? For example:

PyObject *
mymodule_main(PyObject *ignored, PyObject *func)
{
PyObject *result, *my_param;
/* ... do something, e.g. create my_param ... */

/* call func */
result = PyObject_CallFunction(received_func, "O", my_param);
Py_DECREF(my_param); /* assuming you no longer need it */
if (!result)
return NULL;

/* ... do something with result ... */

Py_DECREF(result);
Py_INCREF(Py_None);
return Py_None; /* or whatever */
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
difference between class and object methods Sagy Drucker Ruby 4 02-03-2012 11:51 AM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
difference between class and static methods? John Salerno Python 3 04-19-2006 06:30 AM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
difference between class methods and instance methods John M. Gabriele Python 18 02-18-2005 05:17 PM



Advertisments