From 7a8c3d6ec14d203db0c0a68f291e0563071b7a40 Mon Sep 17 00:00:00 2001 From: Ken Jin Date: Tue, 14 Jun 2022 20:26:59 +0800 Subject: [PATCH 1/2] Documentation for removing LOAD_METHOD --- Doc/library/dis.rst | 27 ++++++++++++++------------- Doc/whatsnew/3.12.rst | 9 +++++++++ 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/Doc/library/dis.rst b/Doc/library/dis.rst index 9163d1a4421a8c..ffcc8b10d1c345 100644 --- a/Doc/library/dis.rst +++ b/Doc/library/dis.rst @@ -887,7 +887,20 @@ iterations of the loop. .. opcode:: LOAD_ATTR (namei) - Replaces TOS with ``getattr(TOS, co_names[namei])``. + If the low bit of ``namei`` is not set, this replaces TOS with + ``getattr(TOS, co_names[namei>>1])``. + + If the low bit of ``namei`` is set, this will attempt to load a method named + ``co_names[namei>>i]`` from the TOS object. TOS is popped. + This bytecode distinguishes two cases: if TOS has a method with the correct + name, the bytecode pushes the unbound method and TOS. TOS will be used as + the first argument (``self``) by :opcode:`CALL` when calling the + unbound method. Otherwise, ``NULL`` and the object return by the attribute + lookup are pushed. + + .. versionchanged:: 3.11 + If the low bit of ``namei`` is set, then a ``NULL`` or ``self`` is + pushed to the stack before the attribute or unbound method respectively. .. opcode:: COMPARE_OP (opname) @@ -1189,18 +1202,6 @@ iterations of the loop. .. versionadded:: 3.6 -.. opcode:: LOAD_METHOD (namei) - - Loads a method named ``co_names[namei]`` from the TOS object. TOS is popped. - This bytecode distinguishes two cases: if TOS has a method with the correct - name, the bytecode pushes the unbound method and TOS. TOS will be used as - the first argument (``self``) by :opcode:`CALL` when calling the - unbound method. Otherwise, ``NULL`` and the object return by the attribute - lookup are pushed. - - .. versionadded:: 3.7 - - .. opcode:: PUSH_NULL Pushes a ``NULL`` to the stack. diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index 5e295f0c469eca..59eb4348cdca47 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -106,6 +106,15 @@ Optimizations (Contributed by Inada Naoki in :gh:`92536`.) +CPython bytecode changes +======================== + +* Removed the :opcode:`LOAD_METHOD` instruction. It has been merged into + :opcode:`LOAD_ATTR`. :opcode:`LOAD_ATTR` will now behave like the old + :opcode:`LOAD_METHOD` instruction if the low bit of its oparg is set. + (Contributed by Ken Jin in :gh:`93429`.) + + Deprecated ========== From 0ba72942d2f747fa011dbf53d6a22ca070b4bb33 Mon Sep 17 00:00:00 2001 From: Ken Jin Date: Tue, 14 Jun 2022 20:32:31 +0800 Subject: [PATCH 2/2] fix a typo --- Doc/library/dis.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/dis.rst b/Doc/library/dis.rst index ffcc8b10d1c345..a6574c4ec1030b 100644 --- a/Doc/library/dis.rst +++ b/Doc/library/dis.rst @@ -891,7 +891,7 @@ iterations of the loop. ``getattr(TOS, co_names[namei>>1])``. If the low bit of ``namei`` is set, this will attempt to load a method named - ``co_names[namei>>i]`` from the TOS object. TOS is popped. + ``co_names[namei>>1]`` from the TOS object. TOS is popped. This bytecode distinguishes two cases: if TOS has a method with the correct name, the bytecode pushes the unbound method and TOS. TOS will be used as the first argument (``self``) by :opcode:`CALL` when calling the