mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
bpo-44297: Add a regression test for line numbers in with statements (GH-26891)
This commit is contained in:
parent
0a3452e7cf
commit
0b6b286518
1 changed files with 24 additions and 7 deletions
|
|
@ -2160,18 +2160,23 @@ def test_incorrect_constructor(self):
|
||||||
|
|
||||||
class PEP626Tests(unittest.TestCase):
|
class PEP626Tests(unittest.TestCase):
|
||||||
|
|
||||||
def lineno_after_raise(self, f, line):
|
def lineno_after_raise(self, f, *expected):
|
||||||
try:
|
try:
|
||||||
f()
|
f()
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
t = ex.__traceback__
|
t = ex.__traceback__
|
||||||
while t.tb_next:
|
else:
|
||||||
t = t.tb_next
|
self.fail("No exception raised")
|
||||||
|
lines = []
|
||||||
|
t = t.tb_next # Skip this function
|
||||||
|
while t:
|
||||||
frame = t.tb_frame
|
frame = t.tb_frame
|
||||||
if line is None:
|
lines.append(
|
||||||
self.assertEqual(frame.f_lineno, line)
|
None if frame.f_lineno is None else
|
||||||
else:
|
frame.f_lineno-frame.f_code.co_firstlineno
|
||||||
self.assertEqual(frame.f_lineno-frame.f_code.co_firstlineno, line)
|
)
|
||||||
|
t = t.tb_next
|
||||||
|
self.assertEqual(tuple(lines), expected)
|
||||||
|
|
||||||
def test_lineno_after_raise_simple(self):
|
def test_lineno_after_raise_simple(self):
|
||||||
def simple():
|
def simple():
|
||||||
|
|
@ -2250,5 +2255,17 @@ def f():
|
||||||
f.__code__ = f.__code__.replace(co_linetable=b'\x04\x80\xff\x80')
|
f.__code__ = f.__code__.replace(co_linetable=b'\x04\x80\xff\x80')
|
||||||
self.lineno_after_raise(f, None)
|
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__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue