[Python-Dev] [Python-checkins] cpython: Close #17828: better handling of codec errors

Walter Dörwald walter at livinglogic.de
Thu Nov 14 18:27:37 CET 2013

On 14.11.13 14:22, Walter Dörwald wrote:

> On 13.11.13 17:25, Nick Coghlan wrote:
 >> [...]
>> A more elegant (and comprehensive) solution as a PEP for 3.5 would
>> certainly be a nice thing to have, but I think this is still much
>> better than the 3.3 status quo.
> Thinking further about this, I like your "frame annotation" suggestion
> Tracebacks could then look like this:
>  >>> b"hello".decode("uu_codec")
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>: decoding with 'uu_codec' codec
> failed
> ValueError: Missing "begin" line in input data
> In fact the traceback already lays out the chain of events. What is
> missing is simply a little additional information.
> Could frame annotation be added via decorators, i.e. something like this:
> @annotate("while doing something with {param}")
> def func(param):
>     do something
> annotate() would catch the exception, call .format() on the annotation
> string with the local variables of the frame as keyword arguments,
> attach the result to a special attribute of the frame and reraise the
> exception.
> The traceback machinery would simply have to print this additional
> attribute.

http://bugs.python.org/19585 is a patch that implements that. With the 
patch the following code:

    import traceback

    @traceback.annotate("while handling x={x!r}")
    def handle(x):
       raise ValueError(42)


will give the traceback:

    Traceback (most recent call last):
      File "spam.py", line 8, in <module>
      File "frame-annotation/Lib/traceback.py", line 322, in wrapped
        f(*args, **kwargs)
      File "spam.py", line 5, in handle: while handling x='spam'
        raise ValueError(42)
    ValueError: 42

Unfortunaty the frame from the decorator shows up in the traceback.


More information about the Python-Dev mailing list