
April 5, 2014
1:12 p.m.
On Mar 22, 2014, at 12:50 PM, sagar masuti <sagar.masuti@gmail.com> wrote:
Is it possible to cast an int to a class type ?
Not safely.
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
3996
Age (days ago)
3996
Last active (days ago)
0 comments
1 participants
participants (1)
-
Philip Semanchuk