GH-96793: Change FOR_ITER to not pop the iterator on exhaustion. (GH-96801)

Change FOR_ITER to have the same stack effect regardless of whether it branches or not.
Performance is unchanged as FOR_ITER (and specialized forms jump over the cleanup code).
This commit is contained in:
Mark Shannon 2022-10-27 03:55:03 -07:00 committed by GitHub
parent e60892f9db
commit 22863df7ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 277 additions and 247 deletions

View file

@ -413,6 +413,15 @@ The Python compiler currently generates the following bytecode instructions.
Removes the top-of-stack (TOS) item.
.. opcode:: END_FOR
Removes the top two values from the stack.
Equivalent to POP_TOP; POP_TOP.
Used to clean up at the end of loops, hence the name.
.. versionadded:: 3.12
.. opcode:: COPY (i)
Push the *i*-th item to the top of the stack. The item is not removed from its
@ -1088,9 +1097,11 @@ iterations of the loop.
TOS is an :term:`iterator`. Call its :meth:`~iterator.__next__` method. If
this yields a new value, push it on the stack (leaving the iterator below
it). If the iterator indicates it is exhausted, TOS is popped, and the byte
it). If the iterator indicates it is exhausted then the byte
code counter is incremented by *delta*.
.. versionchanged:: 3.12
Up until 3.11 the iterator was popped when it was exhausted.
.. opcode:: LOAD_GLOBAL (namei)