LOAD_CONST 1 (<code object <genexpr> at 0x..., file "%s", line %d>)
MAKE_FUNCTION
LOAD_FAST_BORROW 0 (x)
- GET_ITER
CALL 0
%3d LOAD_SMALL_INT 1
MAKE_FUNCTION
SET_FUNCTION_ATTRIBUTE 8 (closure)
LOAD_DEREF 1 (y)
- GET_ITER
CALL 0
CALL 1
RETURN_VALUE
#This should not raise
loop()
+ def test_genexpr_only_calls_dunder_iter_once(self):
+
+ class Iterator:
+
+ def __init__(self):
+ self.val = 0
+
+ def __next__(self):
+ if self.val == 2:
+ raise StopIteration
+ self.val += 1
+ return self.val
+
+ # No __iter__ method
+
+ class C:
+
+ def __iter__(self):
+ return Iterator()
+
+ self.assertEqual([1,2], list(i for i in C()))
+
class ModifyUnderlyingIterableTest(unittest.TestCase):
iterables = [
>>> list(g)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 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 outermost if-expression
>>> include = (2,4,6,8)
--- /dev/null
+No longer call ``__iter__`` twice when creating and executing a generator expression.
+Creating a generator expression from a non-interable will raise only when the
+generator expression is executed.
+This brings the behavior of generator expressions in line with other generators.
}
Py_CLEAR(co);
- if (codegen_comprehension_iter(c, outermost)) {
- goto error;
- }
-
+ VISIT(c, expr, outermost->iter);
ADDOP_I(c, loc, CALL, 0);
if (is_async_comprehension && type != COMP_GENEXP) {