| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -184,7 +184,6 @@ def bug1333982(x=[]): | |||
| 184 | 184 | LOAD_CONST 1 (<code object <genexpr> at 0x..., file "%s", line %d>) | |
| 185 | 185 | MAKE_FUNCTION | |
| 186 | 186 | LOAD_FAST 0 (x) | |
| 187 | - GET_ITER | ||
| 188 | 187 | CALL 0 | |
| 189 | 188 | ||
| 190 | 189 | %3d LOAD_CONST 2 (1) | |
@@ -765,7 +764,6 @@ def foo(x): | |||
| 765 | 764 | MAKE_FUNCTION | |
| 766 | 765 | SET_FUNCTION_ATTRIBUTE 8 (closure) | |
| 767 | 766 | LOAD_DEREF 1 (y) | |
| 768 | - GET_ITER | ||
| 769 | 767 | CALL 0 | |
| 770 | 768 | CALL 1 | |
| 771 | 769 | RETURN_VALUE | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -246,6 +246,28 @@ def loop(): | |||
| 246 | 246 | #This should not raise | |
| 247 | 247 | loop() | |
| 248 | 248 | ||
| 249 | + def test_genexpr_only_calls_dunder_iter_once(self): | ||
| 250 | + | ||
| 251 | + class Iterator: | ||
| 252 | + | ||
| 253 | + def __init__(self): | ||
| 254 | + self.val = 0 | ||
| 255 | + | ||
| 256 | + def __next__(self): | ||
| 257 | + if self.val == 2: | ||
| 258 | + raise StopIteration | ||
| 259 | + self.val += 1 | ||
| 260 | + return self.val | ||
| 261 | + | ||
| 262 | + # No __iter__ method | ||
| 263 | + | ||
| 264 | + class C: | ||
| 265 | + | ||
| 266 | + def __iter__(self): | ||
| 267 | + return Iterator() | ||
| 268 | + | ||
| 269 | + self.assertEqual([1,2], list(i for i in C())) | ||
| 270 | + | ||
| 249 | 271 | ||
| 250 | 272 | class ModifyUnderlyingIterableTest(unittest.TestCase): | |
| 251 | 273 | iterables = [ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -123,15 +123,6 @@ | |||
| 123 | 123 | >>> list(g) | |
| 124 | 124 | [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] | |
| 125 | 125 | ||
| 126 | - Verify that the outermost for-expression makes an immediate check | ||
| 127 | - for iterability | ||
| 128 | - | ||
| 129 | - >>> (i for i in 6) | ||
| 130 | - Traceback (most recent call last): | ||
| 131 | - File "<pyshell#4>", line 1, in -toplevel- | ||
| 132 | - (i for i in 6) | ||
| 133 | - TypeError: 'int' object is not iterable | ||
| 134 | - | ||
| 135 | 126 | Verify late binding for the outermost if-expression | |
| 136 | 127 | ||
| 137 | 128 | >>> include = (2,4,6,8) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,4 @@ | |||
| 1 | + No longer call ``__iter__`` twice when creating and executing a generator expression. | ||
| 2 | + Creating a generator expression from a non-interable will raise only when the | ||
| 3 | + generator expression is executed. | ||
| 4 | + This brings the behavior of generator expressions in line with other generators. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5910,9 +5910,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type, | |||
| 5910 | 5910 | } | |
| 5911 | 5911 | Py_CLEAR(co); | |
| 5912 | 5912 | ||
| 5913 | - if (compiler_comprehension_iter(c, outermost)) { | ||
| 5914 | - goto error; | ||
| 5915 | - } | ||
| 5913 | + VISIT(c, expr, outermost->iter); | ||
| 5916 | 5914 | ||
| 5917 | 5915 | ADDOP_I(c, loc, CALL, 0); | |
| 5918 | 5916 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments