accessing method name from within a c-function

Jan Boelsche jan at muskelfisch.com
Thu Jan 17 15:37:54 EST 2002


"Neil Hodgson" <nhodgson at bigpond.net.au> wrote in message news:<nhp18.6520$gf1.33822 at news-server.bigpond.net.au>...

> > The problem arises here:
> > Because the native methods (the c/c++ functions) are registered at
> > run-time, there would only be a single generic c-function that is
> > directly called by the Python interpreter.
> 
>     You might look at calldll which allows calling any function from any DLL
> (almost - there are occsional issues with calling conventions, etc.)
> http://www.nightmare.com/software.html

Thanks for your answer, Neil.  But I'm afraid calldll not really
addresses this problem.
The problem is, that when I use the generic method of registering a
single function for argument conversion and dispatching as described
in the earlier posting, all calls from python to C must pass through
this function.

In order to properly fullfill it's dispatching task, this c function
must know which python expression initially caused its invokation.

The mechanics are something like this:

A python script calls two c functions:

---bla.py---
foo()
bar()
------------
Because both function pointers point to the same c function
"do_dispatch" (which is intended), the Python interpreter calls this
function twice.
do_dispatch now needs to know, if it was called using "foo()" or
"bar()"

---dispatch.c---
static PyObject *do_dispatch(PyObject *self, PyObject *args)
{
    char *command = getInvokedPyFunctionName();
    // command now should be either "foo" or "bar"
    // the following is pseudo-code, of course
    // do dispatching through list of function-pointers
    Variant *return_value = 
        client_functions[command](convertParametersFromPythonToC(self,
args));
    return convertValueFromCToPython(return_value);
}

Question: Is there a way to implement getInvokedPyFunctionName()?
Maybe be inspecting the interpreter's call stack (traceback) or
something?
Or is there a global variable pointing to the function name just
invoked?

jan



More information about the Python-list mailing list