unpacking Python tuple in C

Michael Hudson mwh21 at cam.ac.uk
Wed Dec 13 11:45:25 CET 2000

"Mike Müller" <mmueller at dgfz.de> writes:

> Hi,
> Calling Python from C I get a PythonTupleObject

C from Python, do you mean?

> PyObject *result = PyObject_CallMethod(...)
> PyTuple_Check(result)  is 1 so its really a tuple!
> The tuple contains only Python floats.
> Using
> PyArg_ParseTuple(result, "dd",  &double1, &double2)
> does not change double1, or double2.

What does PyArg_ParseTuple return?  If, as you say later, result is
quite a long tuple then it will have raised an exception.

>  I can get  an item of the tuple:
> PyObject* floatFromPy0 = PyTuple_GetItem(result, 0);
>   *double1= PyFloat_AsDouble(floatFromPy0)
> since the tuple is pretty long and I need all of the tuple items
> PyTuple_GetItem seems to not the best solution.

Why not?  Why not use a loop and stuff the values into an array of

> Any hints how I can get all the tuple items into C doubles?

Supplying the right number of d's to PyArg_ParseTuple, maybe?

Stepping back from your immediate problem, what are you actually
trying to do?  I can infer it from what you've said here, I'm afraid.


  Any form of evilness that can be detected without *too* much effort
  is worth it...  I have no idea what kind of evil we're looking for
  here or how to detect is, so I can't answer yes or no.
                                       -- Guido Van Rossum, python-dev

More information about the Python-list mailing list