summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2022-01-28 20:41:58 (GMT)
committerGitHub <noreply@github.com>2022-01-28 20:41:58 (GMT)
commit36f538c8092eeb3d5b8bc9df0ae7cc348f08a865 (patch)
treef22ae77d079d5d82e4b4eae15588f6798be4742b /Lib
parentffa505b580464d9d90c29e69bd4db8c52275280a (diff)
downloadcpython-36f538c8092eeb3d5b8bc9df0ae7cc348f08a865.zip
cpython-36f538c8092eeb3d5b8bc9df0ae7cc348f08a865.tar.gz
cpython-36f538c8092eeb3d5b8bc9df0ae7cc348f08a865.tar.bz2
bpo-46458: Add tests for context of exception in finally block (GH-30986)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_exceptions.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index 5932b9d..898b5cc 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -1163,6 +1163,56 @@ class ExceptionTests(unittest.TestCase):
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.