[Tutor] Extending using SWIG: typdef-ed types

cliff@ember.com cliff@ember.com
Fri, 16 Nov 2001 18:47:47 -0500


<color><param>0100,0100,0100</param>I would like to import some existing C functions into Python using 
SWIG.  Many of these have user-defined types in either the 
argument list or as a return value, for example:


/*-------  Sample C header File ----- */


typedef unsigned char int8u;


typedef enum {

NO_ERROR,

ARGUMENT_TOO_LARGE,

} Err;


Err square(int8u* valueP);


/*----- End header File ----- */


When I import the resulting module into Python, I get errors like


>>>square(u)

Traceback (most recent call last):

	File "<<stdin>", line 1 in ?

TypeError: type error.  Expected _int8u_p


I have no idea how to create a type _int8u_p...  :-( As a work-
around, I wrote some glue code that looks like


/*----- Sample C header File ----- */


int8u* createValueP(void);


Err setPValue(int value);


int getPValue(void);


int getErrorValue(Err error);


void freeValueP(int8u* valueP);


/*----- End header File -----*/


So...my questions are: 


1.  Is there some way to use my existing C code without writing all 
of these glue functions  (or reworking it specifically as a Python 
extension)?


2.  If not, do I need to have a function like freeValueP() to free the 
memory allocated in createValueP(), or will Python take care of 
that for me once I eliminate all references to the object created by 
createValueP()?


Many, many thanks!


--Cliff


 




<nofill>