Help in unwrapping a PyObject* returned by an embedded Pythonfunction

R

Read Roberts

Environment: Mac OSX 10.2.4, vanilla Python 2.3.2 install

Why does PyArg_ParseTuple fail to parse a return value which is a tuple?


When inside a C extension function, I call a Python function :
pyStringResult = PyEval_CallObject(pyEN_callback, arglist);

The Python function runs just fine, and returns a string value.
The following works fine, where name is (char**).

from Python callback function: return myString
in calling C function: *name =
PyString_AsString(pyStringResult);

However, the following does not work:
from Python callback function: return (myString)
in calling C function:
if (!PyArg_ParseTuple(pyStringResult, "s", name)) {
dbprintf(4, "c: failed to parse return value\n");
PyErr_Clear();
}
PyArg_ParseTuple fails, and returns NULL. How come? By the
documentation, I would think that PyArg_ParseTuple would parse any
Python tuple object.
 
R

Read Roberts

Thank you taking the time to read and reply.

In fact, I did try all combinations of

in the Python call-back function :
return myString
return (myString)

and, in the C calling function,

PyArg_ParseTuple(pyStringResult, "s", myString)
PyArg_ParseTuple(pyStringResult, "(s)", myString)

Also, the Python documentation (and other working code examples I
have) indicate that 'PyArg_ParseTuple', unlike BuildValues, always
assumes the arguments are supplied in a argument tuple, even if there
is only one argumen.

I speculate that an argument list object, as required by
PyArg_ParseTuple, is not in fact a simple tuple., and that
PyArg_ParseTuple cannot be used to parse a regular tuple. Any idea if
this is correct?
 
D

David Boddie

[Returning a string/tuple to a C function]
In fact, I did try all combinations of

in the Python call-back function :
return myString
return (myString)

and, in the C calling function,

PyArg_ParseTuple(pyStringResult, "s", myString)
PyArg_ParseTuple(pyStringResult, "(s)", myString)

I'm guessing here but, if you want to pass tuples around, have you tried
using the second form of each of the above with the following correction
to the Python code?

return (myString,)
Also, the Python documentation (and other working code examples I
have) indicate that 'PyArg_ParseTuple', unlike BuildValues, always
assumes the arguments are supplied in a argument tuple, even if there
is only one argumen.

Maybe. I haven't really thought about it.
I speculate that an argument list object, as required by
PyArg_ParseTuple, is not in fact a simple tuple., and that
PyArg_ParseTuple cannot be used to parse a regular tuple. Any idea if
this is correct?

I think that there's been a misunderstanding. Since your original message,
and some unavailable reply were quoted below your last message, I think I
can see where it happened.

I don't know whether it does or not, but you have returned a string!
This happened because you need to use a trailing comma within the
brackets. This tells the interpreter that the return value is not
simply a string enclosed in brackets. You can see this from the
following output from an interactive session in Python:
('string',)

In your original example, you returned a string and converted it
successfully to a char pointer. Presumably, you wanted a more
general solution in order to catch any non-string objects returned
and turned to PyArg_ParseTuple. Maybe you could put the returned
object in a tuple and call PyArg_ParseTuple on the result, but
there's surely an easier way to check the validity of the return
value.

Hope this helps,

David
 

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
474,175
Messages
2,570,944
Members
47,491
Latest member
mohitk

Latest Threads

Top