Swig and pointers

J

Java and Swing

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 = PyString_AsString(PyList_GetItem($input,i));
else {
PyErr_SetString(PyExc_TypeError,"list must contain strings");
free($2);
return NULL;
}
}
$2 = 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.
 
M

Miki Tebeka

Hello Java,
...
extern int doIt(char *a, MY_DIGIT **digit);
%include cpointer.i
%pointer_functions(MY_DIGIT, md_prt);
Don't you mean md_ptr?
%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 = PyString_AsString(PyList_GetItem($input,i));
else {
PyErr_SetString(PyExc_TypeError,"list must contain strings");
free($2);
return NULL;
}
}
$2 = 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?

Didn't check it but from http://www.swig.org/Doc1.3/Library.html it looks
like you need to do:
ptr = new_md_prt()

HTH.
--
------------------------------------------------------------------------
Miki Tebeka <[email protected]>
http://tebeka.bizhat.com
The only difference between children and adults is the price of the toys

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (Cygwin)

iD8DBQFDRPq78jAdENsUuJsRAiwfAJ43iL7y8B1PNSgzuYVte+40UuDI5wCgq/4K
vSRoo7AzIRZewYt1SYxMuXU=
=gLFG
-----END PGP SIGNATURE-----
 
J

Java and Swing

im sorry, why would it be md_ptr? what is md_ptr?

i tried..
%include cpointer.i
%pointer_functions(MY_DIGIT, digit_ptr)

ptr = new_digit_ptr()
doIt("a message", ptr)
....doesnt work..still needs a DIGIT **
 
J

Java and Swing

(reposting, i was just told how I should properly reply via
groups.google.com)

im sorry, why would it be md_ptr? what is md_ptr?

i tried..
%include cpointer.i
%pointer_functions(MY_DIGIT, digit_ptr)

ptr = new_digit_ptr()
doIt("a message", ptr)
....doesnt work..still needs a DIGIT **


Miki said:
Hello Java,
...
extern int doIt(char *a, MY_DIGIT **digit);
%include cpointer.i
%pointer_functions(MY_DIGIT, md_prt);
Don't you mean md_ptr?
%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 = PyString_AsString(PyList_GetItem($input,i));
else {
PyErr_SetString(PyExc_TypeError,"list must contain strings");
free($2);
return NULL;
}
}
$2 = 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?

Didn't check it but from http://www.swig.org/Doc1.3/Library.html it looks
like you need to do:
ptr = new_md_prt()

HTH.
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top