Skip to content

Commit d82e08d

Browse files
authored
[3.8] bpo-42474: test TracebackException comparison to non-equal instances (GH-23558)
1 parent b1c48e5 commit d82e08d

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

Lib/test/test_traceback.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,56 @@ def test_context(self):
10831083
self.assertEqual(exc_info[0], exc.exc_type)
10841084
self.assertEqual(str(exc_info[1]), str(exc))
10851085

1086+
def test_comparison_basic(self):
1087+
try:
1088+
1/0
1089+
except Exception:
1090+
exc_info = sys.exc_info()
1091+
exc = traceback.TracebackException(*exc_info)
1092+
exc2 = traceback.TracebackException(*exc_info)
1093+
self.assertIsNot(exc, exc2)
1094+
self.assertEqual(exc, exc2)
1095+
class MyObject:
1096+
pass
1097+
self.assertNotEqual(exc, MyObject())
1098+
1099+
def test_comparison_params_variations(self):
1100+
def raise_exc():
1101+
try:
1102+
raise ValueError('bad value')
1103+
except:
1104+
raise
1105+
1106+
def raise_with_locals():
1107+
x, y = 1, 2
1108+
raise_exc()
1109+
1110+
try:
1111+
raise_with_locals()
1112+
except Exception:
1113+
exc_info = sys.exc_info()
1114+
1115+
exc = traceback.TracebackException(*exc_info)
1116+
exc1 = traceback.TracebackException(*exc_info, limit=10)
1117+
exc2 = traceback.TracebackException(*exc_info, limit=2)
1118+
1119+
self.assertEqual(exc, exc1) # limit=10 gets all frames
1120+
self.assertNotEqual(exc, exc2) # limit=2 truncates the output
1121+
1122+
# locals change the output
1123+
exc3 = traceback.TracebackException(*exc_info, capture_locals=True)
1124+
self.assertNotEqual(exc, exc3)
1125+
1126+
# there are no locals in the innermost frame
1127+
exc4 = traceback.TracebackException(*exc_info, limit=-1)
1128+
exc5 = traceback.TracebackException(*exc_info, limit=-1, capture_locals=True)
1129+
self.assertEqual(exc4, exc5)
1130+
1131+
# there are locals in the next-to-innermost frame
1132+
exc6 = traceback.TracebackException(*exc_info, limit=-2)
1133+
exc7 = traceback.TracebackException(*exc_info, limit=-2, capture_locals=True)
1134+
self.assertNotEqual(exc6, exc7)
1135+
10861136
def test_unhashable(self):
10871137
class UnhashableException(Exception):
10881138
def __eq__(self, other):
@@ -1124,7 +1174,7 @@ def test_lookup_lines(self):
11241174
f = test_frame(c, None, None)
11251175
tb = test_tb(f, 6, None)
11261176
exc = traceback.TracebackException(Exception, e, tb, lookup_lines=False)
1127-
self.assertEqual({}, linecache.cache)
1177+
self.assertEqual(linecache.cache, {})
11281178
linecache.updatecache('/foo.py', globals())
11291179
self.assertEqual(exc.stack[0].line, "import sys")
11301180

0 commit comments

Comments
 (0)