Repository navigation
Python 3.13.3 runs __iter__ twice in a list comprehension #132711
Copy link
Copy link
Closed as duplicate
Labels
3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)pendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is providedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
I can't upload comprehensionTestbed2.py because I am told the .py type is not allowed so I here is the script I used to reproduce this issue:
===========================================
# TestClass: Class that interacts with the DVD data file.
class TestClass:
# Initialize the file timestamp.
def __init__(self):
pass
# The iterator method.
def __iter__(self):
print('S1')
self._index = 0
return self
# The next method.
def __next__(self):
print('S5')
if self._index == 5:
raise StopIteration
value = self._index
self._index += 1
return value
# Test code.
print('#1')
for n in TestClass():
print(n)
print('#2')
numbers = [n for n in TestClass()]
print(numbers)==============================================
Here is the output of the Python script above:
D:\Files\Testing> comprehensionTestbed2.py
#1
S1
S5
0
S5
1
S5
2
S5
3
S5
4
S5
#2
S1
S1 <----- The __iter__ method is invoked twice in the list comprehension when using 3.13.3 (and 3.13.2).
S5
S5
S5
S5
S5
S5
[0, 1, 2, 3, 4]
This does not happen with python 3.12.9 and is a real problem when the iter method creates a mutex.
Edward
CPython versions tested on:
3.13
Operating systems tested on:
Windows
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)pendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is providedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Activity
picnixz commentedon Apr 18, 2025
picnixz commentedon Apr 18, 2025
Ah, could it be a duplicate of #127682?
efimov-mikhail commentedon Apr 19, 2025
I think so, this is a duplicate.
fb2078 commentedon Jun 19, 2025
efimov-mikhail commentedon Jun 19, 2025
Unfortunately, we can't fix this bug without producing other bigger problems. So, fix in 3.13.4 was reverted.
You can read this topic for more information: https://discuss.python.org/t/python-3-13-5-is-now-available-yes-really/95211
fb2078 commentedon Jun 19, 2025
efimov-mikhail commentedon Jun 20, 2025
Only in comprehensions.
JFR, the most correct way for writing related code is to always define
__iter__method for iterator objects this way:fb2078 commentedon Jun 21, 2025
fb2078 commentedon Jun 21, 2025
efimov-mikhail commentedon Jun 21, 2025
Yes,
__iter__method should be idempotent without side effects, so calling it twice gives the same result as calling it one time.It's already fixed on 3.14 branch, you can test on fresh beta: https://discuss.python.org/t/python-3-14-0-beta-3-is-here/95843