[GRASS-dev] RE: [GRASS-user] Question with Python-SWIG example [SEC=UNCLASSIFIED]

Glynn Clements glynn at gclements.plus.com
Fri May 21 11:31:33 EDT 2010


Martin Landa wrote:

> > Does this work:
> >        ...
> >        c_input = c_char_p(input)
> >        c_mapset = c_char_p("")
> >        mapset = grass.G_find_raster2(c_input, c_mapset)
> >        ...
> 
> No,
> 
> String.from_param(): $Revision: 41297 $
> String.__init__(): $Revision: 41297 $
> String.from_param(): $Revision: 41297 $
> String.__init__(): $Revision: 41297 $
> String.from_param(): /home/martin/smetiste/example7.py
> String.__init__(): /home/martin/smetiste/example7.py
> Traceback (most recent call last):
>   File "/home/martin/smetiste/example7.py", line 10, in <module>
>     mapset = grass.G_find_raster2(c_input, c_mapset)
> ctypes.ArgumentError: argument 1: <type 'exceptions.TypeError'>:
> cannot concatenate 'str' and 'c_char_p' objects

That error is coming from your debugging statements; you're probably
doing something like:

	print "String.from_param(): " + obj

Using:

	print "String.from_param(): %s" % obj
or:
	print "String.from_param():" + str(obj)

should work.

> > Or this:
> >        ...
> >        grass.G_find_raster2.argtypes = [c_char_p, c_char_p]
> >        grass.G_find_raster2.restype = c_char_p
> >        mapset = grass.G_find_raster2(c_input, c_mapset)
> 
> Yes, it's working.
> 
> > The most common issue with ctypes, and the most likely reason why the
> > underlying function would end up getting garbage data, is that the
> > ctypes pointer types don't hold a reference to the underlying object,
> > which can result in it being garbage collected while it is still being
> > used.
> 
> Yes, it's seems to be a ctypes issue.

Well, the "manual" ctypes version worked earlier, so it appears to be
a problem with the wrappers. The preamble's String type looks
unnecessarily complex; there's probably a reason, but I have doubts as
to whether the benefits outweight the drawbacks.

Can you try r42331? This eliminates most of the preamble, making
String and ReturnString aliases for c_char_p. If there's a need for
something more complex, I'll deal with that if and when it happens.

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the grass-dev mailing list