Kaydet (Commit) bdf525b7 authored tarafından Benjamin Peterson's avatar Benjamin Peterson

wrap everything at 80 chars

üst 29fec928
...@@ -9,9 +9,9 @@ ...@@ -9,9 +9,9 @@
-------------- --------------
The :mod:`dis` module supports the analysis of CPython :term:`bytecode` by The :mod:`dis` module supports the analysis of CPython :term:`bytecode` by
disassembling it. The CPython bytecode which this module takes as an disassembling it. The CPython bytecode which this module takes as an input is
input is defined in the file :file:`Include/opcode.h` and used by the compiler defined in the file :file:`Include/opcode.h` and used by the compiler and the
and the interpreter. interpreter.
.. impl-detail:: .. impl-detail::
...@@ -43,33 +43,31 @@ Bytecode analysis ...@@ -43,33 +43,31 @@ Bytecode analysis
.. versionadded:: 3.4 .. versionadded:: 3.4
The bytecode analysis API allows pieces of Python code to be wrapped in a The bytecode analysis API allows pieces of Python code to be wrapped in a
:class:`Bytecode` object that provides easy access to details of the :class:`Bytecode` object that provides easy access to details of the compiled
compiled code. code.
.. class:: Bytecode(x, *, first_line=None, current_offset=None) .. class:: Bytecode(x, *, first_line=None, current_offset=None)
Analyse the bytecode corresponding to a function, method, string of Analyse the bytecode corresponding to a function, method, string of source
source code, or a code object (as returned by :func:`compile`). code, or a code object (as returned by :func:`compile`).
This is a convenience wrapper around many of the functions listed below, This is a convenience wrapper around many of the functions listed below, most
most notably :func:`get_instructions`, as iterating over a notably :func:`get_instructions`, as iterating over a :class:`Bytecode`
:class:`Bytecode` instance yields the bytecode operations as instance yields the bytecode operations as :class:`Instruction` instances.
:class:`Instruction` instances.
If *first_line* is not None, it indicates the line number that should If *first_line* is not None, it indicates the line number that should be
be reported for the first source line in the disassembled code. reported for the first source line in the disassembled code. Otherwise, the
Otherwise, the source line information (if any) is taken directly from source line information (if any) is taken directly from the disassembled code
the disassembled code object. object.
If *current_offset* is not None, it refers to an instruction offset If *current_offset* is not None, it refers to an instruction offset in the
in the disassembled code. Setting this means :meth:`.dis` will display disassembled code. Setting this means :meth:`.dis` will display a "current
a "current instruction" marker against the specified opcode. instruction" marker against the specified opcode.
.. classmethod:: from_traceback(tb) .. classmethod:: from_traceback(tb)
Construct a :class:`Bytecode` instance from the given traceback, Construct a :class:`Bytecode` instance from the given traceback, setting
setting *current_offset* to the instruction responsible for the *current_offset* to the instruction responsible for the exception.
exception.
.. data:: codeobj .. data:: codeobj
...@@ -104,10 +102,9 @@ Example:: ...@@ -104,10 +102,9 @@ Example::
Analysis functions Analysis functions
------------------ ------------------
The :mod:`dis` module also defines the following analysis functions that The :mod:`dis` module also defines the following analysis functions that convert
convert the input directly to the desired output. They can be useful if the input directly to the desired output. They can be useful if only a single
only a single operation is being performed, so the intermediate analysis operation is being performed, so the intermediate analysis object isn't useful:
object isn't useful:
.. function:: code_info(x) .. function:: code_info(x)
...@@ -196,13 +193,13 @@ object isn't useful: ...@@ -196,13 +193,13 @@ object isn't useful:
Return an iterator over the instructions in the supplied function, method, Return an iterator over the instructions in the supplied function, method,
source code string or code object. source code string or code object.
The iterator generates a series of :class:`Instruction` named tuples The iterator generates a series of :class:`Instruction` named tuples giving
giving the details of each operation in the supplied code. the details of each operation in the supplied code.
If *first_line* is not None, it indicates the line number that should If *first_line* is not None, it indicates the line number that should be
be reported for the first source line in the disassembled code. reported for the first source line in the disassembled code. Otherwise, the
Otherwise, the source line information (if any) is taken directly from source line information (if any) is taken directly from the disassembled code
the disassembled code object. object.
.. versionadded:: 3.4 .. versionadded:: 3.4
...@@ -501,8 +498,8 @@ the original TOS1. ...@@ -501,8 +498,8 @@ the original TOS1.
.. opcode:: PRINT_EXPR .. opcode:: PRINT_EXPR
Implements the expression statement for the interactive mode. TOS is removed Implements the expression statement for the interactive mode. TOS is removed
from the stack and printed. In non-interactive mode, an expression statement is from the stack and printed. In non-interactive mode, an expression statement
terminated with :opcode:`POP_TOP`. is terminated with :opcode:`POP_TOP`.
.. opcode:: BREAK_LOOP .. opcode:: BREAK_LOOP
...@@ -532,9 +529,9 @@ the original TOS1. ...@@ -532,9 +529,9 @@ the original TOS1.
comprehensions. comprehensions.
For all of the :opcode:`SET_ADD`, :opcode:`LIST_APPEND` and :opcode:`MAP_ADD` For all of the :opcode:`SET_ADD`, :opcode:`LIST_APPEND` and :opcode:`MAP_ADD`
instructions, while the instructions, while the added value or key/value pair is popped off, the
added value or key/value pair is popped off, the container object remains on container object remains on the stack so that it is available for further
the stack so that it is available for further iterations of the loop. iterations of the loop.
.. opcode:: RETURN_VALUE .. opcode:: RETURN_VALUE
...@@ -556,23 +553,23 @@ the stack so that it is available for further iterations of the loop. ...@@ -556,23 +553,23 @@ the stack so that it is available for further iterations of the loop.
.. opcode:: IMPORT_STAR .. opcode:: IMPORT_STAR
Loads all symbols not starting with ``'_'`` directly from the module TOS to the Loads all symbols not starting with ``'_'`` directly from the module TOS to
local namespace. The module is popped after loading all names. This opcode the local namespace. The module is popped after loading all names. This
implements ``from module import *``. opcode implements ``from module import *``.
.. opcode:: POP_BLOCK .. opcode:: POP_BLOCK
Removes one block from the block stack. Per frame, there is a stack of blocks, Removes one block from the block stack. Per frame, there is a stack of
denoting nested loops, try statements, and such. blocks, denoting nested loops, try statements, and such.
.. opcode:: POP_EXCEPT .. opcode:: POP_EXCEPT
Removes one block from the block stack. The popped block must be an exception Removes one block from the block stack. The popped block must be an exception
handler block, as implicitly created when entering an except handler. handler block, as implicitly created when entering an except handler. In
In addition to popping extraneous values from the frame stack, the addition to popping extraneous values from the frame stack, the last three
last three popped values are used to restore the exception state. popped values are used to restore the exception state.
.. opcode:: END_FINALLY .. opcode:: END_FINALLY
...@@ -602,9 +599,9 @@ the stack so that it is available for further iterations of the loop. ...@@ -602,9 +599,9 @@ the stack so that it is available for further iterations of the loop.
.. opcode:: WITH_CLEANUP .. opcode:: WITH_CLEANUP
Cleans up the stack when a :keyword:`with` statement block exits. TOS is Cleans up the stack when a :keyword:`with` statement block exits. TOS is the
the context manager's :meth:`__exit__` bound method. Below TOS are 1--3 context manager's :meth:`__exit__` bound method. Below TOS are 1--3 values
values indicating how/why the finally clause was entered: indicating how/why the finally clause was entered:
* SECOND = ``None`` * SECOND = ``None``
* (SECOND, THIRD) = (``WHY_{RETURN,CONTINUE}``), retval * (SECOND, THIRD) = (``WHY_{RETURN,CONTINUE}``), retval
...@@ -614,10 +611,10 @@ the stack so that it is available for further iterations of the loop. ...@@ -614,10 +611,10 @@ the stack so that it is available for further iterations of the loop.
In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise
``TOS(None, None, None)``. In addition, TOS is removed from the stack. ``TOS(None, None, None)``. In addition, TOS is removed from the stack.
If the stack represents an exception, *and* the function call returns If the stack represents an exception, *and* the function call returns a
a 'true' value, this information is "zapped" and replaced with a single 'true' value, this information is "zapped" and replaced with a single
``WHY_SILENCED`` to prevent :opcode:`END_FINALLY` from re-raising the exception. ``WHY_SILENCED`` to prevent :opcode:`END_FINALLY` from re-raising the
(But non-local gotos will still be resumed.) exception. (But non-local gotos will still be resumed.)
.. XXX explain the WHY stuff! .. XXX explain the WHY stuff!
...@@ -628,8 +625,8 @@ the more significant byte last. ...@@ -628,8 +625,8 @@ the more significant byte last.
.. opcode:: STORE_NAME (namei) .. opcode:: STORE_NAME (namei)
Implements ``name = TOS``. *namei* is the index of *name* in the attribute Implements ``name = TOS``. *namei* is the index of *name* in the attribute
:attr:`co_names` of the code object. The compiler tries to use :opcode:`STORE_FAST` :attr:`co_names` of the code object. The compiler tries to use
or :opcode:`STORE_GLOBAL` if possible. :opcode:`STORE_FAST` or :opcode:`STORE_GLOBAL` if possible.
.. opcode:: DELETE_NAME (namei) .. opcode:: DELETE_NAME (namei)
...@@ -689,8 +686,8 @@ the more significant byte last. ...@@ -689,8 +686,8 @@ the more significant byte last.
.. opcode:: BUILD_TUPLE (count) .. opcode:: BUILD_TUPLE (count)
Creates a tuple consuming *count* items from the stack, and pushes the resulting Creates a tuple consuming *count* items from the stack, and pushes the
tuple onto the stack. resulting tuple onto the stack.
.. opcode:: BUILD_LIST (count) .. opcode:: BUILD_LIST (count)
...@@ -724,8 +721,8 @@ the more significant byte last. ...@@ -724,8 +721,8 @@ the more significant byte last.
Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide Imports the module ``co_names[namei]``. TOS and TOS1 are popped and provide
the *fromlist* and *level* arguments of :func:`__import__`. The module the *fromlist* and *level* arguments of :func:`__import__`. The module
object is pushed onto the stack. The current namespace is not affected: object is pushed onto the stack. The current namespace is not affected: for
for a proper import statement, a subsequent :opcode:`STORE_FAST` instruction a proper import statement, a subsequent :opcode:`STORE_FAST` instruction
modifies the namespace. modifies the namespace.
...@@ -753,14 +750,14 @@ the more significant byte last. ...@@ -753,14 +750,14 @@ the more significant byte last.
.. opcode:: JUMP_IF_TRUE_OR_POP (target) .. opcode:: JUMP_IF_TRUE_OR_POP (target)
If TOS is true, sets the bytecode counter to *target* and leaves TOS If TOS is true, sets the bytecode counter to *target* and leaves TOS on the
on the stack. Otherwise (TOS is false), TOS is popped. stack. Otherwise (TOS is false), TOS is popped.
.. opcode:: JUMP_IF_FALSE_OR_POP (target) .. opcode:: JUMP_IF_FALSE_OR_POP (target)
If TOS is false, sets the bytecode counter to *target* and leaves If TOS is false, sets the bytecode counter to *target* and leaves TOS on the
TOS on the stack. Otherwise (TOS is true), TOS is popped. stack. Otherwise (TOS is true), TOS is popped.
.. opcode:: JUMP_ABSOLUTE (target) .. opcode:: JUMP_ABSOLUTE (target)
...@@ -770,10 +767,10 @@ the more significant byte last. ...@@ -770,10 +767,10 @@ the more significant byte last.
.. opcode:: FOR_ITER (delta) .. opcode:: FOR_ITER (delta)
TOS is an :term:`iterator`. Call its :meth:`~iterator.__next__` method. TOS is an :term:`iterator`. Call its :meth:`~iterator.__next__` method. If
If this yields a new value, push it on the stack (leaving the iterator below 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 it). If the iterator indicates it is exhausted TOS is popped, and the byte
byte code counter is incremented by *delta*. code counter is incremented by *delta*.
.. opcode:: LOAD_GLOBAL (namei) .. opcode:: LOAD_GLOBAL (namei)
...@@ -789,19 +786,19 @@ the more significant byte last. ...@@ -789,19 +786,19 @@ the more significant byte last.
.. opcode:: SETUP_EXCEPT (delta) .. opcode:: SETUP_EXCEPT (delta)
Pushes a try block from a try-except clause onto the block stack. *delta* points Pushes a try block from a try-except clause onto the block stack. *delta*
to the first except block. points to the first except block.
.. opcode:: SETUP_FINALLY (delta) .. opcode:: SETUP_FINALLY (delta)
Pushes a try block from a try-except clause onto the block stack. *delta* points Pushes a try block from a try-except clause onto the block stack. *delta*
to the finally block. points to the finally block.
.. opcode:: STORE_MAP .. opcode:: STORE_MAP
Store a key and value pair in a dictionary. Pops the key and value while leaving Store a key and value pair in a dictionary. Pops the key and value while
the dictionary on the stack. leaving the dictionary on the stack.
.. opcode:: LOAD_FAST (var_num) .. opcode:: LOAD_FAST (var_num)
...@@ -821,8 +818,8 @@ the more significant byte last. ...@@ -821,8 +818,8 @@ the more significant byte last.
.. opcode:: LOAD_CLOSURE (i) .. opcode:: LOAD_CLOSURE (i)
Pushes a reference to the cell contained in slot *i* of the cell and free Pushes a reference to the cell contained in slot *i* of the cell and free
variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i - less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
len(co_cellvars)]``. len(co_cellvars)]``.
...@@ -862,11 +859,12 @@ the more significant byte last. ...@@ -862,11 +859,12 @@ the more significant byte last.
Calls a function. The low byte of *argc* indicates the number of positional Calls a function. The low byte of *argc* indicates the number of positional
parameters, the high byte the number of keyword parameters. On the stack, the parameters, the high byte the number of keyword parameters. On the stack, the
opcode finds the keyword parameters first. For each keyword argument, the value opcode finds the keyword parameters first. For each keyword argument, the
is on top of the key. Below the keyword parameters, the positional parameters value is on top of the key. Below the keyword parameters, the positional
are on the stack, with the right-most parameter on top. Below the parameters, parameters are on the stack, with the right-most parameter on top. Below the
the function object to call is on the stack. Pops all function arguments, and parameters, the function object to call is on the stack. Pops all function
the function itself off the stack, and pushes the return value. arguments, and the function itself off the stack, and pushes the return
value.
.. opcode:: MAKE_FUNCTION (argc) .. opcode:: MAKE_FUNCTION (argc)
...@@ -889,8 +887,8 @@ the more significant byte last. ...@@ -889,8 +887,8 @@ the more significant byte last.
Creates a new function object, sets its *__closure__* slot, and pushes it on Creates a new function object, sets its *__closure__* slot, and pushes it on
the stack. TOS is the :term:`qualified name` of the function, TOS1 is the the stack. TOS is the :term:`qualified name` of the function, TOS1 is the
code associated with the function, and TOS2 is the tuple containing cells for code associated with the function, and TOS2 is the tuple containing cells for
the closure's free variables. The function also has *argc* default parameters, the closure's free variables. The function also has *argc* default
which are found below the cells. parameters, which are found below the cells.
.. opcode:: BUILD_SLICE (argc) .. opcode:: BUILD_SLICE (argc)
...@@ -906,36 +904,37 @@ the more significant byte last. ...@@ -906,36 +904,37 @@ the more significant byte last.
Prefixes any opcode which has an argument too big to fit into the default two Prefixes any opcode which has an argument too big to fit into the default two
bytes. *ext* holds two additional bytes which, taken together with the bytes. *ext* holds two additional bytes which, taken together with the
subsequent opcode's argument, comprise a four-byte argument, *ext* being the two subsequent opcode's argument, comprise a four-byte argument, *ext* being the
most-significant bytes. two most-significant bytes.
.. opcode:: CALL_FUNCTION_VAR (argc) .. opcode:: CALL_FUNCTION_VAR (argc)
Calls a function. *argc* is interpreted as in :opcode:`CALL_FUNCTION`. The top element Calls a function. *argc* is interpreted as in :opcode:`CALL_FUNCTION`. The
on the stack contains the variable argument list, followed by keyword and top element on the stack contains the variable argument list, followed by
positional arguments. keyword and positional arguments.
.. opcode:: CALL_FUNCTION_KW (argc) .. opcode:: CALL_FUNCTION_KW (argc)
Calls a function. *argc* is interpreted as in :opcode:`CALL_FUNCTION`. The top element Calls a function. *argc* is interpreted as in :opcode:`CALL_FUNCTION`. The
on the stack contains the keyword arguments dictionary, followed by explicit top element on the stack contains the keyword arguments dictionary, followed
keyword and positional arguments. by explicit keyword and positional arguments.
.. opcode:: CALL_FUNCTION_VAR_KW (argc) .. opcode:: CALL_FUNCTION_VAR_KW (argc)
Calls a function. *argc* is interpreted as in :opcode:`CALL_FUNCTION`. The top Calls a function. *argc* is interpreted as in :opcode:`CALL_FUNCTION`. The
element on the stack contains the keyword arguments dictionary, followed by the top element on the stack contains the keyword arguments dictionary, followed
variable-arguments tuple, followed by explicit keyword and positional arguments. by the variable-arguments tuple, followed by explicit keyword and positional
arguments.
.. opcode:: HAVE_ARGUMENT .. opcode:: HAVE_ARGUMENT
This is not really an opcode. It identifies the dividing line between opcodes This is not really an opcode. It identifies the dividing line between
which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>= opcodes which don't take arguments ``< HAVE_ARGUMENT`` and those which do
HAVE_ARGUMENT``. ``>= HAVE_ARGUMENT``.
.. _opcode_collections: .. _opcode_collections:
...@@ -967,10 +966,10 @@ instructions: ...@@ -967,10 +966,10 @@ instructions:
.. data:: hasfree .. data:: hasfree
Sequence of bytecodes that access a free variable (note that 'free' in Sequence of bytecodes that access a free variable (note that 'free' in this
this context refers to names in the current scope that are referenced by context refers to names in the current scope that are referenced by inner
inner scopes or names in outer scopes that are referenced from this scope. scopes or names in outer scopes that are referenced from this scope. It does
It does *not* include references to global or builtin scopes). *not* include references to global or builtin scopes).
.. data:: hasname .. data:: hasname
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment