Swig and pointers

Java and Swing codecraig at gmail.com
Wed Oct 5 15:52:56 EDT 2005


I've tried sending a email to the swig mailing list 3 times...but it
never seems to get it...not sure what is going on, still looking into
it.  Until then...

I now have a C function such as...

int doIt(char *a, MY_DIGIT **digit) {
   ...
}

so I am trying to figure out how to pass in that "digit" pointer.

My SWIG interface file looks like...

extern int doIt(char *a, MY_DIGIT **digit);
%include cpointer.i
%pointer_functions(MY_DIGIT, md_prt);
%typemap(in) (char *a, MY_DIGIT **argv) {
  /* Check if is a list */
  if (PyList_Check($input)) {
    int i;
    $1 = PyList_Size($input);
    $2 = (MY_DIGIT **) malloc(($1+1)*sizeof(long *));
    for (i = 0; i < $1; i++) {
      PyObject *o = PyList_GetItem($input,i);
      if (PyString_Check(o))
	$2[i] = PyString_AsString(PyList_GetItem($input,i));
      else {
	PyErr_SetString(PyExc_TypeError,"list must contain strings");
	free($2);
	return NULL;
      }
    }
    $2[i] = 0;
  } else {
    PyErr_SetString(PyExc_TypeError,"not a list");
    return NULL;
  }
}

%typemap(freearg) (char *a, MY_DIGIT **argv) {
  free((MY_DIGIT *) $2);
}


..from Python I am trying

>> ptr = []
>> doIt("blah", ptr)

I thought this was the correct approach as described here:
http://www.swig.org/Doc1.3/SWIGDocumentation.html#Python_nn59

However, python comes back and says "TypeError: argument number 2: a
'MY_DIGIT **' is expected, 'list([])' is received"

..any ideas?  Thanks for all the help.  The python community has been
great.




More information about the Python-list mailing list