Python class method as an argument of a function in a C extension

M

mauro

Hi all,

I am trying to wrap some C code using SWIG (win32, mingw). I am new to
SWIG and to the Python/C API so what I am doing is looking at the
examples and trying to fit them to my needs, but I cannot find any
complete demo example of a C function taking as an argument a Python
function defined by the user.

What I am trying to do is to pass a class method as an argument of a
function defined in a C extension:

# file: runme.py

import myCExtensionModule

class MyClass:
def __init__(self):
self.myCounter = 0
self.myVar = 0
def myMethod(self, myArg):
self.myCounter += 1
return self.myVar + myArg
def runMe(self):
myCExtensionModule.aCFunction(self.myMethod, 10)

x = MyClass()
x.runMe()

# end of runme.py

Can anybody give me an hint (or some link) on how to define
'aCFunction' and how to call 'self.myMethod' in the C source code?

Thank you very much for any kind of help!
 
M

Matimus

Can anybody give me an hint (or some link) on how to define
'aCFunction' and how to call 'self.myMethod' in the C source code?

A python function defined in C accepts a pointer to self and a tuple
of arguments, each of which is also a PyObject. If you pass a function
or method, it will be included in the tuple of arguments. I don't
think there is any difference between passing a method and a function.
You can call that method using functions in the `Object Protocol'
section of the API, such as PyObject_Call(...).

Here is an example from the Python documentation:
http://docs.python.org/ext/callingPython.html

The first part shows how to get a function as an argument. The
following bits of code show how to actually call that function.The
example uses PyEval_CallObject, which I can't find documentation to.
I'm not sure if it even exists (it may be a documentation error). But
from the example it seems to work exactly the same as
PyObject_CallObject.

Matt
 
M

mauro

A python function defined in C accepts a pointer to self and a tuple
of arguments, each of which is also a PyObject. If you pass a function
or method, it will be included in the tuple of arguments. I don't
think there is any difference between passing a method and a function.
You can call that method using functions in the `Object Protocol'
section of the API, such as PyObject_Call(...).

Here is an example from the Python documentation:http://docs.python.org/ext/callingPython.html

The first part shows how to get a function as an argument. The
following bits of code show how to actually call that function.The
example uses PyEval_CallObject, which I can't find documentation to.
I'm not sure if it even exists (it may be a documentation error). But
from the example it seems to work exactly the same as
PyObject_CallObject.

Matt

Thanks a lot!
At last I succeeded in finding an example:
http://mail.python.org/pipermail/python-list/1999-May/003441.html
This uses exactly the PyObject_CallObject, as you suggest.

Mauro
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top