[GRASS-dev] Python SWIG

Glynn Clements glynn at gclements.plus.com
Sun Jun 7 07:33:25 EDT 2009


Hamish wrote:

> > However, some of those wrappers will be unusable because of an
> > inability to pass an argument from Python or return the result to
> > Python.
> 
> is NumPtr of any help here?  (see examples/m.distance.py)
> 
> https://trac.osgeo.org/grass/browser/grass/trunk/swig/python/NumPtr

Not really; we already have a function to convert sequences to arrays
of numeric types. That's currently only used for CELL/FCELL/DCELL, but
typemaps can be added for the others easily enough.

The problem is that if a suitable converter isn't present somewhere in
the compiled code, you're out of luck. There isn't any generic
mechanism to construct values for arbitrary types.

You can construct suitably formatted data with the ctypes module, but
you can't use this with the SWIG wrappers; the data has to be wrapped
in the appropriate SWIG type, and these types can only be added in C.

If there isn't a SWIG type for e.g. char***, there's nothing which you
can do in Python to create a value which will work with a SWIG-wrapped
function whose argument has that type.

So the SWIG interface will require an ongoing process of adding new
wrappers as queries and bug reports arise.

With the ctypes approach, you can construct any value and call any
function without needing binary wrappers, but you may need a dozen
lines of Python for a single C function call (or more if structures
are involved).

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


More information about the grass-dev mailing list