summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2022-07-01 14:33:35 (GMT)
committerGitHub <noreply@github.com>2022-07-01 14:33:35 (GMT)
commitc57aad777afc6c0b382981ee9e4bc94c03bf5f68 (patch)
tree39128a485e3372715a4b2fe4e03d7117e0fba9fc /Doc
parentbe80db14c432c621e44920f8fd95a3f3191aca9b (diff)
downloadcpython-c57aad777afc6c0b382981ee9e4bc94c03bf5f68.zip
cpython-c57aad777afc6c0b382981ee9e4bc94c03bf5f68.tar.gz
cpython-c57aad777afc6c0b382981ee9e4bc94c03bf5f68.tar.bz2
gh-94216: add pseudo instructions to the dis/opcodes modules (GH-94241)
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/dis.rst82
-rw-r--r--Doc/whatsnew/3.12.rst11
2 files changed, 91 insertions, 2 deletions
diff --git a/Doc/library/dis.rst b/Doc/library/dis.rst
index d2c3c1a..9173c1b 100644
--- a/Doc/library/dis.rst
+++ b/Doc/library/dis.rst
@@ -1351,13 +1351,74 @@ iterations of the loop.
.. opcode:: HAVE_ARGUMENT
This is not really an opcode. It identifies the dividing line between
- opcodes which don't use their argument and those that do
- (``< HAVE_ARGUMENT`` and ``>= HAVE_ARGUMENT``, respectively).
+ opcodes in the range [0,255] which don't use their argument and those
+ that do (``< HAVE_ARGUMENT`` and ``>= HAVE_ARGUMENT``, respectively).
+
+ If your application uses pseudo instructions, use the :data:`hasarg`
+ collection instead.
.. versionchanged:: 3.6
Now every instruction has an argument, but opcodes ``< HAVE_ARGUMENT``
ignore it. Before, only opcodes ``>= HAVE_ARGUMENT`` had an argument.
+ .. versionchanged:: 3.12
+ Pseudo instructions were added to the :mod:`dis` module, and for them
+ it is not true that comparison with ``HAVE_ARGUMENT`` indicates whether
+ they use their arg.
+
+
+**Pseudo-instructions**
+
+These opcodes do not appear in python bytecode, they are used by the compiler
+but are replaced by real opcodes or removed before bytecode is generated.
+
+.. opcode:: SETUP_FINALLY (target)
+
+ Set up an exception handler for the following code block. If an exception
+ occurs, the value stack level is restored to its current state and control
+ is transferred to the exception handler at ``target``.
+
+
+.. opcode:: SETUP_CLEANUP (target)
+
+ Like ``SETUP_FINALLY``, but in case of exception also pushes the last
+ instruction (``lasti``) to the stack so that ``RERAISE`` can restore it.
+ If an exception occurs, the value stack level and the last instruction on
+ the frame are restored to their current state, and control is transferred
+ to the exception handler at ``target``.
+
+
+.. opcode:: SETUP_WITH (target)
+
+ Like ``SETUP_CLEANUP``, but in case of exception one more item is popped
+ from the stack before control is transferred to the exception handler at
+ ``target``.
+
+ This variant is used in :keyword:`with` and :keyword:`async with`
+ constructs, which push the return value of the context manager's
+ :meth:`~object.__enter__` or :meth:`~object.__aenter__` to the stack.
+
+
+.. opcode:: POP_BLOCK
+
+ Marks the end of the code block associated with the last ``SETUP_FINALLY``,
+ ``SETUP_CLEANUP`` or ``SETUP_WITH``.
+
+.. opcode:: JUMP
+.. opcode:: JUMP_NO_INTERRUPT
+.. opcode:: POP_JUMP_IF_FALSE
+.. opcode:: POP_JUMP_IF_TRUE
+.. opcode:: POP_JUMP_IF_NONE
+.. opcode:: POP_JUMP_IF_NOT_NONE
+
+ Undirected relative jump instructions which are replaced by their
+ directed (forward/backward) counterparts by the assembler.
+
+.. opcode:: LOAD_METHOD
+
+ Optimized unbound method lookup. Emitted as a ``LOAD_ATTR`` opcode
+ with a flag set in the arg.
+
.. _opcode_collections:
@@ -1367,6 +1428,10 @@ Opcode collections
These collections are provided for automatic introspection of bytecode
instructions:
+ .. versionchanged:: 3.12
+ The collections now contain pseudo instructions as well. These are
+ opcodes with values ``>= MIN_PSEUDO_OPCODE``.
+
.. data:: opname
Sequence of operation names, indexable using the bytecode.
@@ -1382,6 +1447,13 @@ instructions:
Sequence of all compare operation names.
+.. data:: hasarg
+
+ Sequence of bytecodes that use their argument.
+
+ .. versionadded:: 3.12
+
+
.. data:: hasconst
Sequence of bytecodes that access a constant.
@@ -1418,3 +1490,9 @@ instructions:
.. data:: hascompare
Sequence of bytecodes of Boolean operations.
+
+.. data:: hasexc
+
+ Sequence of bytecodes that set an exception handler.
+
+ .. versionadded:: 3.12
diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst
index 11e8b87..620aa91 100644
--- a/Doc/whatsnew/3.12.rst
+++ b/Doc/whatsnew/3.12.rst
@@ -93,6 +93,17 @@ New Modules
Improved Modules
================
+dis
+---
+
+* Pseudo instruction opcodes (which are used by the compiler but
+ do not appear in executable bytecode) are now exposed in the
+ :mod:`dis` module.
+ :data:`~dis.HAVE_ARGUMENT` is still relevant to real opcodes,
+ but it is not useful for pseudo instrcutions. Use the new
+ :data:`~dis.hasarg` collection instead.
+ (Contributed by Irit Katriel in :gh:`94216`.)
+
os
--