summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_exceptions.py
diff options
context:
space:
mode:
authorMark Shannon <mark@hotpy.org>2020-12-17 13:55:28 (GMT)
committerGitHub <noreply@github.com>2020-12-17 13:55:28 (GMT)
commitbf353f3c2d937772a8cf30b15fd8eb7b82665ccb (patch)
tree9196732769c1cca2bd01a44e668fe4c5fb29f7d7 /Lib/test/test_exceptions.py
parent40125ab3252453bf205ed906e46bf9741c27bf9d (diff)
downloadcpython-bf353f3c2d937772a8cf30b15fd8eb7b82665ccb.zip
cpython-bf353f3c2d937772a8cf30b15fd8eb7b82665ccb.tar.gz
cpython-bf353f3c2d937772a8cf30b15fd8eb7b82665ccb.tar.bz2
bpo-42246: Make sure that `f_lasti`, and thus `f_lineno`, is set correctly after raising or reraising an exception (GH-23803)
* Ensure that f_lasti is set correctly after an exception is raised to conform to PEP 626. * Update importlib * Add NEWS.
Diffstat (limited to 'Lib/test/test_exceptions.py')
-rw-r--r--Lib/test/test_exceptions.py83
1 files changed, 83 insertions, 0 deletions
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index e752ab7..8644223 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -1488,5 +1488,88 @@ class ImportErrorTests(unittest.TestCase):
self.assertEqual(exc.path, orig.path)
+class PEP626Tests(unittest.TestCase):
+
+ def lineno_after_raise(self, f, line):
+ try:
+ f()
+ except Exception as ex:
+ t = ex.__traceback__
+ while t.tb_next:
+ t = t.tb_next
+ frame = t.tb_frame
+ self.assertEqual(frame.f_lineno-frame.f_code.co_firstlineno, line)
+
+ def test_lineno_after_raise_simple(self):
+ def simple():
+ 1/0
+ pass
+ self.lineno_after_raise(simple, 1)
+
+ def test_lineno_after_raise_in_except(self):
+ def in_except():
+ try:
+ 1/0
+ except:
+ 1/0
+ pass
+ self.lineno_after_raise(in_except, 4)
+
+ def test_lineno_after_other_except(self):
+ def other_except():
+ try:
+ 1/0
+ except TypeError as ex:
+ pass
+ self.lineno_after_raise(other_except, 3)
+
+ def test_lineno_in_named_except(self):
+ def in_named_except():
+ try:
+ 1/0
+ except Exception as ex:
+ 1/0
+ pass
+ self.lineno_after_raise(in_named_except, 4)
+
+ def test_lineno_in_try(self):
+ def in_try():
+ try:
+ 1/0
+ finally:
+ pass
+ self.lineno_after_raise(in_try, 4)
+
+ def test_lineno_in_finally_normal(self):
+ def in_finally_normal():
+ try:
+ pass
+ finally:
+ 1/0
+ pass
+ self.lineno_after_raise(in_finally_normal, 4)
+
+ def test_lineno_in_finally_except(self):
+ def in_finally_except():
+ try:
+ 1/0
+ finally:
+ 1/0
+ pass
+ self.lineno_after_raise(in_finally_except, 4)
+
+ def test_lineno_after_with(self):
+ class Noop:
+ def __enter__(self):
+ return self
+ def __exit__(self, *args):
+ pass
+ def after_with():
+ with Noop():
+ 1/0
+ pass
+ self.lineno_after_raise(after_with, 2)
+
+
if __name__ == '__main__':
unittest.main()