473,326 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

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?

Oct 12 '05 #1
7 1769
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

On Wed, 2005-10-12 at 13:06 -0700, Java and Swing wrote:
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?


Oct 12 '05 #2
Jeremy Moles wrote:
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


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>

Oct 13 '05 #3
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

Fredrik Lundh wrote:
Jeremy Moles wrote:
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


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>


Oct 13 '05 #4
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[i] = 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 Lundh wrote:
Jeremy Moles wrote:
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


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>


Oct 13 '05 #5
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

Java and Swing wrote:
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[i] = 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 Lundh wrote:
Jeremy Moles wrote:
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


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>


Oct 13 '05 #6
Java and Swing wrote:
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>

Oct 13 '05 #7
"Java and Swing" wrote:
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>

Oct 13 '05 #8

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

Similar topics

2
by: sci | last post by:
class A; void function(vector<A>* l) { .... } main(){ vector<A> aAList; function(&aAList);
5
by: Java and Swing | last post by:
I am having trouble with a wrapper function... My C code looks like ----------------------- #include <stdlib.h> #include <string.h> #include "Python.h" int doStuff(const char *input, const...
14
by: Java and Swing | last post by:
static PyObject *wrap_doStuff(PyObject *self, PyObject *args) { // this will store the result in a Python object PyObject *finalResult; // get arguments from Python char *result = 0; char *in=...
3
by: franco ziade | last post by:
I am stuck :( I am trying to pass the address of a function as an argument to a function (some OS call to launch a task): Example: -------- Declaration of the OS "lunch a Task" LunchTask(...,...
3
by: Brian Pittman | last post by:
Hi, I was wondering how to go about creating a wrapper function for uploading files to the server? I have made an attempt on my own without success. I don't get any exceptions from the...
4
by: peterbe | last post by:
This works exactly as you would expect:: from time import sleep def foo(on='ABC'): for e in list(on): sleep(1) yield e When I run this on the command line It takes about 3 seconds to...
6
by: Wijaya Edward | last post by:
Hi, How can I pass Array, Hash, and a plain variable in to a function at the same time. I come from Perl. Where as you probably know it is done like this: sub myfunc {
3
by: erikcw | last post by:
Hi, I'm getting the following error when I try to pass a list into a function. My List: crea = Traceback (most recent call last): File "wa.py", line 118, in ? curHandler.walkData()
1
by: Ben Warren | last post by:
Hello, Let's say I have a function with a variable number of arguments(please ignore syntax errors): def myfunc(a,b,c,d,...): and I have a tuple whose contents I want to pass to the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.