473,792 Members | 2,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_functi ons(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($in put);
$2 = (MY_DIGIT **) malloc(($1+1)*s izeof(long *));
for (i = 0; i < $1; i++) {
PyObject *o = PyList_GetItem( $input,i);
if (PyString_Check (o))
$2[i] = PyString_AsStri ng(PyList_GetIt em($input,i));
else {
PyErr_SetString (PyExc_TypeErro r,"list must contain strings");
free($2);
return NULL;
}
}
$2[i] = 0;
} else {
PyErr_SetString (PyExc_TypeErro r,"not a list");
return NULL;
}
}

%typemap(freear g) (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 3144
Hello Java,
...
extern int doIt(char *a, MY_DIGIT **digit);
%include cpointer.i
%pointer_functi ons(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($in put);
$2 = (MY_DIGIT **) malloc(($1+1)*s izeof(long *));
for (i = 0; i < $1; i++) {
PyObject *o = PyList_GetItem( $input,i);
if (PyString_Check (o))
$2[i] = PyString_AsStri ng(PyList_GetIt em($input,i));
else {
PyErr_SetString (PyExc_TypeErro r,"list must contain strings");
free($2);
return NULL;
}
}
$2[i] = 0;
} else {
PyErr_SetString (PyExc_TypeErro r,"not a list");
return NULL;
}
}

%typemap(freear g) (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*****@qualco mm.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)

iD8DBQFDRPq78jA dENsUuJsRAiwfAJ 43iL7y8B1PNSgzu YVte+40UuDI5wCg q/4K
vSRoo7AzIRZewYt 1SYxMuXU=
=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_functi ons(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.c om)

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

i tried..
%include cpointer.i
%pointer_functi ons(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_functi ons(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($in put);
$2 = (MY_DIGIT **) malloc(($1+1)*s izeof(long *));
for (i = 0; i < $1; i++) {
PyObject *o = PyList_GetItem( $input,i);
if (PyString_Check (o))
$2[i] = PyString_AsStri ng(PyList_GetIt em($input,i));
else {
PyErr_SetString (PyExc_TypeErro r,"list must contain strings");
free($2);
return NULL;
}
}
$2[i] = 0;
} else {
PyErr_SetString (PyExc_TypeErro r,"not a list");
return NULL;
}
}

%typemap(freear g) (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*****@qualco mm.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
2016
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 Python windows FAQ I shouldn't have to make a seperate dll, see link below). I create a MyModule.i file for swig which contains a simple test c++ class. I run swig on it with the following options: swig -python -c++ -includeall -shadow MyModule.i,...
1
3788
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 = PyFloat_FromDouble($1);" %include mvf.h
0
1538
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
2819
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. Trying to build using mingw out of the cygwin environment but the final call to link the extension calls directly to 'cc' which I think should really be g++ at that point.
1
2162
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
3396
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 rest of what I need to do for my project. I am very familiar with Python, but less so with C++ and Boost or SWIG. Does anyone have an example with all steps that I can follow? More specifically I am looking for the C++ code, ".i" file for...
2
4454
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 */ #include <time.h>
5
7329
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 module is "imageio" and the reader function from the file is image_read() which returns an object ( "filled" C structure) with a lot of members.
0
9670
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10211
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10159
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9033
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3719
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2917
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.