|
From: Angus M. <am...@gm...> - 2009-08-26 19:39:57
|
2009/8/26 Josh Cherry <jc...@nc...>: > On Wed, 26 Aug 2009, Angus McMorland wrote: > >> This really seems like a poor-man's solution to this problem, which >> should be solvable without constructing cases for each structure type. > > I wish there were a better way--perhaps there is, and perhaps one could be > added--but I've had success using this approach. > >> The magic underlying the ".this" Python attribute of swig-wrapped >> structures provides a valid void * pointer that SetData can use. The >> only limitation of this approach is that I can't work out how to find >> out the C-size of the structure in Python. Alternatively, using ctypes >> I can get the size, but not the correct pointer to the data. Is there >> a way to get the equivalent of swig's ".this" attribute for a >> ctypes-wrapped structure? This could then be passed to a function like >> SetData, swig-wrapped function, and expecting a "void *" pointer. > > I think so. My recollection is that you can get an integer representation > of a ctypes poiner, in which case this is possible. 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. > You could, for example, > add a function to your interface that takes an integral type and returns it > as a void *. But why is this necessary? If you can get the size from > ctypes, can't you use that along with a SWIG representation of a pointer? Yes, in principle I have a workable solution now, creating, in Python, both a swig-wrapped structure populated with the data which I can pass, and a second ctypes-version of the structure from which I can work out the size. This, however, seems incredibly clunky, and I was thinking there should be a more elegant solution. Creating only one, ctypes-version, structure, and passing a pointer to _its_ data and getting _its_ size, seemed like one such cleaner solution. I'm surprised this issue doesn't come up more often. Thanks for your thoughts. Angus. -- AJC McMorland Post-doctoral research fellow Neurobiology, University of Pittsburgh |