Function References



Greetings,

I'm trying to wrap a function in a C library as a compiled C Python
module. Everything is going great, but I've hit a snag. There's a
function in the form of this:

First the typedef:
typedef void(*FPtr_DeviceMessageHandler) (const DeviceMessage, const
char*);

Then the actual function prototype:
FPtr_DeviceMessageHandler
RegisterDeviceMessageHandler(FPtr_DeviceMessageHandler);

Whenever this USB device I'm using generates a message, it's then sent
to that function whose reference you passed to
RegisterDeviceMessageHandler(). Here's an example:

void testfunc() {
printf("test");
}
....on to main()
RegisterDeviceMessageHandler(&testfunc)

So I've defined a similar function on my C module to do just this,
using the passed function reference to allow the device to send
messages to a Python function of the user's choice. I'm just not sure
how to do this, and the tutorials I've been digging through don't
offer any help. I've found some functions that look promising, but
can't figure out how to cast the function reference from Python into
something the C RegisterDevice... function can handle. Here's what
I've got:

static PyObject *
Py1_RegisterDeviceMessageHandler(PyObject *self, PyObject *args)
{
PyObject *handle, *parsed;

if(!PyArg_ParseTuple(args, "O", handle)) {
return NULL;
}

parsed = PyMethod_Function(handle);
I1_RegisterDeviceMessageHandler(PyMethod_Function(handle));

Py_RETURN_TRUE;
} // end Py1_RegisterDeviceMessageHandler()

This fails since PyMethod_Function returns a PyObject. Is there a way
to cast this to something generic? Casting to (void*) didn't seem to
work.

Thanks in advance!
.



Relevant Pages

  • sandbox python via module loader
    ... I am moving away from python.. ... namespace SnowCrash { ... PyObject *returningModule; ... static void setImportLock (bool lock); ...
    (comp.lang.python)
  • Re: Official definition of call-by-value (Re: Finding the instance reference...)
    ... I cannot say what defines a Python object in a generic way, but I do know what defines a CPython object. ... Most functions in the CPython API receive, return, or handle PyObject pointers everywhere. ... The PyObject struct is very small and contains nothing apart from the ob_type field. ... We refer to this dictionary as the '__dict__' attribute in Python code, but it is just another field in the object struct, as anything else. ...
    (comp.lang.python)
  • Python/C API, Numeric Python, Type Conversion
    ... I am posting here to seek for help on type conversion between Python ... Those PyObject pointers are created with Numeric.Array(data, ... nx: x-dimension of grid data ... float **data; ...
    (comp.lang.python)
  • embedded python doesnt like socket.accept() and SegFaults
    ... I'm practicing with embedding python into C code and i have encountered a very strange problem: I'm unable to call the "accept" method of a server socket without receiving a "Segmentation fault". ... PyObject *new_server ... pValue = PyObject_CallObject(pFunction, ...
    (comp.lang.python)
  • Re: creating simple Python scripting interfaces via C++
    ... stores a pointer to the C++ instance in the Python object. ... PyObject* selfParam; ... and cast it back to the appropriate pointer type. ...
    (comp.lang.python)