Help in unwrapping a PyObject* returned by an embedded Python function

Read Roberts rroberts at adobe.com
Tue Jan 13 12:07:45 EST 2004


You have resolved my confusion. The key that I was missing is that  " 
(item)" does not evaluate as a tuple containing "item", that I have 
to add a comma to force the value to be a tuple with a single 
element. Somehow I have managed to not notice this, despite some 
years of Python work.

Thank you very much!
- Read Roberts

>Hello,
>
>>  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)
>
>These 2 are the same, a tuple with length one has a comma in it, like
>
>     myString,
>
>or (I prefer having brackets around it)
>
>     (myString,)
>
>The key to making a tuple is the ',' though and not the '(' and ')'.
>
>>  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.
>
>PyArg_ParseTuple is intended to unravel arguments given to a function.
>These arguments are always stored in a tuple (or in Pythoneese, a
>sequence).
>
>>  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 would be highly surprised if this was the case. In my experience,
>Python is very orthogonal and robust. I expect these things to work.
>(I wrote a generator for interfacing Python from C++, and people using
>that generator do much more tricky things than what you try to do).
>
>I did an experiment, and it seems to be working here.
>
>Things to note:
>- PyArg_ParseTuple always needs a tuple as first argument (as you
>   already stated).
>- The return value from Python is passed 'as is', thus 'return "bla"'
>   gives a string PyObject, 'return ("bla",)' gives a tuple object with 1
>   string value, and 'return (("bla",),)' gives a tuple object with 1
>   tuple object containing 1 string object.
>
>Python 'return "bla"' will thus never be parsed by PyArg_ParseTuple,
>because it is not a tuple object.
>
>PyArg_ParseTuple(pyStringResult, "s", myString)
>will match 'return ("bla",)' and
>
>PyArg_ParseTuple(pyStringResult, "(s)", myString)
>will match 'return (("bla",),)'
>
>if myString is of type 'char **' .
>
>See also the attached example code.
>(x.c is a generated (c++) file, manually edited afterwards for the
>experiment. I left the generated code (which uses low level API calls)
>in, may be it is useful to you if you decide to do things yourself
>(and get rid of the tuple requirement from PyArg_ParseTuple in the
>process).
>
>
>If you want to parse 'return "bla"' with PyArg_ParseTuple, you will have
>to first construct a tuple with 1 element around the return value in C
>before handing it over to PyArg_ParseTuple.
>(ie something like
>    tuple==PyTuple_New(1);
>    PyTuple_SET_ITEM(tuple,0,pyStringResult);
>    PyArg_ParseTuple(tuple, "s", myString);
>
>    (followed by the usual DECREF, etc).
>)
>
>
>Hope this helps.
>
>
>Albert
>--
>Unlike popular belief, the .doc format is not an open publically 
>available format.
>
>Content-Type: TEXT/PLAIN; charset=US-ASCII; name="x.c"
>Content-ID: <Pine.LNX.4.44.0401131043030.1593 at se-126.se.wtb.tue.nl>
>Content-Description:
>Content-Disposition: attachment; filename="x.c"
>
>Attachment converted: \:x.c (TEXT/ttxt) (0023E0B6)
>Content-Type: TEXT/PLAIN; charset=US-ASCII; name="bla.py"
>Content-ID: <Pine.LNX.4.44.0401131043031.1593 at se-126.se.wtb.tue.nl>
>Content-Description:
>Content-Disposition: attachment; filename="bla.py"
>
>Attachment converted: \:bla.py (TEXT/ttxt) (0023E0B7)
>Content-Type: TEXT/PLAIN; charset=US-ASCII; name="mk"
>Content-ID: <Pine.LNX.4.44.0401131043032.1593 at se-126.se.wtb.tue.nl>
>Content-Description:
>Content-Disposition: attachment; filename="mk"
>
>Attachment converted: \:mk (TEXT/ttxt) (0023E0B8)

-- 
-----------------------------------------------------------------------------------------
Read K. Roberts			rroberts at adobe.com
Adobe Systems San Jose TW12 	(408)-536-4402 on Weds
San Francisco 			(415) 642-5642 Mon, Tues, Thurs, Fri




More information about the Python-list mailing list