Pass a tuple (or list) to a C wrapper function

J

Java and Swing

I have a C function which takes an array of long values..

I understand that I can pass a tuple to a C wrapper function and in the
C wrapper function have..

int ok = PyArg_ParseTuple(args, "s(ll)", &a, &b, &c);

...that's great if my tuple only contained two longs..but what if it
contained 50
would I have to do..

int ok = PyArg_ParseTuple(args, "s(llllllllllllllllll....ll)", &a, &b,
&c, &d...) ??

how can I handle this?
 
J

Jeremy Moles

It depends on how you want to manipulate the data in C. If you want
compile-time variable access to each float, yeah, 50 floats. :)

Probably what you want to do though is just keep the tuple as is and
iterate over it using the PySequence_* protocol:

http://docs.python.org/api/sequence.html
 
J

Java and Swing

Fredrik...I forgot about that...wish Google Groups had a way to quickly
find the topics a user posts.

anyhow, for receiving an object from python..is it

ok = PyArg_ParseTuple(args, "sO", &x, &y);

....is it "sO" or "s0" ....is it O (as in the letter) or 0 (as in the
number)? I would think "O" the letter..but it looks like a zero.

thanks
 
J

Java and Swing

Fredrik,
...I tried using your code...

static long *get_long_array(PyObject *data, int *data_size) {
int i, size;
long* out;
PyObject* seq;

seq = PySequence_Fast(data, "expected a sequence");
if (!seq)
return NULL;

size = PySequence_Size(seq);
if (size < 0)
return NULL;

if (data_size)
*data_size = size;

out = (long*) PyMem_Malloc(size * sizeof(long));
if (!out) {
Py_DECREF(seq);
PyErr_NoMemory();
return NULL;
}

for (i = 0; i < size; i++)
out = PyInt_AsLong(PySequence_Fast_GET_ITEM(seq, i));

Py_DECREF(seq);

if (PyErr_Occurred()) {
PyMem_Free(out);
out = NULL;
}

return out;

}


and I get this error..

C:\project\myapp.c(549) : error C2040: 'get_long_array' : 'long
*(struct _object *,int *)' differs in levels of indirection from 'int
()'

any idea?
 
J

Java and Swing

I got it. I had get_long_array placed after the method that was
calling it..
i.e.

void doStuf(...) {
x = get_long_array(...);
}

static long *get_long_array(PyObject *data, int *data_size) {
....
}

....I put get_long_array before it in my code..and its fine.

Thanks
Fredrik,
...I tried using your code...

static long *get_long_array(PyObject *data, int *data_size) {
int i, size;
long* out;
PyObject* seq;

seq = PySequence_Fast(data, "expected a sequence");
if (!seq)
return NULL;

size = PySequence_Size(seq);
if (size < 0)
return NULL;

if (data_size)
*data_size = size;

out = (long*) PyMem_Malloc(size * sizeof(long));
if (!out) {
Py_DECREF(seq);
PyErr_NoMemory();
return NULL;
}

for (i = 0; i < size; i++)
out = PyInt_AsLong(PySequence_Fast_GET_ITEM(seq, i));

Py_DECREF(seq);

if (PyErr_Occurred()) {
PyMem_Free(out);
out = NULL;
}

return out;

}


and I get this error..

C:\project\myapp.c(549) : error C2040: 'get_long_array' : 'long
*(struct _object *,int *)' differs in levels of indirection from 'int
()'

any idea?

Fredrik said:
I did post a complete and tested example a few days ago, which contained
code that showed how to do this. a complete waste of time, of course.

</F>
 
F

Fredrik Lundh

Java said:
and I get this error..

C:\project\myapp.c(549) : error C2040: 'get_long_array' : 'long
*(struct _object *,int *)' differs in levels of indirection from 'int
()'

so what's on line 549 in myapp.c?

what other warnings did you get from the compiler?

do you have other things named "get_long_array" in your program ?

(I'm really beginning to think that you should take a break and learn
a little more C before continuing on this task; C isn't that hard, but
it's not really a language you can use to write programs by trial and
error...)

</F>
 
F

Fredrik Lundh

Java and Swing said:
anyhow, for receiving an object from python..is it

ok = PyArg_ParseTuple(args, "sO", &x, &y);

...is it "sO" or "s0" ....is it O (as in the letter) or 0 (as in the
number)? I would think "O" the letter..but it looks like a zero.

eh? if you're not sure, what keeps you from cutting and pasting ?
or comparing it visually with characters that you type in yourself ?

(it's the letter O, for Object. the corresponding variable must be a
pointer to a PyObject pointer. see the documentation for details:

http://www.python.org/doc/current/api/arg-parsing.html

)

</F>
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top