473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Swig and pointers

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

Oct 5 '05 #1
3 3126
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[i] = PyString_AsString(PyList_GetItem($input,i));
else {
PyErr_SetString(PyExc_TypeError,"list must contain strings");
free($2);
return NULL;
}
}
$2[i] = 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/SWIGDocum...ml#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 <mt*****@qualcomm.com>
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-----

Oct 6 '05 #2
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 **

Oct 6 '05 #3
(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 Tebeka wrote:
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[i] = PyString_AsString(PyList_GetItem($input,i));
else {
PyErr_SetString(PyExc_TypeError,"list must contain strings");
free($2);
return NULL;
}
}
$2[i] = 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/SWIGDocum...ml#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 <mt*****@qualcomm.com>
http://tebeka.bizhat.com
The only difference between children and adults is the price of the toys


Oct 6 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jon Moldover | last post by:
Hi, I'm using Python in my win32 app by linking to the python23.dll. I'm trying to expose some c++ code in my app to Python so I can make application calls from Python scripts (according to the...
1
by: Ernie | last post by:
Hi, I am learning swig to use C codes from Python. Here is the swig file mvf.i: %module mvf %include "carrays.i" %array_class(double, doubleArray); %typemap(out) double, float "$result =...
0
by: Bo Peng | last post by:
Dear List, I have had success in using vector.i of SWIG to map between c++: foo(vector<int>) python: foo() However, the map of vector of objects fails: c++: foo(vector<obj> )
3
by: Kevin Dahlhausen | last post by:
Could anyone post a simple setup.py script that uses a SWIG interface to C++ code and the mingw compiler? I followed some online samples, and am using the setup.cfg file to specify swig-c++=1....
1
by: Java and Swing | last post by:
I am trying to wrap some C code I have. Currently I have something like... defs.h ----------- typedef unsigned long MY_DIGIT; myapp.c ------------- void MakeDigits(MY_DIGIT digits) {
3
by: PL | last post by:
I want to pass a 2D array from Python to C++, manipulate it in C++ (for example, add 1 to each element) and pass it back to Python. With these building blocks I will be able to figure out all the...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
5
by: Krish | last post by:
Hello People I hope I am On Topic. Anyways, here is my problem. Any insights would be really appreciated. I have wrapped a C IO module using SWIG -> Python Module. Suppose the name of the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.