function pointer

Alex Martelli aleaxit at yahoo.com
Tue Dec 12 05:55:25 EST 2000


"Andrew H. Chatham" <andrew.spambgone.chatham at duke.edu> wrote in message
news:slrn93b4bd.8bv.andrew.spambgone.chatham at fizgig.dorm.duke.edu...
> You could use a CObject, which is what I've done before, though it has
> limitations. A CObject is really just a Python object that holds a C
pointer.
> See http://python.org/doc/current/api/cObjects.html . Of course, doing it
this
> way, you'd have to already know what kind of arguments and return values
the
> function takes and there's also the possibility that someone could pass a
bad
> pointer if they had a CObject from another module, since you cast it to a
"void
> *" to store and retrieve it from the CObject. So if you already know what
kind
> of function it will be a pointer to and don't expect people to go out of
their
> way to break your module, a CObject would be a good choice.

Just a pedantic observation -- there is NO guarantee in the
C standards that a void* can hold a pointer to FUNCTION.  It
IS guaranteed to be able to hold any pointer to DATA, in
such a way that stashing the pointer into a void* and casting
it out again will give the same value back; but, no such
guarantee for pointers to FUNCTIONS.

For example, there used to be, on 16-bit environments for
PC's, compilation modes in which pointers to data would
only use 16 bits, while pointers to functions could use
32 bits.  Here, stashing a function-pointer into a void*
and back again could lead to disastrous truncation.

I guess this is unlikely to bite you in practice, but, if
you rely on that, do please document it clearly, at least!


Alex






More information about the Python-list mailing list