Skip to content

Commit 6d1b93e

Browse files
committed
GET_ITER may leave the iterable on the stack
1 parent 69a328d commit 6d1b93e

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

Lib/test/test_list.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,5 +365,19 @@ def lappend(l, x, y):
365365
rc, _, _ = assert_python_ok("-c", code)
366366
self.assertEqual(rc, 0)
367367

368+
def test_list_overwrite_local(self):
369+
"""Test that overwriting the last reference to the
370+
iterable doesn't prematurely free the iterable"""
371+
372+
def foo(x):
373+
r = 0
374+
for i in x:
375+
r += i
376+
x = None
377+
return r
378+
379+
self.assertEqual(foo(list(range(10))), 45)
380+
381+
368382
if __name__ == "__main__":
369383
unittest.main()

Python/flowgraph.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2871,6 +2871,7 @@ optimize_load_fast(cfg_builder *g)
28712871

28722872
// Opcodes that consume no inputs
28732873
case GET_ANEXT:
2874+
case GET_ITER:
28742875
case GET_LEN:
28752876
case IMPORT_FROM:
28762877
case MATCH_KEYS:

0 commit comments

Comments
 (0)