c++ extension, problem passing argument

Aaron Brady castironpi at gmail.com
Fri Mar 13 13:19:04 EDT 2009


On Mar 13, 12:03 pm, Matteo <tadweles... at gmail.com> wrote:
> Hi all,
>
> I wrote a few c++ classes for some data analysis on a physics
> esperiment. Now I want to glue the lot with Python, so I built the
> necessary wrap code with SWIG and compiled the library with Distutils.
> All is fine, I can import the module and classes work as expected...
>
> ...but for one thing. In the "Fitter" class I have a "findTrack"
> method, which takes as argument (in c++) an array of pointers to
> "WireHit" object (another class). In the python code I filled a list
> with WireHit objects but passing that to the function raises an
> exception:
>
> here is the (relevant) code:
>
> import wiredevents as we
> ...
> hits_array = []
> for i in range(nhit):
>     hit_data = array('l')
>     hit_data.fromfile(input, hit_data_size)
>     hits_array.append(we.WireHit(*hit_data))
> track = we.Fitter.findTrack(hits_array, nhit)
>
> this crashes with:
> Traceback (most recent call last):
>   File "./python_version.py", line 37, in <module>
>     track = we.Fitter.findTrack(hits_array, nhit)
> TypeError: in method 'Fitter_findTrack', argument 1 of type 'WireHit
> **'
>
> how can I pass the right type to the function, without changing the C+
> + code?

Just some ideas: hits_array.tostring( ), hits_array.buffer_info( )
[ 0 ], or PyObject_AsReadBuffer( hits_array, (some_buffer),
(buffer_len) ).



More information about the Python-list mailing list