[GRASS-SVN] r37741 - grass/trunk/swig/include/python

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Jun 5 08:47:11 EDT 2009


Author: glynn
Date: 2009-06-05 08:47:11 -0400 (Fri, 05 Jun 2009)
New Revision: 37741

Modified:
   grass/trunk/swig/include/python/my_typemaps.i
Log:
Add typemaps for char** and void**


Modified: grass/trunk/swig/include/python/my_typemaps.i
===================================================================
--- grass/trunk/swig/include/python/my_typemaps.i	2009-06-05 02:32:51 UTC (rev 37740)
+++ grass/trunk/swig/include/python/my_typemaps.i	2009-06-05 12:47:11 UTC (rev 37741)
@@ -143,6 +143,9 @@
 	void *buffer;
 	Py_ssize_t len;
 
+	if (input == Py_None)
+		return NULL;
+
 	if (PyCObject_Check(input))
 		return PyCObject_AsVoidPtr(input);
 
@@ -152,6 +155,9 @@
 	if (PyObject_AsReadBuffer(input, &cbuffer, &len) == 0)
 		return (void *) cbuffer;
 
+	if (PyString_Check(input) && strchr("cbB", data_type))
+		return (void *) PyString_AsString(input);
+
 	return pyseq_to_ptr(input, data_type);
 }
 
@@ -169,4 +175,70 @@
 	$1 = (DCELL *) pyobj_to_ptr($input, 'd');
 }
 
+%{
+
+static void **pyseq_to_ptr_ptr(PyObject *input, int data_type) __attribute__ ((unused));
+static void **pyobj_to_ptr_ptr(PyObject *input, int data_type) __attribute__ ((unused));
+
+static void **pyseq_to_ptr_ptr(PyObject *input, int data_type)
+{
+	Py_ssize_t len;
+	void **array;
+	int i;
+
+	if (!PySequence_Check(input)) {
+		PyErr_SetString(PyExc_ValueError,"Expected a CObject, buffer or sequence");
+		return NULL;
+	}
+
+	len = PySequence_Length(input);
+	array = malloc((len + 1) * sizeof(void *));
+
+	for (i = 0; i < len; i++) {
+		PyObject *val = PySequence_GetItem(input, i);
+		void *p = pyobj_to_ptr(val, data_type);
+
+		Py_DECREF(val);
+		if (val != Py_None && p == NULL) {
+			PyErr_SetString(PyExc_ValueError,"Unable to convert element to pointer");
+			free(array);
+			return NULL;
+		}
+
+		array[i] = p;
+	}
+
+	array[i] = NULL;
+
+	return array;
+}
+
+static void **pyobj_to_ptr_ptr(PyObject *input, int data_type)
+{
+	const void *cbuffer;
+	void *buffer;
+	Py_ssize_t len;
+
+	if (PyCObject_Check(input))
+		return (void **) PyCObject_AsVoidPtr(input);
+
+	if (PyObject_AsWriteBuffer(input, &buffer, &len) == 0)
+		return (void **) buffer;
+
+	if (PyObject_AsReadBuffer(input, &cbuffer, &len) == 0)
+		return (void **) cbuffer;
+
+	return pyseq_to_ptr_ptr(input, data_type);
+}
+
+%}
+
+%typemap(in) void ** {
+	$1 = pyobj_to_ptr_ptr($input, 'c');
+}
+
+%typemap(in) char ** {
+	$1 = (char **) pyobj_to_ptr_ptr($input, 'c');
+}
+
 #endif



More information about the grass-commit mailing list