[capi-sig] (no subject)
Philip Semanchuk
philip at semanchuk.com
Sat Apr 5 20:12:00 CEST 2014
On Mar 22, 2014, at 12:50 PM, sagar masuti <sagar.masuti at gmail.com> wrote:
> Is it possible to cast an int to a class type ?
Not safely.
> How can I typecast the address passed from C to a class type in python so
> that i can access those class parameters or indirectly saying accessing the
> structure parameters?
I had to do something similar to support the passing of a C pointer (memory address) for my sysv_ipc module. Here’s how I did it --
PyObject *
SharedMemory_attach(SharedMemory *self, PyObject *args) {
PyObject *py_address = NULL;
void *address = NULL;
int flags = 0;
if (!PyArg_ParseTuple(args, "|Oi", &py_address, &flags))
goto error_return;
if ((!py_address) || (py_address == Py_None))
address = NULL;
else {
if (PyLong_Check(py_address))
address = PyLong_AsVoidPtr(py_address);
else {
PyErr_SetString(PyExc_TypeError, "address must be a long");
goto error_return;
}
}
// You should now be able to cast address as needed.
}
That code is pulled from memory.c in this module:
http://semanchuk.com/philip/sysv_ipc/
I don’t think I invented that technique myself. I think someone on a mailing list (perhaps this one) gave me the idea.
Hope this helps
Philip
More information about the capi-sig
mailing list