Skip to content

Commit 2712247

Browse files
miss-islingtonserhiy-storchaka
authored andcommitted
[3.6] bpo-28603: Fix formatting tracebacks for unhashable exceptions (GH-4014) (#4024)
(cherry picked from commit de86073)
1 parent 858ea43 commit 2712247

File tree

9 files changed

+114
-9
lines changed

9 files changed

+114
-9
lines changed

‎Lib/idlelib/idle_test/test_run.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import unittest
2+
from unittest import mock
3+
4+
from test.support import captured_stderr
5+
import idlelib.run as idlerun
6+
7+
8+
class RunTest(unittest.TestCase):
9+
def test_print_exception_unhashable(self):
10+
class UnhashableException(Exception):
11+
def __eq__(self, other):
12+
return True
13+
14+
ex1 = UnhashableException('ex1')
15+
ex2 = UnhashableException('ex2')
16+
try:
17+
raise ex2 from ex1
18+
except UnhashableException:
19+
try:
20+
raise ex1
21+
except UnhashableException:
22+
with captured_stderr() as output:
23+
with mock.patch.object(idlerun,
24+
'cleanup_traceback') as ct:
25+
ct.side_effect = lambda t, e: t
26+
idlerun.print_exception()
27+
28+
tb = output.getvalue().strip().splitlines()
29+
self.assertEqual(11, len(tb))
30+
self.assertIn('UnhashableException: ex2', tb[3])
31+
self.assertIn('UnhashableException: ex1', tb[10])
32+
33+
34+
if __name__ == '__main__':
35+
unittest.main(verbosity=2)

‎Lib/idlelib/run.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,16 +203,16 @@ def print_exception():
203203
seen = set()
204204

205205
def print_exc(typ, exc, tb):
206-
seen.add(exc)
206+
seen.add(id(exc))
207207
context = exc.__context__
208208
cause = exc.__cause__
209-
if cause is not None and cause not in seen:
209+
if cause is not None and id(cause) not in seen:
210210
print_exc(type(cause), cause, cause.__traceback__)
211211
print("\nThe above exception was the direct cause "
212212
"of the following exception:\n", file=efile)
213213
elif (context is not None and
214214
not exc.__suppress_context__ and
215-
context not in seen):
215+
id(context) not in seen):
216216
print_exc(type(context), context, context.__traceback__)
217217
print("\nDuring handling of the above exception, "
218218
"another exception occurred:\n", file=efile)

‎Lib/test/test_traceback.py‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,33 @@ def fmt():
443443
' return traceback.format_stack()\n' % (__file__, lineno+1),
444444
])
445445

446+
@cpython_only
447+
def test_unhashable(self):
448+
from _testcapi import exception_print
449+
450+
class UnhashableException(Exception):
451+
def __eq__(self, other):
452+
return True
453+
454+
ex1 = UnhashableException('ex1')
455+
ex2 = UnhashableException('ex2')
456+
try:
457+
raise ex2 from ex1
458+
except UnhashableException:
459+
try:
460+
raise ex1
461+
except UnhashableException:
462+
exc_type, exc_val, exc_tb = sys.exc_info()
463+
464+
with captured_output("stderr") as stderr_f:
465+
exception_print(exc_val)
466+
467+
tb = stderr_f.getvalue().strip().splitlines()
468+
self.assertEqual(11, len(tb))
469+
self.assertEqual(context_message.strip(), tb[5])
470+
self.assertIn('UnhashableException: ex2', tb[3])
471+
self.assertIn('UnhashableException: ex1', tb[10])
472+
446473

447474
cause_message = (
448475
"\nThe above exception was the direct cause "
@@ -994,6 +1021,25 @@ def test_context(self):
9941021
self.assertEqual(exc_info[0], exc.exc_type)
9951022
self.assertEqual(str(exc_info[1]), str(exc))
9961023

1024+
def test_unhashable(self):
1025+
class UnhashableException(Exception):
1026+
def __eq__(self, other):
1027+
return True
1028+
1029+
ex1 = UnhashableException('ex1')
1030+
ex2 = UnhashableException('ex2')
1031+
try:
1032+
raise ex2 from ex1
1033+
except UnhashableException:
1034+
try:
1035+
raise ex1
1036+
except UnhashableException:
1037+
exc_info = sys.exc_info()
1038+
exc = traceback.TracebackException(*exc_info)
1039+
formatted = list(exc.format())
1040+
self.assertIn('UnhashableException: ex2\n', formatted[2])
1041+
self.assertIn('UnhashableException: ex1\n', formatted[6])
1042+
9971043
def test_limit(self):
9981044
def recurse(n):
9991045
if n:

‎Lib/traceback.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,11 +459,11 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
459459
# Handle loops in __cause__ or __context__.
460460
if _seen is None:
461461
_seen = set()
462-
_seen.add(exc_value)
462+
_seen.add(id(exc_value))
463463
# Gracefully handle (the way Python 2.4 and earlier did) the case of
464464
# being called with no type or value (None, None, None).
465465
if (exc_value and exc_value.__cause__ is not None
466-
and exc_value.__cause__ not in _seen):
466+
and id(exc_value.__cause__) not in _seen):
467467
cause = TracebackException(
468468
type(exc_value.__cause__),
469469
exc_value.__cause__,
@@ -475,7 +475,7 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
475475
else:
476476
cause = None
477477
if (exc_value and exc_value.__context__ is not None
478-
and exc_value.__context__ not in _seen):
478+
and id(exc_value.__context__) not in _seen):
479479
context = TracebackException(
480480
type(exc_value.__context__),
481481
exc_value.__context__,

‎Misc/ACKS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ Dominic Binks
145145
Philippe Biondi
146146
Michael Birtwell
147147
Stuart Bishop
148+
Zane Bitter
148149
Roy Bixler
149150
Daniel Black
150151
Jonathan Black
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Print the full context/cause chain of exceptions on interpreter exit, even
2+
if an exception in the chain is unhashable or compares equal to later ones.
3+
Patch by Zane Bitter.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a TypeError that caused a shell restart when printing a traceback that
2+
includes an exception that is unhashable. Patch by Zane Bitter.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
traceback: Fix a TypeError that occurred during printing of exception
2+
tracebacks when either the current exception or an exception in its
3+
context/cause chain is unhashable. Patch by Zane Bitter.

‎Python/pythonrun.c‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -811,13 +811,21 @@ print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
811811

812812
if (seen != NULL) {
813813
/* Exception chaining */
814-
if (PySet_Add(seen, value) == -1)
814+
PyObject *value_id = PyLong_FromVoidPtr(value);
815+
if (value_id == NULL || PySet_Add(seen, value_id) == -1)
815816
PyErr_Clear();
816817
else if (PyExceptionInstance_Check(value)) {
818+
PyObject *check_id = NULL;
817819
cause = PyException_GetCause(value);
818820
context = PyException_GetContext(value);
819821
if (cause) {
820-
res = PySet_Contains(seen, cause);
822+
check_id = PyLong_FromVoidPtr(cause);
823+
if (check_id == NULL) {
824+
res = -1;
825+
} else {
826+
res = PySet_Contains(seen, check_id);
827+
Py_DECREF(check_id);
828+
}
821829
if (res == -1)
822830
PyErr_Clear();
823831
if (res == 0) {
@@ -829,7 +837,13 @@ print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
829837
}
830838
else if (context &&
831839
!((PyBaseExceptionObject *)value)->suppress_context) {
832-
res = PySet_Contains(seen, context);
840+
check_id = PyLong_FromVoidPtr(context);
841+
if (check_id == NULL) {
842+
res = -1;
843+
} else {
844+
res = PySet_Contains(seen, check_id);
845+
Py_DECREF(check_id);
846+
}
833847
if (res == -1)
834848
PyErr_Clear();
835849
if (res == 0) {
@@ -842,6 +856,7 @@ print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
842856
Py_XDECREF(context);
843857
Py_XDECREF(cause);
844858
}
859+
Py_XDECREF(value_id);
845860
}
846861
print_exception(f, value);
847862
if (err != 0)

0 commit comments

Comments
 (0)