Skip to content

Commit

Permalink
pythonGH-120507: Lower the BEFORE_WITH and BEFORE_ASYNC_WITH inst…
Browse files Browse the repository at this point in the history
…ructions. (python#120640)

* Remove BEFORE_WITH and BEFORE_ASYNC_WITH instructions.

* Add LOAD_SPECIAL instruction

* Reimplement `with` and `async with` statements using LOAD_SPECIAL
  • Loading branch information
markshannon authored Jun 18, 2024
1 parent 73dc1c6 commit 9cefcc0
Show file tree
Hide file tree
Showing 22 changed files with 663 additions and 651 deletions.
33 changes: 11 additions & 22 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down Expand Up @@ -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]))``.
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions Include/ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ PyAPI_FUNC(void) PyEval_ReleaseThread(PyThreadState *tstate);
#define FVS_MASK 0x4
#define FVS_HAVE_SPEC 0x4

/* Special methods used by LOAD_SPECIAL */
#define SPECIAL___ENTER__ 0
#define SPECIAL___EXIT__ 1
#define SPECIAL___AENTER__ 2
#define SPECIAL___AEXIT__ 3
#define SPECIAL_MAX 3

#ifndef Py_LIMITED_API
# define Py_CPYTHON_CEVAL_H
# include "cpython/ceval.h"
Expand Down
7 changes: 7 additions & 0 deletions Include/internal/pycore_ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@ typedef PyObject *(*conversion_func)(PyObject *);
PyAPI_DATA(const binaryfunc) _PyEval_BinaryOps[];
PyAPI_DATA(const conversion_func) _PyEval_ConversionFuncs[];

typedef struct _special_method {
PyObject *name;
const char *error;
} _Py_SpecialMethod;

PyAPI_DATA(const _Py_SpecialMethod) _Py_SpecialMethods[];

PyAPI_FUNC(int) _PyEval_CheckExceptStarTypeValid(PyThreadState *tstate, PyObject* right);
PyAPI_FUNC(int) _PyEval_CheckExceptTypeValid(PyThreadState *tstate, PyObject* right);
PyAPI_FUNC(int) _PyEval_ExceptionGroupMatch(PyObject* exc_value, PyObject *match_type, PyObject **match, PyObject **rest);
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ extern int _PyObject_IsInstanceDictEmpty(PyObject *);

// Export for 'math' shared extension
PyAPI_FUNC(PyObject*) _PyObject_LookupSpecial(PyObject *, PyObject *);
PyAPI_FUNC(PyObject*) _PyObject_LookupSpecialMethod(PyObject *self, PyObject *attr, PyObject **self_or_null);

extern int _PyObject_IsAbstract(PyObject *);

Expand Down
27 changes: 11 additions & 16 deletions Include/internal/pycore_opcode_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Include/internal/pycore_uop_ids.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Include/internal/pycore_uop_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9cefcc0

Please sign in to comment.