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

@ -5,6 +5,7 @@ import unittest
from test.support import cpython_only
from test.support.os_helper import TESTFN, unlink
from test.support import check_free_after_iterating, ALWAYS_EQ, NEVER_EQ
from test.support import BrokenIter
import pickle
import collections.abc
import functools
@ -1148,35 +1149,22 @@ class TestCase(unittest.TestCase):
# The location of an exception raised from __init__ or
# __next__ should should be the iterator expression
class Iter:
def __init__(self, init_raises=False, next_raises=False):
if init_raises:
1/0
self.next_raises = next_raises
def __next__(self):
if self.next_raises:
1/0
def __iter__(self):
return self
def init_raises():
try:
for x in Iter(init_raises=True):
for x in BrokenIter(init_raises=True):
pass
except Exception as e:
return e
def next_raises():
try:
for x in Iter(next_raises=True):
for x in BrokenIter(next_raises=True):
pass
except Exception as e:
return e
for func, expected in [(init_raises, "Iter(init_raises=True)"),
(next_raises, "Iter(next_raises=True)"),
for func, expected in [(init_raises, "BrokenIter(init_raises=True)"),
(next_raises, "BrokenIter(next_raises=True)"),
]:
with self.subTest(func):
exc = func()