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.
This commit is contained in:
Mark Shannon 2020-12-17 13:55:28 +00:00 committed by GitHub
parent 40125ab325
commit bf353f3c2d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 317 additions and 225 deletions

View file

@ -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()