mirror of
https://github.com/python/cpython.git
synced 2025-08-11 12:29:34 +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,8 +1,11 @@
|
|||
import doctest
|
||||
import textwrap
|
||||
import traceback
|
||||
import types
|
||||
import unittest
|
||||
|
||||
from test.support import BrokenIter
|
||||
|
||||
|
||||
doctests = """
|
||||
########### Tests borrowed from or inspired by test_genexps.py ############
|
||||
|
@ -706,6 +709,35 @@ class ListComprehensionTest(unittest.TestCase):
|
|||
self._check_in_scopes(code, {"x": 2, "y": [3]}, ns={"x": 3}, scopes=["class"])
|
||||
self._check_in_scopes(code, {"x": 2, "y": [2]}, ns={"x": 3}, scopes=["function", "module"])
|
||||
|
||||
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 for x in BrokenIter(init_raises=True)]
|
||||
except Exception as e:
|
||||
return e
|
||||
|
||||
def next_raises():
|
||||
try:
|
||||
[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)
|
||||
|
||||
__test__ = {'doctests' : doctests}
|
||||
|
||||
def load_tests(loader, tests, pattern):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue