source: trunk/essentials/dev-lang/python/Doc/lib/libdis.tex@ 3398

Last change on this file since 3398 was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 21.2 KB
Line 
1\section{\module{dis} ---
2 Disassembler for Python byte code}
3
4\declaremodule{standard}{dis}
5\modulesynopsis{Disassembler for Python byte code.}
6
7
8The \module{dis} module supports the analysis of Python byte code by
9disassembling it. Since there is no Python assembler, this module
10defines the Python assembly language. The Python byte code which
11this module takes as an input is defined in the file
12\file{Include/opcode.h} and used by the compiler and the interpreter.
13
14Example: Given the function \function{myfunc}:
15
16\begin{verbatim}
17def myfunc(alist):
18 return len(alist)
19\end{verbatim}
20
21the 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
34The \module{dis} module defines the following functions and constants:
35
36\begin{funcdesc}{dis}{\optional{bytesource}}
37Disassemble the \var{bytesource} object. \var{bytesource} can denote
38either a module, a class, a method, a function, or a code object.
39For a module, it disassembles all functions. For a class,
40it disassembles all methods. For a single code sequence, it prints
41one line per byte code instruction. If no object is provided, it
42disassembles the last traceback.
43\end{funcdesc}
44
45\begin{funcdesc}{distb}{\optional{tb}}
46Disassembles the top-of-stack function of a traceback, using the last
47traceback if none was passed. The instruction causing the exception
48is indicated.
49\end{funcdesc}
50
51\begin{funcdesc}{disassemble}{code\optional{, lasti}}
52Disassembles a code object, indicating the last instruction if \var{lasti}
53was 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
65The parameter interpretation recognizes local and global
66variable names, constant values, branch targets, and compare
67operators.
68\end{funcdesc}
69
70\begin{funcdesc}{disco}{code\optional{, lasti}}
71A synonym for disassemble. It is more convenient to type, and kept
72for compatibility with earlier Python releases.
73\end{funcdesc}
74
75\begin{datadesc}{opname}
76Sequence of operation names, indexable using the byte code.
77\end{datadesc}
78
79\begin{datadesc}{opmap}
80Dictionary mapping byte codes to operation names.
81\end{datadesc}
82
83\begin{datadesc}{cmp_op}
84Sequence of all compare operation names.