mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
gh-123142: Fix too wide source locations in tracebacks of exceptions from broken iterables in comprehensions (#123173)
This commit is contained in:
parent
a4fd7aa4a6
commit
ec89620e5e
8 changed files with 122 additions and 22 deletions
|
@ -1,6 +1,9 @@
|
|||
import doctest
|
||||
import traceback
|
||||
import unittest
|
||||
|
||||
from test.support import BrokenIter
|
||||
|
||||
|
||||
doctests = """
|
||||
########### Tests mostly copied from test_listcomps.py ############
|
||||
|
@ -148,6 +151,35 @@ We also repeat each of the above scoping tests inside a function
|
|||
|
||||
"""
|
||||
|
||||
class SetComprehensionTest(unittest.TestCase):
|
||||
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}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue