#1315 numinputs=0 doesn't work with argout of type "pointer to struct" in python

open
nobody
None
5
2014-03-23
2013-04-12
No

When trying to wrap a C function that has an output argument of type "pointer to struct", the (in,numinputs=0) typemap prevents the (argout) typemap from working.

Example:

The C function to access from python is:

ucis_GetScopeSourceInfo(
  ucisT               db,
  ucisScopeT          scope,
  ucisSourceInfoT*    sourceinfo);

The input typemap used to prevent python scripts from having to pass in a dummy 3rd argument is:

%typemap(in, numinputs=0) ucisSourceInfoT *sourceinfo (ucisSourceInfoT *temp) {
  $1 = &temp;
}

The typemap specifying that the 3rd argument is an output, and how to handle its pointer is:

%typemap(argout) ucisSourceInfoT *sourceinfo {
  PyObject *o = SWIG_NewPointerObj($1, $1_descriptor, 0);
  $result = SWIG_Python_AppendOutput($result, o);
}

This should be callable from a python script like this:

(status,sourceinfo) = ucis_GetScopeSourceInfo(cb.db, cb.obj)

This will run, but the fields of sourceinfo contain garbage when printed.

To prove that the (numinputs=0) typemap is the source of the problem, it was removed while keeping the (argout) typemap the same, and the python script was modified as follows:

dummy = malloc_ucisSourceInfoT()
(status,sourceinfo) = ucis_GetScopeSourceInfo(cb.db, cb.obj, dummy)

This works perfectly, except that it requires scripts to add dummy code, and makes things more cumbersome/less readable.

Discussion