473,487 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C Extension - return an array of longs or pointer?

Hi,
I have been posting about writing a C extension for Python...so far,
so good. At least for the "simple" functions that I need to wrap.

Ok, my c function looks like...

MY_NUM *doNumberStuff(const char *in, const char *x) { ... }

MY_NUM is defined as, typedef unsigned long MY_NUM; (not sure if that
matters, or can i just create a wrapper which handles longs?)

anyhow..for my wrapper I have this..

static PyObject *wrap_doNumberStuff(PyObject *self, PyObject args) {
char *in = 0;
char *x = 0;
long *result = 0;
int ok = PyArg_ParseTuple(args, "ss", &in, &x);
if (!ok) return 0;

result = doNumberStuff(in, x);

return Py_BuildValue("l", result);
}

....my question is...in the c code, result is a pointer to an array of
longs, how can I get the returned result to be a list or something
similar to an array in Python?

....I also have a function which returns a character array (denoted by a
char *)...would it work the same as the previous question?

Thanks!!

Oct 12 '05 #1
6 6284
All the veteran programmers out there can correct me, but the way I did
it in my extension was this:

static PyObject *wrap_doNumberStuff(PyObject* self, PyObject* args)
{
char* in = 0;
char* x = 0;
long* result = 0;
int i = 0;
PyObject* py = PyTuple_New()
int ok = PyArg_ParseTuple(args,"ss",&in, &x);
if(!ok) return NULL;

result = doNumberStuff(in,x):
len = sizeof(result)/sizeof(long)
for(i;i < len; i++)
PyTuple_SET_ITEM(py, i,Py_BuildValue("l",*result[i])
}

Simple enough idea...i'm not quite sure if I've done everything
correctly with the pointers, but I'm sure you can figure that out, the
algorithm is simple enough.
Hi,
I have been posting about writing a C extension for Python...so far,
so good. At least for the "simple" functions that I need to wrap.

Ok, my c function looks like...

MY_NUM *doNumberStuff(const char *in, const char *x) { ... }

MY_NUM is defined as, typedef unsigned long MY_NUM; (not sure if that
matters, or can i just create a wrapper which handles longs?)

anyhow..for my wrapper I have this..

static PyObject *wrap_doNumberStuff(PyObject *self, PyObject args) {
char *in = 0;
char *x = 0;
long *result = 0;
int ok = PyArg_ParseTuple(args, "ss", &in, &x);
if (!ok) return 0;

result = doNumberStuff(in, x);

return Py_BuildValue("l", result);
}

...my question is...in the c code, result is a pointer to an array of
longs, how can I get the returned result to be a list or something
similar to an array in Python?

...I also have a function which returns a character array (denoted by a
char *)...would it work the same as the previous question?

Thanks!!

----== Posted via Newsgroups.com - Usenet Access to over 100,000 Newsgroups ==----
Get Anonymous, Uncensored, Access to West and East Coast Server Farms!
----== Highest Retention and Completion Rates! HTTP://WWW.NEWSGROUPS.COM ==----
Oct 12 '05 #2
I'm sorry...I just woke up and forgot my C...must have left it in the
Coffee...Anyway, i made a few mistakes (can't initialize blank
tuple...function should return a value, lol).

static PyObject* wrap_doNumberStuff(PyObject* self, PyObject* args)
{
char* in = 0;
char* x = 0;
long* result = 0;
int i = 0;
PyObject* py = NULL;
if(!PyArg_ParseTuple(args,"ss",&in,&x) return NULL;

result = doNumberStuff(in,x);
len = sizeof(result)/sizeof(long);
py = PyTuple_New(len);
for(i; i < len; i++)
PyTuple_SET_ITEM(py, i, Py_BuildValue("l",*result[i]);

return py;
}

Additionally, the Python/C api in the docs tells you all of these nifty
little abstract layer functions that you can call from your extension.
All the veteran programmers out there can correct me, but the way I did
it in my extension was this:

static PyObject *wrap_doNumberStuff(PyObject* self, PyObject* args)
{
char* in = 0;
char* x = 0;
long* result = 0;
int i = 0;
PyObject* py = PyTuple_New()
int ok = PyArg_ParseTuple(args,"ss",&in, &x);
if(!ok) return NULL;

result = doNumberStuff(in,x):
len = sizeof(result)/sizeof(long)
for(i;i < len; i++)
PyTuple_SET_ITEM(py, i,Py_BuildValue("l",*result[i])
}

Simple enough idea...i'm not quite sure if I've done everything
correctly with the pointers, but I'm sure you can figure that out, the
algorithm is simple enough.

----== Posted via Newsgroups.com - Usenet Access to over 100,000 Newsgroups ==----
Get Anonymous, Uncensored, Access to West and East Coast Server Farms!
----== Highest Retention and Completion Rates! HTTP://WWW.NEWSGROUPS.COM ==----
Oct 12 '05 #3
Interesting...thanks. Any good tutorials out there, other than the
python doc for ext?
thanks.

Brandon K wrote:
All the veteran programmers out there can correct me, but the way I did
it in my extension was this:

static PyObject *wrap_doNumberStuff(PyObject* self, PyObject* args)
{
char* in = 0;
char* x = 0;
long* result = 0;
int i = 0;
PyObject* py = PyTuple_New()
int ok = PyArg_ParseTuple(args,"ss",&in, &x);
if(!ok) return NULL;

result = doNumberStuff(in,x):
len = sizeof(result)/sizeof(long)
for(i;i < len; i++)
PyTuple_SET_ITEM(py, i,Py_BuildValue("l",*result[i])
}

Simple enough idea...i'm not quite sure if I've done everything
correctly with the pointers, but I'm sure you can figure that out, the
algorithm is simple enough.
Hi,
I have been posting about writing a C extension for Python...so far,
so good. At least for the "simple" functions that I need to wrap.

Ok, my c function looks like...

MY_NUM *doNumberStuff(const char *in, const char *x) { ... }

MY_NUM is defined as, typedef unsigned long MY_NUM; (not sure if that
matters, or can i just create a wrapper which handles longs?)

anyhow..for my wrapper I have this..

static PyObject *wrap_doNumberStuff(PyObject *self, PyObject args) {
char *in = 0;
char *x = 0;
long *result = 0;
int ok = PyArg_ParseTuple(args, "ss", &in, &x);
if (!ok) return 0;

result = doNumberStuff(in, x);

return Py_BuildValue("l", result);
}

...my question is...in the c code, result is a pointer to an array of
longs, how can I get the returned result to be a list or something
similar to an array in Python?

...I also have a function which returns a character array (denoted by a
char *)...would it work the same as the previous question?

Thanks!!

----== Posted via Newsgroups.com - Usenet Access to over 100,000 Newsgroups ==----
Get Anonymous, Uncensored, Access to West and East Coast Server Farms!
----== Highest Retention and Completion Rates! HTTP://WWW.NEWSGROUPS.COM ==----


Oct 12 '05 #4
Brandon K <pr***********@yahoo.com> writes:
long* result = 0; [...] result = doNumberStuff(in,x);
len = sizeof(result)/sizeof(long);


I don't think this will do what you appear to expect it to do.

Bernhard

--
Intevation GmbH http://intevation.de/
Skencil http://skencil.org/
Thuban http://thuban.intevation.org/
Oct 12 '05 #5
On 12 Oct 2005 04:46:34 -0700, Java and Swing <co*******@gmail.com> wrote:
....
...my question is...in the c code, result is a pointer to an array of
longs, how can I get the returned result to be a list or something
similar to an array in Python?
RTFM. There are API functions to create an empty list [] and to append
objects to a list. PyList_something(), I think.
...I also have a function which returns a character array (denoted by a
char *)...would it work the same as the previous question?


You might want to return this as a Python string, even if it's just random
octet data garbage. Depends on what that data really represents, but it's a
common idiom, and modules like struct and array expect their data to be
strings. Note that Python strings may contain NUL bytes.

/Jorgen

--
// Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
\X/ algonet.se> R'lyeh wgah'nagl fhtagn!
Oct 12 '05 #6
Brandon K wrote:
PyTuple_SET_ITEM(py, i, Py_BuildValue("l",*result[i]);


Using Py_BuildValue is overkill here. PyInt_FromLong will do just
as well.

Regards,
Martin
Oct 12 '05 #7

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

Similar topics

2
1737
by: Letbetter, Jason | last post by:
I'm creating Python extensions for several c/c++ components. I'm using swig to create the extensions. The biggest challenge so far is working with the c args between Python and the Python...
8
1787
by: Russell E. Owen | last post by:
I'm writing a C extension for numarray and am puzzled about the idiom for error handling. The documentation seems to say one should always decref an array after calling NA_InputArray, etc., to...
1
2243
by: sugaray | last post by:
I don't get why the correct way to allocate memory space for 3d-array should be like this in C++: long (*ptr); ptr=new long; and when deallocation delete ptr;
10
2209
by: Java and Swing | last post by:
I need to write an extension for a C function so that I can call it from python. C code (myapp.c) ====== typedef unsigned long MY_LONG; char *DoStuff(char *input, MY_LONG *x) { ... } so...
2
2863
by: Richard L Rosenheim | last post by:
I'm converting some routines from C to C#. The code involves having arrays of bytes which are passed to a function as an array of longs. Well, technically the C code has a pointer to an array of...
3
1428
by: rimmer | last post by:
I'm writing an extension module in C in which I'm passing an array of floats from C to python. The code below illustrates a simple C function designed to output an array of floats. ---------...
4
1938
by: Philip Semanchuk | last post by:
I'm writing a Python extension in C that wraps a function which takes a void * as a parameter. (The function is shmat() which attaches a chunk of shared memory to the process at the address...
0
1382
by: Philip Semanchuk | last post by:
On Oct 22, 2008, at 8:33 PM, Robert Kern wrote: I agree. To deny users of this module access to this param of shmat() would be design arrogance on my part. To do so would be to claim that I...
3
3020
by: Keith Thompson | last post by:
Victor <vhnguyenn@yahoo.comwrites: You're declaring an array of pointers to unsigned long long, but you're initializing the pointers with integer values. This is actually a constraint...
0
7106
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
6967
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
7137
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,...
0
7181
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...
0
7349
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...
0
5442
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,...
1
4874
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...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1381
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 ...

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.