473,320 Members | 1,823 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,320 software developers and data experts.

SWIG and char* newb questions :)

Hi i'm relatively new to Python and my C/C++ knowledge is near to
None. Having said that I feel justified to ask stupid questions :)

Ok now more seriously. I have question refering to char* used as
function parameters to return values. I have read SWIG manual to find
best way to overcome that, but there are many warnings about memory
leaks and stuff, so I feel confused.

Ok to put it more simply: how to safely define a variable in Python
and have it modified by C/C++ function?
Even better would be a way to make a tuple of return value and out
parameters, but thats probably a lot more work.

Any hint will be appreciated!
Jul 29 '08 #1
3 2101
Am Dienstag, 29. Juli 2008 12:51:36 schrieb code_berzerker:
Ok now more seriously. I have question refering to char* used as
function parameters to return values. I have read SWIG manual to find
best way to overcome that, but there are many warnings about memory
leaks and stuff, so I feel confused.

Ok to put it more simply: how to safely define a variable in Python
and have it modified by C/C++ function?
At least for strings, this won't work. Python strings are immutable (and
Python optimizes some things based on this knowledge), and as such you can
pass a Python string(-object) into a C/C++ function and retrieve its value
there as a const (!) char* using the PyString_*-API (I have no idea how this
is encapsulated in SWIG), but cannot/should not modify it (a const_cast<is
almost always a sign of bad programming, anyway).

The only real choice you have is to have your wrapper return a new string
object, created using one of the PyString_FromString[AndSize] functions.
Check the Python C-API documentation for more info on this.

Anyway, on a different note, I personally have always found it simpler to not
use SWIG to generate C extensions for Python, but to use the Python C-API
directly.

Hope this helps!

--
Heiko Wundram
Jul 29 '08 #2
Ok I think I got it:
PyObject* myFuncXXX(char* p_1, int p_2, char* p_3, int p_4)
{
int res;
char _host[255] = "";
int _port;
res = funcXXX(p_1, p_2, p_3, p_4, _host, &_port);

PyObject* res1 = PyInt_FromLong(res);
PyObject* res2 = PyString_FromStringAndSize(_host, strlen(_host));
PyObject* res3 = PyInt_FromLong(_port);

PyObject* resTuple = PyTuple_New(3);

PyTuple_SetItem(resTuple, 0, res1);
PyTuple_SetItem(resTuple, 1, res2);
PyTuple_SetItem(resTuple, 2, res3);

return resTuple;
}

It seems to work when I put it into swig's "*.i" file.

me proud of me.self :D
Jul 29 '08 #3
code_berzerker wrote:
Hi i'm relatively new to Python and my C/C++ knowledge is near to
None. Having said that I feel justified to ask stupid questions :)
Have you considered using Cython? It's almost Python, but it compiles to C
code for a Python extension module and even lets you call C functions directly
from your code.

http://cython.org/

Stefan
Jul 29 '08 #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...
0
by: Norman Young | last post by:
Hi, Forgive me that the following aren't really Python questions but SWIG questions: 1) I'm trying to SWIG a function that looks like this: void print(ofstream out); The problem is that...
0
by: Chris | last post by:
Hi I have a C function that builds a list of null terminated strings: void buildStringList(char **asStrings, int n); The number of elements in the list is given in input. I'd like to have...
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) {
0
by: Java and Swing | last post by:
When I try to print an object in Python, python.exe crashes. Below are my files. defs.h ------------------------------------------------------------------------------------------------------...
3
by: Java and Swing | last post by:
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... ...
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...
3
by: Soren | last post by:
Hi, I went through the SWIG tutorial for the example named "simple". I managed to get to the first step, creating example_wrap.c using swig, and doing: "gcc -fpic -c example_wrap.c...
1
by: Sells, Fred | last post by:
I'm using python 2.4 under linux (centos 5.1). I need to pass an array of doubles to a c function but am getting an error, shown near the bottom of this post....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.