[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:
Irit Katriel 2024-08-22 10:22:43 +01:00 committed by GitHub
parent 8edfa0b0b4
commit fbbde4dc6a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 122 additions and 21 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()