From philip at semanchuk.com Sat Apr 5 20:12:00 2014 From: philip at semanchuk.com (Philip Semanchuk) Date: Sat, 5 Apr 2014 14:12:00 -0400 Subject: [capi-sig] (no subject) In-Reply-To: References: Message-ID: <48EF9A46-7F41-4646-8C1C-5580DBBD5095@semanchuk.com> On Mar 22, 2014, at 12:50 PM, sagar masuti 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