[New-bugs-announce] [issue14739] Add PyArg_Parse format unit like O& but providing context

Larry Hastings report at bugs.python.org
Mon May 7 11:35:09 CEST 2012


New submission from Larry Hastings <larry at hastings.org>:

If you write a PyArg_Parse "converter", and your conversion hits an error, you must raise an exception and error out.  But, since your converter has no context about the parameter, it can't provide any helpful information in the error.

For example, PyUnicode_FSConverter attempts to convert a PyUnicode object to a PyBytes object; if it gets back some other kind of object, it calls
     PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes");
What parameter did this happen with?  When calling what function?  The hapless programmer is forced to guess.

In practice, the argument parser generally knows the name of the function and the name of the parameter.  It'd be nice to encode those values in the error.  But that information isn't passed in to the converter.

I propose adding a new format unit, let's call it 'O%', which is identical to 'O&' except for the signature of the converter function:

    int converter(PyObject *o, void *p, PyConverterContext_t *context);

where PyConverterContext_t is defined as

    typedef struct {
        char *function_name;
        char *parameter_name;
    } PyConverterContext_t;

If the function name or parameter name is not known, it will be NULL.

----------
messages: 160125
nosy: larry
priority: normal
severity: normal
status: open
title: Add PyArg_Parse format unit like O& but providing context

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue14739>
_______________________________________


More information about the New-bugs-announce mailing list