Name of current method (for C functions) ?

Michael Hudson mwh at python.net
Wed Oct 24 05:09:48 EDT 2001


tgloth at earthlink.net (Tobias Gloth) writes:

> I am implementing a module for python that registers new methods
> dynamically, using Py_InitModule. At compile-time it is not known 
> how many methods will be registered, so I can't provide a separate
> callback for each method. My plan is to register the same C function
> for all those python methods, and from within that C function
> dispatch, based on the name the function was called under.
> 
> The problem is: I seem to be unable to obtain the name of the
> current method anywhere. For methods implemented in python this
> works, by analyzing the call stack, but for C functions, the relevant
> name field is empty.
> 
> Any hints or suggestions would be greatly appreciated!

I don't think what you ask is directly possible (because of C's
suckiness[0], basically).  I'd suggest a Python wrapper along the
lines of

import my_c_module

class _Func:
    def __init__(self, func, descr):
        self.func = func
        self.descr = descr
    def __call__(self, *args):
        self.func(self.descr, *args)

for name, descr, func in my_c_module.get_funcs():
    globals()[name] = _Func(descr, func)

Could something like this fly?

Cheers,
M.

[0] The fact that you can't dynamically create functions.  Whether
    this is suckiness or not is not opened to debate here.

-- 
6. Symmetry is a complexity-reducing concept (co-routines include
   subroutines); seek it everywhere.
  -- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html



More information about the Python-list mailing list