Was Re: [Python-Dev] unicode/string asymmetries

Just van Rossum just@letterror.com
Wed, 9 Jan 2002 16:22:05 +0100


Thomas Heller wrote:

> Offtopic again: PyArg_ParseTuple() is also nice for parsing a tuple
> in C code, which you for example receive as a result from calling a method.
> IIRC the only problem here is that it may throw weird error
> messages if the object is not a tuple.
> Instead of 'TypeError: unpack non-sequence' you get a
> 'SystemError: new style getargs format but argument is not a tuple'.

You can do that with PyArg_Parse(), too, if you point parens around your format
string, as in this converter function:

    int
    CGPoint_Convert(PyObject *v, CGPoint *p_itself)
    {
        if( !PyArg_Parse(v, "(ff)",
                &p_itself->x,
                &p_itself->y) )
            return 0;
        return 1;
    }

The nice is that this will accept _any_ (length 2) sequence, not just tuples! So
this seems to be a case where PyArg_Parse() is actually better than
PyArg_ParseTuple().

Just