Callback mysteries

F

Fons Adriaensen

Hello all,

I wonder if someone could explain some of the following.

(Python 3.2)

I have a class which has a method called 'callback()'.
An instance of this class calls a C extension which
then calls back into Python.

In all cases below, two arguments are passed to the C
code and end up in

PyObject *object;
PyObject *method;

The original solution was:

P: self -> object, "callback" -> method
C: PyObject_CallMethodObjArgs(object, method, 0);

and this works nicely.

Just out of academic interest, I was wondering if the run-time
lookup of 'callback' could be avoided. My first attempt was:

P: self -> object, classname.callback -> method
C: PyObject_CallFunctionObjArgs(method, object, 0);

and this also works, I'm just not sure it's kosher.

Now in theory 'self.callback' should contain all info
that is required (or not ?). So I also tried:

P: self -> object, self.callback -> method
C: PyObject_CallFunctionObjArgs(method, PyMethod_Self(method), 0)

which fails,

P: self -> object, self.callback -> method
C: PyObject_CallFunctionObjArgs(PyMethod_Function(method), PyMethod_Self(method), 0);

which also fails.

And indeed the value returned by PyMethod_Self() is not equal to
the value of object (= self). In fact it seems not to depend on
the calling instance at all. Do I misunderstand what PyMethod_Self()
is supposed to return ?

I also tried

P: self -> object, self.callback -> method
C: PyObject_CallObject(method, 0);

which also fails.

Any comments that help me understand these things will be appreciated !


Ciao,
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top