Python C Interface: finding out the called method's name in C PyMethod?

Florian Link florianlink at gmx.de
Sat Feb 28 06:45:51 EST 2004


Purpose:  Dynamically multiplex PyMethodDefs to QT Slots

Hi,

I would like to create a PyObject using the C API which can dispatch calls 
to it's methods
to just ONE C function, which then forwards the call depending on the 
method name and argument types/number
to other classes.

E.g. I would dynamically create:

PyMethodDef methods_QObject[] = {
	{"method1", methodHandler, METH_VARARGS, NULL},
	{"method2", methodHandler, METH_VARARGS, NULL},
	{"method3", methodHandler, METH_VARARGS, NULL},
   ... more methods, read dynamically from a C++ Runtime system...
}


static PyObject *methodHandler(PyObject *sipSelf,PyObject *sipArgs)
{
   // Now my problem arises, all methods point to this dispatcher
   // and I would like to know the called method name something like:

   const char* method = Py_..._getCurrentInvocationName(sipSelf);

   // now do something dependend on method and sipArgs...
   qobject->qt_invoke(method,args)
}

Your will say, hey, why does he want to do that, since I could just 
implement "method1Handler", "method2Handler", ...

BUT, this would mean creating wrapper code like in the SIP library or SWIG 
etc. which needs to be compiled on each change.
I want to do it dynamically, since I have a generic way to forward the 
calls and I don't want to recreate the wrappers just to create
some dummy forwarders. I can create PyMethodDef dynamically, but I cannot 
create C forwarder functions dynamically.

So, it there a way to find out the called method name from a PyMethod C 
function?

Or maybe there is a way deeper in Python, can I extend the method 
dispatcher of a PyObject, so get all call before the lookup
to tp_dict is done?

best regards,
Florian 





More information about the Python-list mailing list