[New-bugs-announce] [issue28603] traceback module can't format/print unhashable exceptions

Andreas Stührk report at bugs.python.org
Thu Nov 3 17:22:14 EDT 2016


New submission from Andreas Stührk:

The traceback module tries to handle loops caused by an exception's __cause__ or __context__ attributes when printing tracebacks. To do so, it adds already seen exceptions to a set. Unfortunately, it doesn't handle unhashable exceptions:

>>> class E(Exception): __hash__ = None
...
>>> traceback.print_exception(E, E(), None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/traceback.py", line 100, in print_exception
    type(value), value, tb, limit=limit).format(chain=chain):
  File "/usr/lib/python3.5/traceback.py", line 439, in __init__
    _seen.add(exc_value)
TypeError: unhashable type: 'E'

CPython's internal exception printing pretty much does the same, except it ignores any exception while operating on the seen set (see https://hg.python.org/cpython/file/8ee4ed577c03/Python/pythonrun.c#l813 ff).

Attached is a patch that makes the traceback module ignore TypeErrors while operating on the seen set. It also adds a (minimal) test.

----------
components: Library (Lib)
files: unhashable_exceptions.diff
keywords: patch
messages: 280022
nosy: Trundle
priority: normal
severity: normal
status: open
title: traceback module can't format/print unhashable exceptions
Added file: http://bugs.python.org/file45342/unhashable_exceptions.diff

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


More information about the New-bugs-announce mailing list