mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
[3.12] gh-123142: Fix too wide source locations in tracebacks of exceptions from broken iterables in comprehensions (GH-123173). (#123210)
(cherry picked from commit ec89620e5e
)
This commit is contained in:
parent
8edfa0b0b4
commit
fbbde4dc6a
8 changed files with 122 additions and 21 deletions
|
@ -1,5 +1,8 @@
|
|||
import traceback
|
||||
import unittest
|
||||
|
||||
from test.support import BrokenIter
|
||||
|
||||
# For scope testing.
|
||||
g = "Global variable"
|
||||
|
||||
|
@ -127,6 +130,34 @@ class DictComprehensionTest(unittest.TestCase):
|
|||
self.assertEqual({i: i*i for i in [*range(4)]}, expected)
|
||||
self.assertEqual({i: i*i for i in (*range(4),)}, expected)
|
||||
|
||||
def test_exception_locations(self):
|
||||
# The location of an exception raised from __init__ or
|
||||
# __next__ should should be the iterator expression
|
||||
def init_raises():
|
||||
try:
|
||||
{x:x for x in BrokenIter(init_raises=True)}
|
||||
except Exception as e:
|
||||
return e
|
||||
|
||||
def next_raises():
|
||||
try:
|
||||
{x:x for x in BrokenIter(next_raises=True)}
|
||||
except Exception as e:
|
||||
return e
|
||||
|
||||
for func, expected in [(init_raises, "BrokenIter(init_raises=True)"),
|
||||
(next_raises, "BrokenIter(next_raises=True)"),
|
||||
]:
|
||||
with self.subTest(func):
|
||||
exc = func()
|
||||
f = traceback.extract_tb(exc.__traceback__)[0]
|
||||
indent = 16
|
||||
co = func.__code__
|
||||
self.assertEqual(f.lineno, co.co_firstlineno + 2)
|
||||
self.assertEqual(f.end_lineno, co.co_firstlineno + 2)
|
||||
self.assertEqual(f.line[f.colno - indent : f.end_colno - indent],
|
||||
expected)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue