GH-120507: Lower the BEFORE_WITH and BEFORE_ASYNC_WITH instructions. (#120640)

* Remove BEFORE_WITH and BEFORE_ASYNC_WITH instructions.

* Add LOAD_SPECIAL instruction

* Reimplement `with` and `async with` statements using LOAD_SPECIAL
This commit is contained in:
Mark Shannon 2024-06-18 12:17:46 +01:00 committed by GitHub
parent 73dc1c678e
commit 9cefcc0ee7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
22 changed files with 663 additions and 651 deletions

View file

@ -780,16 +780,6 @@ not have to be) the original ``STACK[-2]``.
.. versionadded:: 3.12
.. opcode:: BEFORE_ASYNC_WITH
Resolves ``__aenter__`` and ``__aexit__`` from ``STACK[-1]``.
Pushes ``__aexit__`` and result of ``__aenter__()`` to the stack::
STACK.extend((__aexit__, __aenter__())
.. versionadded:: 3.5
**Miscellaneous opcodes**
@ -944,18 +934,6 @@ iterations of the loop.
Pushes :func:`!builtins.__build_class__` onto the stack. It is later called
to construct a class.
.. opcode:: BEFORE_WITH
This opcode performs several operations before a with block starts. First,
it loads :meth:`~object.__exit__` from the context manager and pushes it onto
the stack for later use by :opcode:`WITH_EXCEPT_START`. Then,
:meth:`~object.__enter__` is called. Finally, the result of calling the
``__enter__()`` method is pushed onto the stack.
.. versionadded:: 3.11
.. opcode:: GET_LEN
Perform ``STACK.append(len(STACK[-1]))``.
@ -1812,6 +1790,17 @@ iterations of the loop.
.. versionadded:: 3.12
.. opcode:: LOAD_SPECIAL
Performs special method lookup on ``STACK[-1]``.
If ``type(STACK[-1]).__xxx__`` is a method, leave
``type(STACK[-1]).__xxx__; STACK[-1]`` on the stack.
If ``type(STACK[-1]).__xxx__`` is not a method, leave
``STACK[-1].__xxx__; NULL`` on the stack.
.. versionadded:: 3.14
**Pseudo-instructions**
These opcodes do not appear in Python bytecode. They are used by the compiler