SWIG and char* newb questions :)

C

code_berzerker

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!
 
H

Heiko Wundram

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!
 
C

code_berzerker

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
 
S

Stefan Behnel

code_berzerker said:
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
 

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

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top