python - Using SWIG with pointer to function in C struct -
I am trying to write a SWIG wrapper for a C library that points to the pointer to work in its strings Uses. I have no idea how to handle the strings containing function pointers, a simple example is below.
test.i:
/ * test.i * /% module exam% {typedef struct {int (* my_func) (int); } Test_struct; Int add1 (int n) {return n + 1; } Test_struct * init_test () {test_struct * t = (test_struct *) malloc (sizeof (test_struct)); T- & gt; My_func = add1; }%} Typingfile {int (* my_func) (int); } Test_struct; Extern test_struct * init_test ();
Sample session:
Python 2.6.2 (Release 26-Ash, 19 April, 01:56:41) [GCC 4.3.3] On Linux2 type "help", "copyright", "credit" or "license" for more information & gt; & Gt; & Gt; Import test & gt; & Gt; & Gt; T = test.init_test () & gt; & Gt; & Gt; T & lt; Test.test_struct; & Lt; The type of stroller object of 'test_struct *' at 0xa1cafd0> & Gt; & Gt; & Gt; & Gt; T.my_func & lt; Swig Object Type 'int (*) (int)' 0xb8009810 & gt; & Gt; & Gt; & Gt; T.my_func (1) Traceback (most recent call final): File "& lt; stdin>", line 1, & lt; Module & gt; TypeError: The 'PySwigObject' object is not worth the corner
Anyone know that 2
Thank you!
I got an answer. If I declare the function pointer as SWIG "Member Function", then it works as expected:
% module exam% {typedef struct {int (* my_func) ( Int); } Test_struct; Int add1 (int n) {return n + 1; } Test_struct * init_test () {test_struct * t = (test_struct *) malloc (sizeof (test_struct)); T- & gt; My_func = add1; Return T; }%} Typed {int my_func (int); } Test_struct; Extern test_struct * init_test ();
Session:
$ Python Python 2.6.2 (Release 26-Maintenance, April 19, 01:56:41) [GCC 4.3.3 ] For more information on Linux2, type "help", "copyright", "credit" or "license" & gt; & Gt; & Gt; Import test & gt; & Gt; & Gt; T = test.init_test () & gt; & Gt; & Gt; T.my_func (1) 2
I was hoping for something that does not need to be written to any custom SWIG-specific code (I'm just " % Included "I like to like my headers), but it seems to me.
Comments
Post a Comment