From: Haoyu B. <div...@gm...> - 2009-08-28 07:39:29
|
On Fri, Aug 28, 2009 at 1:45 AM, Angus McMorland<am...@gm...> wrote: > 2009/8/26 Josh Cherry <jc...@nc...>: >> On Wed, 26 Aug 2009, Angus McMorland wrote: >> >>> I'm not sure that we're looking for an integer representation: in >>> Python, SWIG's ".this" attribute is a <Swig Object of type >>> 'MDF_TEST_DATA *' at ...>, which seems like some special thing that >>> swig uses to keep track of its objects. I looked inside the swig >>> wrapper code, to see how ".this" was generated, but it comes from the >>> binary .so generated during the swig build, so I follow that lead any >>> further. >> >> I had in mind, in the crudest form, something like this: >> >> %inline %{ >> void *void_ptr(long long n) { >> return (void *) n; >> } >> %} >> >> Adding a bit of code with %pythoncode could hide this all from your users, >> so they can just pass the ctypes pointer (they wouldn't even need to pass >> the size; the Python code could get that from the ctypes pointer). >> >> Josh > > I just want to say a public "big thanks, Josh". That void_ptr code > snippet was just what I needed. With it, and a dash of %pythoncode to > wrap the copying of data to and from the ctypes structures (as you > suggested), everything works seamlessly, without the need for lots of > boilerplate code for each data type. > > Angus. > -- > AJC McMorland > Post-doctoral research fellow > Neurobiology, University of Pittsburgh > Hi, FYI, do you aware there's SWIG's support for Python buffer protocol (http://www.swig.org/Doc1.3/Python.html#Python_nn75)? This should be able to used for any Python types that support the buffer protocol, eg., string, unicode. But I'm not sure whether it is suitable for your case. -- Haoyu BAI School of Computing, National University of Singapore. |