| 1 | \section{\module{dis} ---
|
|---|
| 2 | Disassembler for Python byte code}
|
|---|
| 3 |
|
|---|
| 4 | \declaremodule{standard}{dis}
|
|---|
| 5 | \modulesynopsis{Disassembler for Python byte code.}
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 | The \module{dis} module supports the analysis of Python byte code by
|
|---|
| 9 | disassembling it. Since there is no Python assembler, this module
|
|---|
| 10 | defines the Python assembly language. The Python byte code which
|
|---|
| 11 | this module takes as an input is defined in the file
|
|---|
| 12 | \file{Include/opcode.h} and used by the compiler and the interpreter.
|
|---|
| 13 |
|
|---|
| 14 | Example: Given the function \function{myfunc}:
|
|---|
| 15 |
|
|---|
| 16 | \begin{verbatim}
|
|---|
| 17 | def myfunc(alist):
|
|---|
| 18 | return len(alist)
|
|---|
| 19 | \end{verbatim}
|
|---|
| 20 |
|
|---|
| 21 | the following command can be used to get the disassembly of
|
|---|
| 22 | \function{myfunc()}:
|
|---|
| 23 |
|
|---|
| 24 | \begin{verbatim}
|
|---|
| 25 | >>> dis.dis(myfunc)
|
|---|
| 26 | 2 0 LOAD_GLOBAL 0 (len)
|
|---|
| 27 | 3 LOAD_FAST 0 (alist)
|
|---|
| 28 | 6 CALL_FUNCTION 1
|
|---|
| 29 | 9 RETURN_VALUE
|
|---|
| 30 | \end{verbatim}
|
|---|
| 31 |
|
|---|
| 32 | (The ``2'' is a line number).
|
|---|
| 33 |
|
|---|
| 34 | The \module{dis} module defines the following functions and constants:
|
|---|
| 35 |
|
|---|
| 36 | \begin{funcdesc}{dis}{\optional{bytesource}}
|
|---|
| 37 | Disassemble the \var{bytesource} object. \var{bytesource} can denote
|
|---|
| 38 | either a module, a class, a method, a function, or a code object.
|
|---|
| 39 | For a module, it disassembles all functions. For a class,
|
|---|
| 40 | it disassembles all methods. For a single code sequence, it prints
|
|---|
| 41 | one line per byte code instruction. If no object is provided, it
|
|---|
| 42 | disassembles the last traceback.
|
|---|
| 43 | \end{funcdesc}
|
|---|
| 44 |
|
|---|
| 45 | \begin{funcdesc}{distb}{\optional{tb}}
|
|---|
| 46 | Disassembles the top-of-stack function of a traceback, using the last
|
|---|
| 47 | traceback if none was passed. The instruction causing the exception
|
|---|
| 48 | is indicated.
|
|---|
| 49 | \end{funcdesc}
|
|---|
| 50 |
|
|---|
| 51 | \begin{funcdesc}{disassemble}{code\optional{, lasti}}
|
|---|
| 52 | Disassembles a code object, indicating the last instruction if \var{lasti}
|
|---|
| 53 | was provided. The output is divided in the following columns:
|
|---|
| 54 |
|
|---|
| 55 | \begin{enumerate}
|
|---|
| 56 | \item the line number, for the first instruction of each line
|
|---|
| 57 | \item the current instruction, indicated as \samp{-->},
|
|---|
| 58 | \item a labelled instruction, indicated with \samp{>>},
|
|---|
| 59 | \item the address of the instruction,
|
|---|
| 60 | \item the operation code name,
|
|---|
| 61 | \item operation parameters, and
|
|---|
| 62 | \item interpretation of the parameters in parentheses.
|
|---|
| 63 | \end{enumerate}
|
|---|
| 64 |
|
|---|
| 65 | The parameter interpretation recognizes local and global
|
|---|
| 66 | variable names, constant values, branch targets, and compare
|
|---|
| 67 | operators.
|
|---|
| 68 | \end{funcdesc}
|
|---|
| 69 |
|
|---|
| 70 | \begin{funcdesc}{disco}{code\optional{, lasti}}
|
|---|
| 71 | A synonym for disassemble. It is more convenient to type, and kept
|
|---|
| 72 | for compatibility with earlier Python releases.
|
|---|
| 73 | \end{funcdesc}
|
|---|
| 74 |
|
|---|
| 75 | \begin{datadesc}{opname}
|
|---|
| 76 | Sequence of operation names, indexable using the byte code.
|
|---|
| 77 | \end{datadesc}
|
|---|
| 78 |
|
|---|
| 79 | \begin{datadesc}{opmap}
|
|---|
| 80 | Dictionary mapping byte codes to operation names.
|
|---|
| 81 | \end{datadesc}
|
|---|
| 82 |
|
|---|
| 83 | \begin{datadesc}{cmp_op}
|
|---|
| 84 | Sequence of all compare operation names.
|
|---|
|
|---|