Repository navigation
Regression of 3.13.1 with iterator creation being duplicated #127682
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Dec 6, 2024 This is an expected result from the change @brianschubert linked, which fixes a crash. Our belief was that
__iter__is supposed to return self on iterators, so it's safe to call it multiple times.It does return
selfthough in my concrete code here. My goal was to see what generator expressions do when to be 100% compatible.def __iter__(self): print("Giving iterator now", self.x) return selfBut nobody ever promised to not have a side effect in there, and calling it twice is strange, which one of the two gets used for what there? Right now I cannot distinguish the two.
As for real-life code relevance, I am more than willing to admit this is garbage code. I just did a bunch of prints in iterator functions to see when they get called, and now they're getting called more often. Would it have to be a performance regression to make two calls?
- addedinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
on Dec 6, 2024 Actually, there is a PR for main branch related to this issue:
#126408
It it will be accepted, then multiple calls of__iter__be removed.But on 3.13 current behavior is not a bug.
The problem is specific to generator expressions which have acted in a subtly different way from generators that (almost) no one noticed.
For a generator, any iteration occurs once the generator is executed. For generator expressions creating an iterator from the iterable happened when the generator expression is created. However there was no check that the iteration variable held an iterator when the generator expression executed, which could lead to a crash in exceptional circumstances.
This can be seen by disassembling this function:
def f(seq): return (x for x in seq)
Up to 3.13
2 LOAD_CONST 0 (<code object <genexpr> at 0x7f1ad8c25be0, file "<python-input-9>", line 2>) MAKE_FUNCTION LOAD_FAST 0 (seq) GET_ITER CALL 0 RETURN_VALUE Disassembly of <code object <genexpr> at 0x7f1ad8c25be0, file "<python-input-9>", line 2>: 2 RETURN_GENERATOR POP_TOP L1: RESUME 0 LOAD_FAST 0 (.0) L2: FOR_ITER 6 (to L3) # This is unsafe if .0 contains a non-iterator ...Now:
2 LOAD_CONST 0 (<code object <genexpr> at 0x7f1ad8c25be0, file "<python-input-9>", line 2>) MAKE_FUNCTION LOAD_FAST 0 (seq) GET_ITER CALL 0 RETURN_VALUE Disassembly of <code object <genexpr> at 0x7f1ad8c25be0, file "<python-input-9>", line 2>: 2 RETURN_GENERATOR POP_TOP L1: RESUME 0 LOAD_FAST 0 (.0) GET_ITER L2: FOR_ITER 6 (to L3) ...What I would like:
2 LOAD_CONST 0 (<code object <genexpr> at 0x7f1ad8c25be0, file "<python-input-9>", line 2>) MAKE_FUNCTION LOAD_FAST 0 (seq) CALL 0 RETURN_VALUE Disassembly of <code object <genexpr> at 0x7f1ad8c25be0, file "<python-input-9>", line 2>: 2 RETURN_GENERATOR POP_TOP L1: RESUME 0 LOAD_FAST 0 (.0) GET_ITER L2: FOR_ITER 6 (to L3) ...I think we should make generator expressions behave exactly like generators. It seems surprising that they would not.
FWIW we have discovered a segfault in libdnf because of this. The problem pre-existed in libdnf but was never triggered because nobody ever did
iter(iter(...))there.https://bugzilla.redhat.com/2330562 rpm-software-management/libdnf#1682
Reacted by Mikhail Efimov and Chris AdamsAnd another one: https://bugzilla.redhat.com/2331665 rpm-software-management/libcomps#116
I realize both of those use cases did it wrong, but this Python's new behavior tends to uncover problems that would otherwise never bite anybody.
- marked Python 3.13.3 runs __iter__ twice in a list comprehension #132711 as a duplicate of this issue
on Apr 19, 2025 This issue should be closed, shouldn't it?
@markshannon @picnixz @kayhayenReacted by Bénédikt TranI've started related d.p.o. thread here: https://discuss.python.org/t/generator-expressions-behavior-with-non-iterable-objects/96329.
- added a commit that references this issue
on Jun 24, 2025
Bug report
Bug description:
For my Python compiler Nuitka, I use CPython as the oracle of what the correct behaviour is. I am running some tests that I used to clarify the behavior from decades ago, in this case I wanted to know when exactly the iterator creation is used. I striped this test a bunch, so the regression is still visible. I first noticed the issue on GitHub Actions, where 3.13.0 got replaced with 3.13.1 for Windows and Linux, but it applies to all OSes. See below for a diff, that the same iterator is created multiple times.
The unified diff between 3.13.0 output (and basically all Python versions before) and 3.13.1 output.
The duplicated prints out the iterator creation are new. This is not optimal and new. I don't know if the iterator being through a slot cause cause this or what it is. I checked if
generator.cchanged but I think it didn't at all.My self compiled Python 3.13.1 for Linux and the official Windows download agree in behaviour.
CPython versions tested on:
3.13
Operating systems tested on:
Linux, Windows
Linked PRs
__iter__once in generator expressions. #132351