C Extension - return an array of longs or pointer?

J

Java and Swing

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

Brandon K

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)
}

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 ==----
 
B

Brandon K

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);

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)
}

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 ==----
 
J

Java and Swing

Interesting...thanks. Any good tutorials out there, other than the
python doc for ext?


thanks.

Brandon said:
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)
}

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 ==----
 
B

Bernhard Herzog

Brandon K said:
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
 
J

Jorgen Grahn

On 12 Oct 2005 04:46:34 -0700 said:
...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
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Brandon said:
PyTuple_SET_ITEM(py, i, Py_BuildValue("l",*result);


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

Regards,
Martin
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top