determining identity of a builtin C function

Hugh Secker-Walker faxacover at hodain.ci.net
Thu May 29 01:59:38 EDT 2003


I have a couple of C functions a() and b() in an extension module,
foo, exposed to Python via a PyMethodDef table.  They work, to some
degree.

I'd like a() to return 1 if it's been handed b() as an argument, and
None otherwise.  E.g.
>>> foo.a(foo.b)
1
>>> foo.a(0)
None
>>> foo.a(foo.a)
None
etc.

How do I implement this using the C-Api?  a() can tell when it's been
handed a callable, via PyCallable_Check(), but beyond that I'm
striking out.  I've tried, PyMethod_Check() and PyCFunction_Check()
but they return 0.  I was hoping in the end to be able to use
PyCFunction_GetFunction() and compare its value against the function
pointer for b() from the PyMethodDef table.

I've also considered getting a hold of the module object and looking
up b() and seeing if it's the same PyObject* that a() has been handed,
but that doesn't seem as straightforward as verifying that the
argument to a() is a builtin C function and then getting the function
pointer.... (indeed, in the end, I need that function pointer to use
in the C code).

What steps do I need to take on the argument to a() to determine if
it's a C function and if that C function is the one that implements
b()?  Where is this documented?

BTW, this is all actually happening in the typemap code for a SWIG
wrap of the C functions.

TAI,
-Hugh




More information about the Python-list mailing list