[GRASS-user] Question with Python-SWIG example [SEC=UNCLASSIFIED]
Andrew MacIntyre
Andrew.MacIntyre at acma.gov.au
Tue Apr 20 00:26:12 EDT 2010
[Oops - original inadvertently sent only to Glynn]
> From: Glynn Clements [mailto:glynn at gclements.plus.com]
>
> Andrew MacIntyre wrote:
>
> > > Glynn Clements wrote:
> >
> > {...}
> >
> > > Nor can I figure out how to make use of any pointers returned from
> > > functions. Typemaps exist to allow strings, arrays, etc to be
> > > converted to pointers when passing them into functions, but it
> > > isn't possible to do the reverse (converting to a pointer discards
> > > the size and type; converting from a pointer would require some
> > > way to obtain this information). The functions generated by
> > > cpointer.i and carrays.i don't seem to work as expected (I get a
segfault).
> >
> > Doesn't the ctypes cast() function work for you in making use of
> > returned pointers?
>
> In case it wasn't clear, I was talking about SWIG-wrapped pointers,
> e.g.:
>
> > import grass.lib.raster as rast
> > import grass.lib.grass as gis
> > import ctypes
> > gis.G_gisinit("")
> > p = rast.Rast_allocate_c_buf()
> > p
> <Swig Object of type 'int *' at 0x912e5b8>
> > q = ctypes.cast(p, ctypes.c_void_p)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "/usr/lib/python2.6/ctypes/__init__.py", line 489, in cast
> return _cast(obj, obj, typ)
> ctypes.ArgumentError: argument 1: <type 'exceptions.TypeError'>: wrong
> type
Ahh, sorry for not paying close enough attention.
I've never used SWIG and don't profess to understand exactly what its
returning in the code for _wrap_Rast_allocate_c_buf in raster_wrap.c
(~line 12530).
If the pointer object being made available in Python can be made to
publish the pointer address, then the from_address() method of the
target ctypes type (such as a structure) can be used.
After some googling, it seems that the pointer address should be
obtainable by using int() on the pointer object returned by the SWIG
wrapper (i.e. int(p) in the above example), with the resulting value
usable in ctypes' from_address() method. I might be wrong, but I think
that e.g. ctypes.c_void_p(int(p)) might also work to create a ctypes
void pointer instance.
-------------------------> "These thoughts are mine alone!" <---------
Andrew MacIntyre Operations Branch
tel: +61 2 6219 5356 Communications Infrastructure Division
fax: +61 2 6253 3277 Australian Communications & Media Authority
email: andrew.macintyre at acma.gov.au http://www.acma.gov.au/
If you have received this email in error, please notify the sender immediately and erase all copies of the email and any attachments to it. The information contained in this email and any attachments may be private, confidential and legally privileged or the subject of copyright. If you are not the addressee it may be illegal to review, disclose, use, forward, or distribute this email and/or its contents.
Unless otherwise specified, the information in the email and any attachments is intended as a guide only and should not be relied upon as legal or technical advice or regarded as a substitute for legal or technical advice in individual cases. Opinions contained in this email or any of its attachments do not necessarily reflect the opinions of ACMA.
More information about the grass-user
mailing list