[Python-checkins] bpo-44297: Add a regression test for line numbers in with statements (GH-26891)

pablogsal webhook-mailer at python.org
Thu Jun 24 08:09:22 EDT 2021


https://github.com/python/cpython/commit/0b6b2865187bca7ed7f1f511a02fc8bd13ee38ca
commit: 0b6b2865187bca7ed7f1f511a02fc8bd13ee38ca
branch: 3.10
author: Mark Shannon <mark at hotpy.org>
committer: pablogsal <Pablogsal at gmail.com>
date: 2021-06-24T13:09:14+01:00
summary:

bpo-44297: Add a regression test for line numbers in with statements (GH-26891)

files:
M Lib/test/test_exceptions.py

diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index d444a122af9be..8f689546a6229 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -2160,18 +2160,23 @@ def test_incorrect_constructor(self):
 
 class PEP626Tests(unittest.TestCase):
 
-    def lineno_after_raise(self, f, line):
+    def lineno_after_raise(self, f, *expected):
         try:
             f()
         except Exception as ex:
             t = ex.__traceback__
-            while t.tb_next:
-                t = t.tb_next
+        else:
+            self.fail("No exception raised")
+        lines = []
+        t = t.tb_next # Skip this function
+        while t:
             frame = t.tb_frame
-            if line is None:
-                self.assertEqual(frame.f_lineno, line)
-            else:
-                self.assertEqual(frame.f_lineno-frame.f_code.co_firstlineno, line)
+            lines.append(
+                None if frame.f_lineno is None else
+                frame.f_lineno-frame.f_code.co_firstlineno
+            )
+            t = t.tb_next
+        self.assertEqual(tuple(lines), expected)
 
     def test_lineno_after_raise_simple(self):
         def simple():
@@ -2250,5 +2255,17 @@ def f():
         f.__code__ = f.__code__.replace(co_linetable=b'\x04\x80\xff\x80')
         self.lineno_after_raise(f, None)
 
+    def test_lineno_after_raise_in_with_exit(self):
+        class ExitFails:
+            def __enter__(self):
+                return self
+            def __exit__(self, *args):
+                raise ValueError
+
+        def after_with():
+            with ExitFails():
+                1/0
+        self.lineno_after_raise(after_with, 1, 1)
+
 if __name__ == '__main__':
     unittest.main()



More information about the Python-checkins mailing list