PyArg_Parse vs PyArg_ParseTuple

Ignacio Vazquez-Abrams ignacio at openservices.net
Wed Oct 17 14:20:08 EDT 2001


On Wed, 17 Oct 2001, David Brady wrote:

> >-----Original Message-----
>  > From: Ignacio Vazquez-Abrams [mailto:ignacio at openservices.net]
>  > Sent: Tuesday, October 16, 2001 7:42 PM
>  >
>  > > I read in the documentatian that PyArg_Parse() is
>  > deprecated, so I would
>  > > like to get this working correctly.
>  >
>  > You're misunderstanding what PyArg_Parse() and
>  > PyArg_ParseTuple() are used
>  > for. They are used for when a C function is called by a
>  > Python program, not
>  > the other way around. In your case you should probably be
>  > using one of the
>  > PyInt_As*() or PyLong_As*() functions.
>
> Okay, two things.  First, thank you for replying!  The python list is as
> responsive and useful as the language!
>
> Second, I'm a little confused by your explanation.  The code I posted is
> copied almost straight from Programming Python (2e, p1157,
> "embed-object.c").

Huh, interesting. Nuke. Housefly.

> Also, my reading of Programming Python, Python Essential Reference, and
> the online Extending/Embedding Tutorial has led me to believe that
> Py_BuildValue() for moving data from C types to Python types, and
> PyArg_Parse*() are for going the other direction.  The only difference
> between embedding and extending is that the order of their use is
> reversed.  Extensions in C have to Parse the arguments passed in from
> Python, and have to Build the return value, whereas embedding code has
> to Build the arguments and Parse the return.  In both cases, C-to-Python
> is a Build, where Python-to-C is a Parse.
>
> I may be wrong.  But at least this lets you know I'm trying!  :-)

Well, you can do it that way, but see my previous comment above. If you know
that the PyObject is actually just a single value, then PyArg_Parse*() is
way overkill.

> Thank you for pointing out PyInt_As* and PyInt_From*.  They appear to do
> exactly what I need in this case as well.  After reading their
> documentation, I think the difference between 'val = PyInt_AsLong(parg)'
> and 'PyArg_Parse(parg, "d", &val)' is that PyArg_Parse() could handle
> more than one object at a time, where PyInt_AsLong() requires that I
> pass it a single PyObject that contains an integer.  Do I understand
> this correctly?

Yes.

> Finally, this brings me back to my original question.  Why does
> PyArg_ParseTuple() not work?  I should point out that this is ultimately
> intended for an embedded macro language, so there will have to be some
> extension code involved eventually as well.

It has to do with the "Tuple" part of the function name. PyArg_ParseTuple()
expects to be decoding a tuple, but the function only returns an integer.

-- 
Ignacio Vazquez-Abrams  <ignacio at openservices.net>

   "As far as I can tell / It doesn't matter who you are /
    If you can believe there's something worth fighting for."
       - "Parade", Garbage





More information about the Python-list mailing list