467,114 Members | 1,278 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,114 developers. It's quick & easy.

help with Python C-API, tuple object

Hi,
sorry for the rather basic question but I've searched everywhere and
don't find an answer.
I want to call PyObject_CallObject from the Python C-API and pass a
tuple I've created from a C-array

How can I pass the tuple as an object rather then having to declare
the python function with the number of arguments equal to the no of
elements in the tuple?

Example:
C-Code fragment:
PyObject *pArgs = PyTuple_New(3);
//module is imported and function object is build and checked
for (i=0; i<3; i++){
pInt = PyInt_FromLong(i);
error = PyTuple_SetItem(pArgs, i, pInt);
}
pValue=PyObject_CallObject(pFunc, pArgs);//returns NULL!!!

Python Script:
def length(a):
length = len(a)
return length

May 12 '07 #1
  • viewed: 3443
Share:
2 Replies
On 11 May 2007 21:11:06 -0700, fabian.conrad wrote
Hi,
sorry for the rather basic question but I've searched everywhere and
don't find an answer.
I want to call PyObject_CallObject from the Python C-API and pass a
tuple I've created from a C-array

How can I pass the tuple as an object rather then having to declare
the python function with the number of arguments equal to the no of
elements in the tuple?
The documentation at http://docs.python.org/api/object.html lists various Call
flavors. The one you chose will unpack the tuple as if you called
pFunc(*pArgs), which results in the undesired behavior of pFunc receiving
three arguments. You want it to receive one argument that is the tuple pArgs.

One (bad) way of achieving this would be to build another tuple of size one,
stick pArgs into it, and pass that size-one tuple to PyObject_CallObject. A
better way is to use PyObject_CallFunctionObjArgs. If I'm not mistaken, you'll
want something like

pValue = PyObject_CallFunctionObjArgs(pFunc, pArgs, NULL);

Hope this helps,

--
Carsten Haese
http://informixdb.sourceforge.net

May 12 '07 #2
On May 12, 2:49 pm, "Carsten Haese" <cars...@uniqsys.comwrote:
On 11 May 2007 21:11:06 -0700, fabian.conrad wrote
Hi,
sorry for the rather basic question but I've searched everywhere and
don't find an answer.
I want to call PyObject_CallObject from the Python C-API and pass a
tuple I've created from a C-array
How can I pass the tuple as an object rather then having to declare
the python function with the number of arguments equal to the no of
elements in the tuple?

The documentation athttp://docs.python.org/api/object.htmllists various Call
flavors. The one you chose will unpack the tuple as if you called
pFunc(*pArgs), which results in the undesired behavior of pFunc receiving
three arguments. You want it to receive one argument that is the tuple pArgs.

One (bad) way of achieving this would be to build another tuple of size one,
stick pArgs into it, and pass that size-one tuple to PyObject_CallObject. A
better way is to use PyObject_CallFunctionObjArgs. If I'm not mistaken, you'll
want something like

pValue = PyObject_CallFunctionObjArgs(pFunc, pArgs, NULL);

Hope this helps,

--
Carsten Haesehttp://informixdb.sourceforge.net
Thanks billions!!! Works perfect, the world would be a lot worse
without Pro's like you guys helping newbies like me ;-)

May 12 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Guido van Rossum | last post: by
3 posts views Thread by danissim | last post: by
10 posts views Thread by Jacek Generowicz | last post: by
reply views Thread by python-help-bounces@python.org | last post: by
2 posts views Thread by Tomislav Lepusic | last post: by
3 posts views Thread by stuart_white_@hotmail.com | last post: by
1 post views Thread by Paul Rubin | last post: by
reply views Thread by Jack Wu | last post: by
2 posts views Thread by Scott Smith | last post: by
reply views Thread by Support Desk | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.