473,782 Members | 2,498 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2121
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_FromSt ring[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_FromSt ringAndSize(_ho st, 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
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,...
0
404
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 the variable "out" is a keyword in the target
0
1595
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 this function available in Python. I use SWIG but I'm a complete newbie with it. I've only found an example to convert a Python list to C char** http://www.swig.org/Doc1.3/Python.html#Python_nn59
1
2161
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
1055
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 ------------------------------------------------------------------------------------------------------ typedef unsigned long MY_DIGIT; myapp.c ------------------------------------------------------------------------------------------------------
3
3143
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... int doIt(char *a, MY_DIGIT **digit) { ... }
2
4453
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>
3
3695
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 -IC:\python24\include " to create example_wrap.o But when I needed to compile the library file using:
1
1664
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. ---------------------------------------------------- my swig interface file looks like this * File: rug520.i */ %module rug520 %include "typemaps.i"
0
9641
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
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10313
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
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
10080
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
8968
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
7494
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
4044
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
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.