Skip to content

Python 3.13.3 runs __iter__ twice in a list comprehension #132711

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

Closed
fb2078 opened this issue Apr 18, 2025 · 3 comments
Closed

Python 3.13.3 runs __iter__ twice in a list comprehension #132711

fb2078 opened this issue Apr 18, 2025 · 3 comments
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) pending The issue will be closed if no feedback is provided type-bug An unexpected behavior, bug, or error

Comments

@fb2078
Copy link

fb2078 commented Apr 18, 2025

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

@fb2078 fb2078 added the type-bug An unexpected behavior, bug, or error label Apr 18, 2025
@picnixz picnixz added interpreter-core (Objects, Python, Grammar, and Parser dirs) 3.13 bugs and security fixes 3.14 new features, bugs and security fixes labels Apr 18, 2025
@picnixz

This comment has been minimized.

@picnixz
Copy link
Member

picnixz commented Apr 18, 2025

Ah, could it be a duplicate of #127682?

@picnixz picnixz added the pending The issue will be closed if no feedback is provided label Apr 18, 2025
@efimov-mikhail
Copy link
Contributor

Ah, could it be a duplicate of #127682?

I think so, this is a duplicate.

@picnixz picnixz closed this as completed Apr 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) pending The issue will be closed if no feedback is provided type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants