gh-123142: Fix too wide source locations in tracebacks of exceptions from broken iterables in comprehensions (#123173)

This commit is contained in:
Irit Katriel 2024-08-21 19:12:05 +01:00 committed by GitHub
parent a4fd7aa4a6
commit ec89620e5e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 122 additions and 22 deletions

View file

@ -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}