Skip to content

[3.13] GH-135171: Fix generator expressions one last time (hopefully) (GH-135225) #135293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 3.13
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ def bug1333982(x=[]):
LOAD_CONST 1 (<code object <genexpr> at 0x..., file "%s", line %d>)
MAKE_FUNCTION
LOAD_FAST 0 (x)
GET_ITER
CALL 0

%3d LOAD_CONST 2 (1)
Expand Down Expand Up @@ -764,6 +765,7 @@ def foo(x):
MAKE_FUNCTION
SET_FUNCTION_ATTRIBUTE 8 (closure)
LOAD_DEREF 1 (y)
GET_ITER
CALL 0
CALL 1
RETURN_VALUE
Expand All @@ -784,7 +786,6 @@ def foo(x):
POP_TOP
L1: RESUME 0
LOAD_FAST 0 (.0)
GET_ITER
L2: FOR_ITER 10 (to L3)
STORE_FAST 1 (z)
LOAD_DEREF 2 (x)
Expand Down
21 changes: 13 additions & 8 deletions Lib/test/test_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,21 +296,26 @@ def gen(it):
yield x
return gen(range(10))

def process_tests(self, get_generator):
def process_tests(self, get_generator, is_expr):
err_iterator = "'.*' object is not an iterator"
err_iterable = "'.*' object is not iterable"
for obj in self.iterables:
g_obj = get_generator(obj)
with self.subTest(g_obj=g_obj, obj=obj):
self.assertListEqual(list(g_obj), list(obj))
if is_expr:
self.assertRaisesRegex(TypeError, err_iterator, list, g_obj)
else:
self.assertListEqual(list(g_obj), list(obj))

g_iter = get_generator(iter(obj))
with self.subTest(g_iter=g_iter, obj=obj):
self.assertListEqual(list(g_iter), list(obj))

err_regex = "'.*' object is not iterable"
for obj in self.non_iterables:
g_obj = get_generator(obj)
with self.subTest(g_obj=g_obj):
self.assertRaisesRegex(TypeError, err_regex, list, g_obj)
err = err_iterator if is_expr else err_iterable
self.assertRaisesRegex(TypeError, err, list, g_obj)

def test_modify_f_locals(self):
def modify_f_locals(g, local, obj):
Expand All @@ -323,8 +328,8 @@ def get_generator_genexpr(obj):
def get_generator_genfunc(obj):
return modify_f_locals(self.genfunc(), 'it', obj)

self.process_tests(get_generator_genexpr)
self.process_tests(get_generator_genfunc)
self.process_tests(get_generator_genexpr, True)
self.process_tests(get_generator_genfunc, False)

def test_new_gen_from_gi_code(self):
def new_gen_from_gi_code(g, obj):
Expand All @@ -337,8 +342,8 @@ def get_generator_genexpr(obj):
def get_generator_genfunc(obj):
return new_gen_from_gi_code(self.genfunc(), obj)

self.process_tests(get_generator_genexpr)
self.process_tests(get_generator_genfunc)
self.process_tests(get_generator_genexpr, True)
self.process_tests(get_generator_genfunc, False)


class ExceptionTest(unittest.TestCase):
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_genexps.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@
>>> list(g)
[1, 9, 25, 49, 81]

Verify that the outermost for-expression makes an immediate check
for iterability
>>> (i for i in 6)
Traceback (most recent call last):
File "<pyshell#4>", line 1, in -toplevel-
(i for i in 6)
TypeError: 'int' object is not iterable

Verify late binding for the innermost for-expression

>>> g = ((i,j) for i in range(3) for j in range(x))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Reverted change made in 3.13.4 where creating a generator from an object
that was not an iterable did not immediately raise an exception. Code like
this::

def is_iterable(x):
try:
(_ for _ in x)
return True
except TypeError
return False

now works again.
27 changes: 24 additions & 3 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2599,7 +2599,14 @@ dummy_func(

replaced op(_FOR_ITER, (iter -- iter, next)) {
/* before: [iter]; after: [iter, iter()] *or* [] (and jump over END_FOR.) */
next = (*Py_TYPE(iter)->tp_iternext)(iter);
iternextfunc func = Py_TYPE(iter)->tp_iternext;
if (func == NULL) {
_PyErr_Format(tstate, PyExc_TypeError,
"'%.100s' object is not an iterator",
Py_TYPE(iter)->tp_name);
ERROR_NO_POP();
}
next = func(iter);
if (next == NULL) {
if (_PyErr_Occurred(tstate)) {
if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Expand All @@ -2622,7 +2629,14 @@ dummy_func(

op(_FOR_ITER_TIER_TWO, (iter -- iter, next)) {
/* before: [iter]; after: [iter, iter()] *or* [] (and jump over END_FOR.) */
next = (*Py_TYPE(iter)->tp_iternext)(iter);
iternextfunc func = Py_TYPE(iter)->tp_iternext;
if (func == NULL) {
_PyErr_Format(tstate, PyExc_TypeError,
"'%.100s' object is not an iterator",
Py_TYPE(iter)->tp_name);
ERROR_NO_POP();
}
next = func(iter);
if (next == NULL) {
if (_PyErr_Occurred(tstate)) {
if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Expand All @@ -2643,7 +2657,14 @@ dummy_func(
inst(INSTRUMENTED_FOR_ITER, (unused/1 -- )) {
_Py_CODEUNIT *target;
PyObject *iter = TOP();
PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
iternextfunc func = Py_TYPE(iter)->tp_iternext;
if (func == NULL) {
_PyErr_Format(tstate, PyExc_TypeError,
"'%.100s' object is not an iterator",
Py_TYPE(iter)->tp_name);
ERROR_NO_POP();
}
PyObject *next = func(iter);
if (next != NULL) {
PUSH(next);
target = next_instr;
Expand Down
11 changes: 8 additions & 3 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -5368,7 +5368,7 @@ compiler_sync_comprehension_generator(struct compiler *c, location loc,

comprehension_ty gen = (comprehension_ty)asdl_seq_GET(generators,
gen_index);

int is_outer_genexpr = gen_index == 0 && type == COMP_GENEXP;
if (!iter_on_stack) {
if (gen_index == 0) {
/* Receive outermost iter as an implicit argument */
Expand Down Expand Up @@ -5407,7 +5407,9 @@ compiler_sync_comprehension_generator(struct compiler *c, location loc,

if (IS_LABEL(start)) {
depth++;
ADDOP(c, LOC(gen->iter), GET_ITER);
if (!is_outer_genexpr) {
ADDOP(c, LOC(gen->iter), GET_ITER);
}
USE_LABEL(c, start);
ADDOP_JUMP(c, LOC(gen->iter), FOR_ITER, anchor);
}
Expand Down Expand Up @@ -5810,6 +5812,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
location loc = LOC(e);

outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
int is_sync_genexpr = type == COMP_GENEXP && !outermost->is_async;
if (is_inlined) {
if (compiler_visit_expr(c, outermost->iter) < 0) {
goto error;
Expand Down Expand Up @@ -5898,7 +5901,9 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
Py_CLEAR(co);

VISIT(c, expr, outermost->iter);

if (is_sync_genexpr) {
ADDOP(c, loc, GET_ITER);
}
ADDOP_I(c, loc, CALL, 0);

if (is_async_generator && type != COMP_GENEXP) {
Expand Down
9 changes: 8 additions & 1 deletion Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading