Skip to content

Commit 36f538c

Browse files
authored
bpo-46458: Add tests for context of exception in finally block (GH-30986)
1 parent ffa505b commit 36f538c

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

Lib/test/test_exceptions.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,56 @@ class E(Exception):
11631163
self.assertIs(b.__context__, a)
11641164
self.assertIs(a.__context__, c)
11651165

1166+
def test_context_of_exception_in_try_and_finally(self):
1167+
try:
1168+
try:
1169+
te = TypeError(1)
1170+
raise te
1171+
finally:
1172+
ve = ValueError(2)
1173+
raise ve
1174+
except Exception as e:
1175+
exc = e
1176+
1177+
self.assertIs(exc, ve)
1178+
self.assertIs(exc.__context__, te)
1179+
1180+
def test_context_of_exception_in_except_and_finally(self):
1181+
try:
1182+
try:
1183+
te = TypeError(1)
1184+
raise te
1185+
except:
1186+
ve = ValueError(2)
1187+
raise ve
1188+
finally:
1189+
oe = OSError(3)
1190+
raise oe
1191+
except Exception as e:
1192+
exc = e
1193+
1194+
self.assertIs(exc, oe)
1195+
self.assertIs(exc.__context__, ve)
1196+
self.assertIs(exc.__context__.__context__, te)
1197+
1198+
def test_context_of_exception_in_else_and_finally(self):
1199+
try:
1200+
try:
1201+
pass
1202+
except:
1203+
pass
1204+
else:
1205+
ve = ValueError(1)
1206+
raise ve
1207+
finally:
1208+
oe = OSError(2)
1209+
raise oe
1210+
except Exception as e:
1211+
exc = e
1212+
1213+
self.assertIs(exc, oe)
1214+
self.assertIs(exc.__context__, ve)
1215+
11661216
def test_unicode_change_attributes(self):
11671217
# See issue 7309. This was a crasher.
11681218

0 commit comments

Comments
 (0)