Walter Dörwald added the comment:

See http://bugs.python.org/issue18861 and the discussion started here: 
https://mail.python.org/pipermail/python-dev/2013-November/130155.html.

Basically it allows to add context information to a traceback without changing 
the type of the exception.

In the following example:

   import itertools
   ', '.join(itertools.chain((str(i) for i in range(100)), [42]))

the join method itself adds context information to the TypeError:

   Traceback (most recent call last):
     File "hurz.py", line 2, in <module>
       ', '.join(itertools.chain((str(i) for i in range(100)), [42]))
   TypeError: sequence item 100: expected str instance, int found

i.e. the "sequence item 100" is context information.

However when the exception occurs higher up in the call chain, no such context 
information is added:

   import itertools

   def foo(x):
      return str(x+1)

   ', '.join(foo(x) for x in itertools.chain(range(100), [None]))

This gives:

   Traceback (most recent call last):
     File "hurz.py", line 6, in <module>
       ', '.join(foo(x) for x in itertools.chain(range(100), [None]))
     File "hurz.py", line 6, in <genexpr>
       ', '.join(foo(x) for x in itertools.chain(range(100), [None]))
     File "hurz.py", line 4, in foo
       return str(x+1)
  TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

With frame annotations the traceback might look like this:

   Traceback (most recent call last):
     File "hurz.py", line 6, in <module>
       ', '.join(foo(x) for x in itertools.chain(range(100), [None]))
     File "hurz.py", line 6, in <genexpr>: sequence item 100
       ', '.join(foo(x) for x in itertools.chain(range(100), [None]))
     File "hurz.py", line 4, in foo
       return str(x+1)
  TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue19585>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to