Skip to content
This repository has been archived by the owner on Nov 25, 2023. It is now read-only.

bpo-28603 Fix tracebacks for unhashable exceptions #11

Merged
merged 1 commit into from Mar 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions traceback2/__init__.py
Expand Up @@ -438,11 +438,11 @@ def __init__(self, exc_type, exc_value, exc_traceback, limit=None,
# Handle loops in __cause__ or __context__.
if _seen is None:
_seen = set()
_seen.add(exc_value)
_seen.add(id(exc_value))
# Gracefully handle (the way Python 2.4 and earlier did) the case of
# being called with no type or value (None, None, None).
if (exc_value and getattr(exc_value, '__cause__', None) is not None
and exc_value.__cause__ not in _seen):
and id(exc_value.__cause__) not in _seen):
cause = TracebackException(
type(exc_value.__cause__),
exc_value.__cause__,
Expand All @@ -454,7 +454,7 @@ def __init__(self, exc_type, exc_value, exc_traceback, limit=None,
else:
cause = None
if (exc_value and getattr(exc_value, '__context__', None) is not None
and exc_value.__context__ not in _seen):
and id(exc_value.__context__) not in _seen):
context = TracebackException(
type(exc_value.__context__),
exc_value.__context__,
Expand Down
24 changes: 24 additions & 0 deletions traceback2/tests/test_traceback.py
Expand Up @@ -747,6 +747,30 @@ def test_context(self):
self.assertEqual(exc_info[0], exc.exc_type)
self.assertEqual(str(exc_info[1]), str(exc))

def test_unhashable(self):
class UnhashableException(Exception):
__hash__ = None

def __eq__(self, other):
return True

ex1 = UnhashableException('ex1')
ex2 = UnhashableException('ex2')
try:
raise_from(ex2, ex1)
except UnhashableException:
try:
raise ex1
except UnhashableException:
exc_info = sys.exc_info()
exc = traceback.TracebackException(*exc_info)
formatted = list(exc.format())
if six.PY2:
self.assertIn('UnhashableException: ex1\n', formatted[2])
else:
self.assertIn('UnhashableException: ex2\n', formatted[3])
self.assertIn('UnhashableException: ex1\n', formatted[7])

def test_limit(self):
def recurse(n):
if n:
Expand Down