how to pass a file descriptor in a swig module

Pierre Schnizer P.Schnizer at nospam.gsi.de
Fri Jul 4 02:58:25 EDT 2003


Pierre Schnizer <P.Schnizer at nospam.gsi.de> writes:

> "kj.kjn" <kj.kjn at wanadoo.fr> writes:
> 
> > My function func(File * des) is embedded in a module swig.
> > 
> > I would to know how to call this function from python script and if it's
> > necessay
> > 
> > to declare a typemaps ?
> > 
> > Thank you

Sorry I think I misunderstood you. In case you want to pass a file to swig you
need a typemap:
<snip>
/* 
 * Type mapping for grabbing a FILE * from Python
 * Taken from the Swig documentation ... 
 */
%typemap(python,in) FILE * {
  if (!PyFile_Check($input)) {
      PyErr_SetString(PyExc_TypeError, "Need a file!");
      goto fail;
  }
  $1 = PyFile_AsFile($input);
}
</snip>

Please note that this typemap is for swig1.3. In case you are using swig1.1
you should replace $1 with $result and "goto fail" with return NULL (which can
leak memory ).

Pierre




More information about the Python-list mailing list