Skip to content

bpo-46563: Add tests for context of exception in finally block #30986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,56 @@ class E(Exception):
self.assertIs(b.__context__, a)
self.assertIs(a.__context__, c)

def test_context_of_exception_in_try_and_finally(self):
try:
try:
te = TypeError(1)
raise te
finally:
ve = ValueError(2)
raise ve
except Exception as e:
exc = e

self.assertIs(exc, ve)
self.assertIs(exc.__context__, te)

def test_context_of_exception_in_except_and_finally(self):
try:
try:
te = TypeError(1)
raise te
except:
ve = ValueError(2)
raise ve
finally:
oe = OSError(3)
raise oe
except Exception as e:
exc = e

self.assertIs(exc, oe)
self.assertIs(exc.__context__, ve)
self.assertIs(exc.__context__.__context__, te)

def test_context_of_exception_in_else_and_finally(self):
try:
try:
pass
except:
pass
else:
ve = ValueError(1)
raise ve
finally:
oe = OSError(2)
raise oe
except Exception as e:
exc = e

self.assertIs(exc, oe)
self.assertIs(exc.__context__, ve)

def test_unicode_change_attributes(self):
# See issue 7309. This was a crasher.

Expand Down