passing a C struct to Python and back
Kevin
other at cazabon.com
Tue Jun 17 01:51:16 EDT 2003
In a Python extension module, I have a struct that I need to pass to Python,
then back into the extension module later for use. Python doesn't need to
do anything with the struct in the meantime though.
I'm trying to do it using a CObject through the following, but
PyCObject_AsVoidPtr() returns a void*, not a struct...
Is there an easy way to do this? Creating a new Python type seems like a
big pain in the butt for something as "simple" as this.
Sample code:
// section that creates the struct, called theProcs
return Py_BuildValue("isO", 0, "Library loaded successfully.",
PyCObject_FromVoidPtr(&theProcs, KPDCLoader_Unload));
}
// section that gets the struct back as a CObject... doesn't work of course
static PyObject *
findCameras(PyObject *self, PyObject *args)
{
void *pyTheProcs;
// Structure of SDK funtion entry points
KPDCProcs theProcs;
// Initialize data structures
memset(&theProcs, 0, sizeof( theProcs ) );
if (!PyArg_ParseTuple(args, "O", &pyTheProcs)) {
return Py_BuildValue("iss", -1, "ERROR: could not parse the KPDCProcs
object from the argument tuple.", "");
}
theProcs = (KPDCProcs) PyCObject_AsVoidPtr(pyTheProcs);
// doesn't work, can't cast a void* to a struct
}
More information about the Python-list
mailing list