1 \section{\module{dis
} ---
3 \declaremodule{standard
}{dis
}
5 \modulesynopsis{Disassembler.
}
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.
14 Example: Given the function
\function{myfunc
}:
21 the following command can be used to get the disassembly of
33 16 LOAD_CONST
0 (None)
37 The
\module{dis
} module defines the following functions:
39 \begin{funcdesc
}{dis
}{\optional{bytesource
}}
40 Disassemble the
\var{bytesource
} object.
\var{bytesource
} can denote
41 either a class, a method, a function, or a code object. For a class,
42 it disassembles all methods. For a single code sequence, it prints
43 one line per byte code instruction. If no object is provided, it
44 disassembles the last traceback.
47 \begin{funcdesc
}{distb
}{\optional{tb
}}
48 Disassembles the top-of-stack function of a traceback, using the last
49 traceback if none was passed. The instruction causing the exception
53 \begin{funcdesc
}{disassemble
}{code
\optional{, lasti
}}
54 Disassembles a code object, indicating the last instruction if
\var{lasti
}
55 was provided. The output is divided in the following columns:
58 \item the current instruction, indicated as
\samp{-->
},
59 \item a labelled instruction, indicated with
\samp{>>
},
60 \item the address of the instruction,
61 \item the operation code name,
62 \item operation parameters, and
63 \item interpretation of the parameters in parentheses.
66 The parameter interpretation recognizes local and global
67 variable names, constant values, branch targets, and compare
71 \begin{funcdesc
}{disco
}{code
\optional{, lasti
}}
72 A synonym for disassemble. It is more convenient to type, and kept
73 for compatibility with earlier Python releases.
76 \begin{datadesc
}{opname
}
77 Sequence of a operation names, indexable using the byte code.
80 \begin{datadesc
}{cmp_op
}
81 Sequence of all compare operation names.
84 \begin{datadesc
}{hasconst
}
85 Sequence of byte codes that have a constant parameter.
88 \begin{datadesc
}{hasname
}
89 Sequence of byte codes that access a attribute by name.
92 \begin{datadesc
}{hasjrel
}
93 Sequence of byte codes that have a relative jump target.
96 \begin{datadesc
}{hasjabs
}
97 Sequence of byte codes that have an absolute jump target.
100 \begin{datadesc
}{haslocal
}
101 Sequence of byte codes that access a a local variable.
104 \begin{datadesc
}{hascompare
}
105 Sequence of byte codes of boolean operations.
108 \subsection{Python Byte Code Instructions
}
111 The Python compiler currently generates the following byte code
114 \setindexsubitem{(byte code insns)
}
116 \begin{opcodedesc
}{STOP_CODE
}{}
117 Indicates end-of-code to the compiler, not used by the interpreter.
120 \begin{opcodedesc
}{POP_TOP
}{}
121 Removes the top-of-stack (TOS) item.
124 \begin{opcodedesc
}{ROT_TWO
}{}
125 Swaps the two top-most stack items.
128 \begin{opcodedesc
}{ROT_THREE
}{}
129 Lifts second and third stack item one position up, moves top down
133 \begin{opcodedesc
}{DUP_TOP
}{}
134 Duplicates the reference on top of the stack.
137 Unary Operations take the top of the stack, apply the operation, and
138 push the result back on the stack.
140 \begin{opcodedesc
}{UNARY_POSITIVE
}{}
141 Implements
\code{TOS = +TOS
}.
144 \begin{opcodedesc
}{UNARY_NEG
}{}
145 Implements
\code{TOS = -TOS
}.
148 \begin{opcodedesc
}{UNARY_NOT
}{}
149 Implements
\code{TOS = not TOS
}.
152 \begin{opcodedesc
}{UNARY_CONVERT
}{}
153 Implements
\code{TOS = `TOS`
}.
156 \begin{opcodedesc
}{UNARY_INVERT
}{}
157 Implements
\code{TOS = \~TOS
}.
160 Binary operations remove the top of the stack (TOS) and the second top-most
161 stack item (TOS1) from the stack. They perform the operation, and put the
162 result back on the stack.
164 \begin{opcodedesc
}{BINARY_POWER
}{}
165 Implements
\code{TOS = TOS1 ** TOS
}.
168 \begin{opcodedesc
}{BINARY_MULTIPLY
}{}
169 Implements
\code{TOS = TOS1 * TOS
}.
172 \begin{opcodedesc
}{BINARY_DIVIDE
}{}
173 Implements
\code{TOS = TOS1 / TOS
}.
176 \begin{opcodedesc
}{BINARY_MODULO
}{}
177 Implements
\code{TOS = TOS1 \% TOS
}.
180 \begin{opcodedesc
}{BINARY_ADD
}{}
181 Implements
\code{TOS = TOS1 + TOS
}.
184 \begin{opcodedesc
}{BINARY_SUBTRACT
}{}
185 Implements
\code{TOS = TOS1 - TOS
}.
188 \begin{opcodedesc
}{BINARY_SUBSCR
}{}
189 Implements
\code{TOS = TOS1
[TOS
]}.
192 \begin{opcodedesc
}{BINARY_LSHIFT
}{}
193 Implements
\code{TOS = TOS1 << TOS
}.
196 \begin{opcodedesc
}{BINARY_RSHIFT
}{}
197 Implements
\code{TOS = TOS1 >> TOS
}.
200 \begin{opcodedesc
}{BINARY_AND
}{}
201 Implements
\code{TOS = TOS1 and TOS
}.
204 \begin{opcodedesc
}{BINARY_XOR
}{}
205 Implements
\code{TOS = TOS1 \^\ TOS
}.
208 \begin{opcodedesc
}{BINARY_OR
}{}
209 Implements
\code{TOS = TOS1 or TOS
}.
212 The slice opcodes take up to three parameters.
214 \begin{opcodedesc
}{SLICE+
0}{}
215 Implements
\code{TOS = TOS
[:
]}.
218 \begin{opcodedesc
}{SLICE+
1}{}
219 Implements
\code{TOS = TOS1
[TOS:
]}.
222 \begin{opcodedesc
}{SLICE+
2}{}
223 Implements
\code{TOS = TOS1
[:TOS1
]}.
226 \begin{opcodedesc
}{SLICE+
3}{}
227 Implements
\code{TOS = TOS2
[TOS1:TOS
]}.
230 Slice assignment needs even an additional parameter. As any statement,
231 they put nothing on the stack.
233 \begin{opcodedesc
}{STORE_SLICE+
0}{}
234 Implements
\code{TOS
[:
] = TOS1
}.
237 \begin{opcodedesc
}{STORE_SLICE+
1}{}
238 Implements
\code{TOS1
[TOS:
] = TOS2
}.
241 \begin{opcodedesc
}{STORE_SLICE+
2}{}
242 Implements
\code{TOS1
[:TOS
] = TOS2
}.
245 \begin{opcodedesc
}{STORE_SLICE+
3}{}
246 Implements
\code{TOS2
[TOS1:TOS
] = TOS3
}.
249 \begin{opcodedesc
}{DELETE_SLICE+
0}{}
250 Implements
\code{del TOS
[:
]}.
253 \begin{opcodedesc
}{DELETE_SLICE+
1}{}
254 Implements
\code{del TOS1
[TOS:
]}.
257 \begin{opcodedesc
}{DELETE_SLICE+
2}{}
258 Implements
\code{del TOS1
[:TOS
]}.
261 \begin{opcodedesc
}{DELETE_SLICE+
3}{}
262 Implements
\code{del TOS2
[TOS1:TOS
]}.
265 \begin{opcodedesc
}{STORE_SUBSCR
}{}
266 Implements
\code{TOS1
[TOS
] = TOS2
}.
269 \begin{opcodedesc
}{DELETE_SUBSCR
}{}
270 Implements
\code{del TOS1
[TOS
]}.
273 \begin{opcodedesc
}{PRINT_EXPR
}{}
274 Implements the expression statement for the interactive mode. TOS is
275 removed from the stack and printed. In non-interactive mode, an
276 expression statement is terminated with
\code{POP_STACK
}.
279 \begin{opcodedesc
}{PRINT_ITEM
}{}
280 Prints TOS. There is one such instruction for
281 each item in the print statement.
284 \begin{opcodedesc
}{PRINT_NEWLINE
}{}
285 Prints a new line on
\code{sys.stdout
}. This is generated as the
286 last operation of a print statement, unless the statement ends
290 \begin{opcodedesc
}{BREAK_LOOP
}{}
291 Terminates a loop due to a break statement.
294 \begin{opcodedesc
}{LOAD_LOCALS
}{}
295 Pushes a reference to the locals of the current scope on the stack.
296 This is used in the code for a class definition: After the class body
297 is evaluated, the locals are passed to the class definition.
300 \begin{opcodedesc
}{RETURN_VALUE
}{}
301 Returns with TOS to the caller of the function.
304 \begin{opcodedesc
}{EXEC_STMT
}{}
305 Implements
\code{exec TOS2,TOS1,TOS
}. The compiler fills
306 missing optional parameters with None.
309 \begin{opcodedesc
}{POP_BLOCK
}{}
310 Removes one block from the block stack. Per frame, there is a
311 stack of blocks, denoting nested loops, try statements, and such.
314 \begin{opcodedesc
}{END_FINALLY
}{}
315 Terminates a finally-block. The interpreter recalls whether the
316 exception has to be re-raised, or whether the function returns,
317 and continues with the outer-next block.
320 \begin{opcodedesc
}{BUILD_CLASS
}{}
321 Creates a new class object. TOS is the methods dictionary, TOS1
322 the tuple of the names of the base classes, and TOS2 the class name.
325 All of the following opcodes expect arguments. An argument is two
326 bytes, with the more significant byte last.
328 \begin{opcodedesc
}{STORE_NAME
}{namei
}
329 Implements
\code{name = TOS
}.
\var{namei
} is the index of
\var{name
}
330 in the attribute
\member{co_names
} of the code object.
331 The compiler tries to use
\code{STORE_LOCAL
} or
\code{STORE_GLOBAL
}
335 \begin{opcodedesc
}{DELETE_NAME
}{namei
}
336 Implements
\code{del name
}, where
\var{namei
} is the index into
337 \member{co_names
} attribute of the code object.
340 \begin{opcodedesc
}{UNPACK_TUPLE
}{count
}
341 Unpacks TOS into
\var{count
} individual values, which are put onto
342 the stack right-to-left.
345 \begin{opcodedesc
}{UNPACK_LIST
}{count
}
346 Unpacks TOS into
\var{count
} individual values.
349 %\begin{opcodedesc}{UNPACK_ARG}{count}
350 %This opcode is obsolete.
353 \begin{opcodedesc
}{STORE_ATTR
}{namei
}
354 Implements
\code{TOS.name = TOS1
}, where
\var{namei
} is the index
355 of name in
\member{co_names
}.
358 \begin{opcodedesc
}{DELETE_ATTR
}{namei
}
359 Implements
\code{del TOS.name
}, using
\var{namei
} as index into
363 \begin{opcodedesc
}{STORE_GLOBAL
}{namei
}
364 Works as
\code{STORE_NAME
}, but stores the name as a global.
367 \begin{opcodedesc
}{DELETE_GLOBAL
}{namei
}
368 Works as
\code{DELETE_NAME
}, but deletes a global name.
371 %\begin{opcodedesc}{UNPACK_VARARG}{argc}
372 %This opcode is obsolete.
375 \begin{opcodedesc
}{LOAD_CONST
}{consti
}
376 Pushes
\samp{co_consts
[\var{consti
}]} onto the stack.
379 \begin{opcodedesc
}{LOAD_NAME
}{namei
}
380 Pushes the value associated with
\samp{co_names
[\var{namei
}]} onto the stack.
383 \begin{opcodedesc
}{BUILD_TUPLE
}{count
}
384 Creates a tuple consuming
\var{count
} items from the stack, and pushes
385 the resulting tuple onto the stack.
388 \begin{opcodedesc
}{BUILD_LIST
}{count
}
389 Works as
\code{BUILD_TUPLE
}, but creates a list.
392 \begin{opcodedesc
}{BUILD_MAP
}{zero
}
393 Pushes an empty dictionary object onto the stack. The argument is ignored
394 and set to zero by the compiler.
397 \begin{opcodedesc
}{LOAD_ATTR
}{namei
}
398 Replaces TOS with
\code{getattr(TOS,co_names
[\var{namei
}]}.
401 \begin{opcodedesc
}{COMPARE_OP
}{opname
}
402 Performs a boolean operation. The operation name can be found
403 in
\code{cmp_op
[\var{opname
}]}.
406 \begin{opcodedesc
}{IMPORT_NAME
}{namei
}
407 Imports the module
\code{co_names
[\var{namei
}]}. The module object is
408 pushed onto the stack. The current name space is not affected: for a
409 proper import statement, a subsequent
\code{STORE_FAST
} instruction
410 modifies the name space.
413 \begin{opcodedesc
}{IMPORT_FROM
}{namei
}
414 Imports the attribute
\code{co_names
[\var{namei
}]}. The module to import
415 from is found in TOS and left there.
418 \begin{opcodedesc
}{JUMP_FORWARD
}{delta
}
419 Increments byte code counter by
\var{delta
}.
422 \begin{opcodedesc
}{JUMP_IF_TRUE
}{delta
}
423 If TOS is true, increment the byte code counter by
\var{delta
}. TOS is
427 \begin{opcodedesc
}{JUMP_IF_FALSE
}{delta
}
428 If TOS is false, increment the byte code counter by
\var{delta
}. TOS
432 \begin{opcodedesc
}{JUMP_ABSOLUTE
}{target
}
433 Set byte code counter to
\var{target
}.
436 \begin{opcodedesc
}{FOR_LOOP
}{delta
}
437 Iterate over a sequence. TOS is the current index, TOS1 the sequence.
438 First, the next element is computed. If the sequence is exhausted,
439 increment byte code counter by
\var{delta
}. Otherwise, push the
440 sequence, the incremented counter, and the current item onto the stack.
443 %\begin{opcodedesc}{LOAD_LOCAL}{namei}
444 %This opcode is obsolete.
447 \begin{opcodedesc
}{LOAD_GLOBAL
}{namei
}
448 Loads the global named
\code{co_names
[\var{namei
}]} onto the stack.
451 %\begin{opcodedesc}{SET_FUNC_ARGS}{argc}
452 %This opcode is obsolete.
455 \begin{opcodedesc
}{SETUP_LOOP
}{delta
}
456 Pushes a block for a loop onto the block stack. The block spans
457 from the current instruction with a size of
\var{delta
} bytes.
460 \begin{opcodedesc
}{SETUP_EXCEPT
}{delta
}
461 Pushes a try block from a try-except clause onto the block stack.
462 \var{delta
} points to the first except block.
465 \begin{opcodedesc
}{SETUP_FINALLY
}{delta
}
466 Pushes a try block from a try-except clause onto the block stack.
467 \var{delta
} points to the finally block.
470 \begin{opcodedesc
}{LOAD_FAST
}{var_num
}
471 Pushes a reference to the local
\code{co_varnames
[\var{var_num
}]} onto
475 \begin{opcodedesc
}{STORE_FAST
}{var_num
}
476 Stores TOS into the local
\code{co_varnames
[\var{var_num
}]}.
479 \begin{opcodedesc
}{DELETE_FAST
}{var_num
}
480 Deletes local
\code{co_varnames
[\var{var_num
}]}.
483 \begin{opcodedesc
}{SET_LINENO
}{lineno
}
484 Sets the current line number to
\var{lineno
}.
487 \begin{opcodedesc
}{RAISE_VARARGS
}{argc
}
488 Raises an exception.
\var{argc
} indicates the number of parameters
489 to the raise statement, ranging from
1 to
3. The handler will find
490 the traceback as TOS2, the parameter as TOS1, and the exception
494 \begin{opcodedesc
}{CALL_FUNCTION
}{argc
}
495 Calls a function. The low byte of
\var{argc
} indicates the number of
496 positional parameters, the high byte the number of keyword parameters.
497 On the stack, the opcode finds the keyword parameters first. For each
498 keyword argument, the value is on top of the key. Below the keyword
499 parameters, the positional parameters are on the stack, with the
500 right-most parameter on top. Below the parameters, the function object
501 to call is on the stack.
504 \begin{opcodedesc
}{MAKE_FUNCTION
}{argc
}
505 Pushes a new function object on the stack. TOS is the code associated
506 with the function. The function object is defined to have
\var{argc
}
507 default parameters, which are found below TOS.
510 \begin{opcodedesc
}{BUILD_SLICE
}{argc
}
511 Pushes a slice object on the stack.
\var{argc
} must be
2 or
3. If it
512 is
2,
\code{slice(TOS1, TOS)
} is pushed; if it is
3,
513 \code{slice(TOS2, TOS1, TOS)
} is pushed.
514 See the
\code{slice()
}\bifuncindex{slice
} built-in function.