determining identity of a builtin C function

Hugh Secker-Walker faxacover at hodain.ci.net
Thu May 29 16:27:22 EDT 2003


faxacover at hodain.ci.net (Hugh Secker-Walker) wrote in message news:<3803fd8d.0305282159.20c970b8 at posting.google.com>...
> 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.

Problem solved.  This works:
    if( PyCFunction_Check(obj0)
        && PyCFunction_GetFunction(obj0) == functionPtrForb )
     {
        // we've been called with foo.b
     }

Yes, it helps to have the right PyObject as obj0 ....

-Hugh




More information about the Python-list mailing list