c++ extension, problem passing argument

Matteo tadwelessar at gmail.com
Fri Mar 13 18:52:08 CET 2009


On 13 Mar, 18:19, Aaron Brady <castiro... at gmail.com> wrote:
> 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) ).

Doesn't seem to work.

The relevant lines in wiredevents_wrap.cxx should be:

SWIGINTERN PyObject *_wrap_Fitter_findTrack(PyObject *SWIGUNUSEDPARM
(self), PyObject *args) {
  if (!PyArg_ParseTuple(args,(char
*)"OO:Fitter_findTrack",&obj0,&obj1)) SWIG_fail;
    SWIG_exception_fail(SWIG_ArgError(res1), "in method '"
"Fitter_findTrack" "', argument " "1"" of type '" "WireHit **""'");



More information about the Python-list mailing list