1 \section{\module{dis
} ---
2 Disassembler for Python byte code
}
4 \declaremodule{standard
}{dis
}
5 \modulesynopsis{Disassembler for Python byte code.
}
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 and constants:
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 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 an 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 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
}{ROT_FOUR
}{}
134 Lifts second, third and forth stack item one position up, moves top down to
138 \begin{opcodedesc
}{DUP_TOP
}{}
139 Duplicates the reference on top of the stack.
142 Unary Operations take the top of the stack, apply the operation, and
143 push the result back on the stack.
145 \begin{opcodedesc
}{UNARY_POSITIVE
}{}
146 Implements
\code{TOS = +TOS
}.
149 \begin{opcodedesc
}{UNARY_NEGATIVE
}{}
150 Implements
\code{TOS = -TOS
}.
153 \begin{opcodedesc
}{UNARY_NOT
}{}
154 Implements
\code{TOS = not TOS
}.
157 \begin{opcodedesc
}{UNARY_CONVERT
}{}
158 Implements
\code{TOS = `TOS`
}.
161 \begin{opcodedesc
}{UNARY_INVERT
}{}
162 Implements
\code{TOS = \~
{}TOS
}.
165 Binary operations remove the top of the stack (TOS) and the second top-most
166 stack item (TOS1) from the stack. They perform the operation, and put the
167 result back on the stack.
169 \begin{opcodedesc
}{BINARY_POWER
}{}
170 Implements
\code{TOS = TOS1 ** TOS
}.
173 \begin{opcodedesc
}{BINARY_MULTIPLY
}{}
174 Implements
\code{TOS = TOS1 * TOS
}.
177 \begin{opcodedesc
}{BINARY_DIVIDE
}{}
178 Implements
\code{TOS = TOS1 / TOS
}.
181 \begin{opcodedesc
}{BINARY_MODULO
}{}
182 Implements
\code{TOS = TOS1 \%
{} TOS
}.
185 \begin{opcodedesc
}{BINARY_ADD
}{}
186 Implements
\code{TOS = TOS1 + TOS
}.
189 \begin{opcodedesc
}{BINARY_SUBTRACT
}{}
190 Implements
\code{TOS = TOS1 - TOS
}.
193 \begin{opcodedesc
}{BINARY_SUBSCR
}{}
194 Implements
\code{TOS = TOS1
[TOS
]}.
197 \begin{opcodedesc
}{BINARY_LSHIFT
}{}
198 Implements
\code{TOS = TOS1 <
\code{}< TOS
}.
201 \begin{opcodedesc
}{BINARY_RSHIFT
}{}
202 Implements
\code{TOS = TOS1 >
\code{}> TOS
}.
205 \begin{opcodedesc
}{BINARY_AND
}{}
206 Implements
\code{TOS = TOS1 \&\ TOS
}.
209 \begin{opcodedesc
}{BINARY_XOR
}{}
210 Implements
\code{TOS = TOS1 \^\ TOS
}.
213 \begin{opcodedesc
}{BINARY_OR
}{}
214 Implements
\code{TOS = TOS1 | TOS
}.
217 In-place operations are like binary operations, in that they remove TOS and
218 TOS1, and push the result back on the stack, but the operation is done
219 in-place when TOS1 supports it, and the resulting TOS may be (but does not
220 have to be) the original TOS1.
222 \begin{opcodedesc
}{INPLACE_POWER
}{}
223 Implements in-place
\code{TOS = TOS1 ** TOS
}.
226 \begin{opcodedesc
}{INPLACE_MULTIPLY
}{}
227 Implements in-place
\code{TOS = TOS1 * TOS
}.
230 \begin{opcodedesc
}{INPLACE_DIVIDE
}{}
231 Implements in-place
\code{TOS = TOS1 / TOS
}.
234 \begin{opcodedesc
}{INPLACE_MODULO
}{}
235 Implements in-place
\code{TOS = TOS1 \%
{} TOS
}.
238 \begin{opcodedesc
}{INPLACE_ADD
}{}
239 Implements in-place
\code{TOS = TOS1 + TOS
}.
242 \begin{opcodedesc
}{INPLACE_SUBTRACT
}{}
243 Implements in-place
\code{TOS = TOS1 - TOS
}.
246 \begin{opcodedesc
}{INPLACE_LSHIFT
}{}
247 Implements in-place
\code{TOS = TOS1 <
\code{}< TOS
}.
250 \begin{opcodedesc
}{INPLACE_RSHIFT
}{}
251 Implements in-place
\code{TOS = TOS1 >
\code{}> TOS
}.
254 \begin{opcodedesc
}{INPLACE_AND
}{}
255 Implements in-place
\code{TOS = TOS1 \&\ TOS
}.
258 \begin{opcodedesc
}{INPLACE_XOR
}{}
259 Implements in-place
\code{TOS = TOS1 \^\ TOS
}.
262 \begin{opcodedesc
}{INPLACE_OR
}{}
263 Implements in-place
\code{TOS = TOS1 | TOS
}.
266 The slice opcodes take up to three parameters.
268 \begin{opcodedesc
}{SLICE+
0}{}
269 Implements
\code{TOS = TOS
[:
]}.
272 \begin{opcodedesc
}{SLICE+
1}{}
273 Implements
\code{TOS = TOS1
[TOS:
]}.
276 \begin{opcodedesc
}{SLICE+
2}{}
277 Implements
\code{TOS = TOS1
[:TOS1
]}.
280 \begin{opcodedesc
}{SLICE+
3}{}
281 Implements
\code{TOS = TOS2
[TOS1:TOS
]}.
284 Slice assignment needs even an additional parameter. As any statement,
285 they put nothing on the stack.
287 \begin{opcodedesc
}{STORE_SLICE+
0}{}
288 Implements
\code{TOS
[:
] = TOS1
}.
291 \begin{opcodedesc
}{STORE_SLICE+
1}{}
292 Implements
\code{TOS1
[TOS:
] = TOS2
}.
295 \begin{opcodedesc
}{STORE_SLICE+
2}{}
296 Implements
\code{TOS1
[:TOS
] = TOS2
}.
299 \begin{opcodedesc
}{STORE_SLICE+
3}{}
300 Implements
\code{TOS2
[TOS1:TOS
] = TOS3
}.
303 \begin{opcodedesc
}{DELETE_SLICE+
0}{}
304 Implements
\code{del TOS
[:
]}.
307 \begin{opcodedesc
}{DELETE_SLICE+
1}{}
308 Implements
\code{del TOS1
[TOS:
]}.
311 \begin{opcodedesc
}{DELETE_SLICE+
2}{}
312 Implements
\code{del TOS1
[:TOS
]}.
315 \begin{opcodedesc
}{DELETE_SLICE+
3}{}
316 Implements
\code{del TOS2
[TOS1:TOS
]}.
319 \begin{opcodedesc
}{STORE_SUBSCR
}{}
320 Implements
\code{TOS1
[TOS
] = TOS2
}.
323 \begin{opcodedesc
}{DELETE_SUBSCR
}{}
324 Implements
\code{del TOS1
[TOS
]}.
327 \begin{opcodedesc
}{PRINT_EXPR
}{}
328 Implements the expression statement for the interactive mode. TOS is
329 removed from the stack and printed. In non-interactive mode, an
330 expression statement is terminated with
\code{POP_STACK
}.
333 \begin{opcodedesc
}{PRINT_ITEM
}{}
334 Prints TOS to the file-like object bound to
\code{sys.stdout
}. There
335 is one such instruction for each item in the
\keyword{print
} statement.
338 \begin{opcodedesc
}{PRINT_ITEM_TO
}{}
339 Like
\code{PRINT_ITEM
}, but prints the item second from TOS to the
340 file-like object at TOS. This is used by the extended print statement.
343 \begin{opcodedesc
}{PRINT_NEWLINE
}{}
344 Prints a new line on
\code{sys.stdout
}. This is generated as the
345 last operation of a
\keyword{print
} statement, unless the statement
349 \begin{opcodedesc
}{PRINT_NEWLINE_TO
}{}
350 Like
\code{PRINT_NEWLINE
}, but prints the new line on the file-like
351 object on the TOS. This is used by the extended print statement.
354 \begin{opcodedesc
}{BREAK_LOOP
}{}
355 Terminates a loop due to a
\keyword{break
} statement.
358 \begin{opcodedesc
}{LOAD_LOCALS
}{}
359 Pushes a reference to the locals of the current scope on the stack.
360 This is used in the code for a class definition: After the class body
361 is evaluated, the locals are passed to the class definition.
364 \begin{opcodedesc
}{RETURN_VALUE
}{}
365 Returns with TOS to the caller of the function.
368 \begin{opcodedesc
}{IMPORT_STAR
}{}
369 Loads all symbols not starting with
\character{_
} directly from the module TOS
370 to the local namespace. The module is popped after loading all names.
371 This opcode implements
\code{from module import *
}.
374 \begin{opcodedesc
}{EXEC_STMT
}{}
375 Implements
\code{exec TOS2,TOS1,TOS
}. The compiler fills
376 missing optional parameters with
\code{None
}.
379 \begin{opcodedesc
}{POP_BLOCK
}{}
380 Removes one block from the block stack. Per frame, there is a
381 stack of blocks, denoting nested loops, try statements, and such.
384 \begin{opcodedesc
}{END_FINALLY
}{}
385 Terminates a
\keyword{finally
} clause. The interpreter recalls
386 whether the exception has to be re-raised, or whether the function
387 returns, and continues with the outer-next block.
390 \begin{opcodedesc
}{BUILD_CLASS
}{}
391 Creates a new class object. TOS is the methods dictionary, TOS1
392 the tuple of the names of the base classes, and TOS2 the class name.
395 All of the following opcodes expect arguments. An argument is two
396 bytes, with the more significant byte last.
398 \begin{opcodedesc
}{STORE_NAME
}{namei
}
399 Implements
\code{name = TOS
}.
\var{namei
} is the index of
\var{name
}
400 in the attribute
\member{co_names
} of the code object.
401 The compiler tries to use
\code{STORE_LOCAL
} or
\code{STORE_GLOBAL
}
405 \begin{opcodedesc
}{DELETE_NAME
}{namei
}
406 Implements
\code{del name
}, where
\var{namei
} is the index into
407 \member{co_names
} attribute of the code object.
410 \begin{opcodedesc
}{UNPACK_SEQUENCE
}{count
}
411 Unpacks TOS into
\var{count
} individual values, which are put onto
412 the stack right-to-left.
415 %\begin{opcodedesc}{UNPACK_LIST}{count}
416 %This opcode is obsolete.
419 %\begin{opcodedesc}{UNPACK_ARG}{count}
420 %This opcode is obsolete.
423 \begin{opcodedesc
}{DUP_TOPX
}{count
}
424 Duplicate
\var{count
} items, keeping them in the same order. Due to
425 implementation limits,
\var{count
} should be between
1 and
5 inclusive.
428 \begin{opcodedesc
}{STORE_ATTR
}{namei
}
429 Implements
\code{TOS.name = TOS1
}, where
\var{namei
} is the index
430 of name in
\member{co_names
}.
433 \begin{opcodedesc
}{DELETE_ATTR
}{namei
}
434 Implements
\code{del TOS.name
}, using
\var{namei
} as index into
438 \begin{opcodedesc
}{STORE_GLOBAL
}{namei
}
439 Works as
\code{STORE_NAME
}, but stores the name as a global.
442 \begin{opcodedesc
}{DELETE_GLOBAL
}{namei
}
443 Works as
\code{DELETE_NAME
}, but deletes a global name.
446 %\begin{opcodedesc}{UNPACK_VARARG}{argc}
447 %This opcode is obsolete.
450 \begin{opcodedesc
}{LOAD_CONST
}{consti
}
451 Pushes
\samp{co_consts
[\var{consti
}]} onto the stack.
454 \begin{opcodedesc
}{LOAD_NAME
}{namei
}
455 Pushes the value associated with
\samp{co_names
[\var{namei
}]} onto the stack.
458 \begin{opcodedesc
}{BUILD_TUPLE
}{count
}
459 Creates a tuple consuming
\var{count
} items from the stack, and pushes
460 the resulting tuple onto the stack.
463 \begin{opcodedesc
}{BUILD_LIST
}{count
}
464 Works as
\code{BUILD_TUPLE
}, but creates a list.
467 \begin{opcodedesc
}{BUILD_MAP
}{zero
}
468 Pushes a new empty dictionary object onto the stack. The argument is
469 ignored and set to zero by the compiler.
472 \begin{opcodedesc
}{LOAD_ATTR
}{namei
}
473 Replaces TOS with
\code{getattr(TOS, co_names
[\var{namei
}]}.
476 \begin{opcodedesc
}{COMPARE_OP
}{opname
}
477 Performs a boolean operation. The operation name can be found
478 in
\code{cmp_op
[\var{opname
}]}.
481 \begin{opcodedesc
}{IMPORT_NAME
}{namei
}
482 Imports the module
\code{co_names
[\var{namei
}]}. The module object is
483 pushed onto the stack. The current namespace is not affected: for a
484 proper import statement, a subsequent
\code{STORE_FAST
} instruction
485 modifies the namespace.
488 \begin{opcodedesc
}{IMPORT_FROM
}{namei
}
489 Loads the attribute
\code{co_names
[\var{namei
}]} from the module found in
490 TOS. The resulting object is pushed onto the stack, to be subsequently
491 stored by a
\code{STORE_FAST
} instruction.
494 \begin{opcodedesc
}{JUMP_FORWARD
}{delta
}
495 Increments byte code counter by
\var{delta
}.
498 \begin{opcodedesc
}{JUMP_IF_TRUE
}{delta
}
499 If TOS is true, increment the byte code counter by
\var{delta
}. TOS is
503 \begin{opcodedesc
}{JUMP_IF_FALSE
}{delta
}
504 If TOS is false, increment the byte code counter by
\var{delta
}. TOS
508 \begin{opcodedesc
}{JUMP_ABSOLUTE
}{target
}
509 Set byte code counter to
\var{target
}.
512 \begin{opcodedesc
}{FOR_LOOP
}{delta
}
513 Iterate over a sequence. TOS is the current index, TOS1 the sequence.
514 First, the next element is computed. If the sequence is exhausted,
515 increment byte code counter by
\var{delta
}. Otherwise, push the
516 sequence, the incremented counter, and the current item onto the stack.
519 %\begin{opcodedesc}{LOAD_LOCAL}{namei}
520 %This opcode is obsolete.
523 \begin{opcodedesc
}{LOAD_GLOBAL
}{namei
}
524 Loads the global named
\code{co_names
[\var{namei
}]} onto the stack.
527 %\begin{opcodedesc}{SET_FUNC_ARGS}{argc}
528 %This opcode is obsolete.
531 \begin{opcodedesc
}{SETUP_LOOP
}{delta
}
532 Pushes a block for a loop onto the block stack. The block spans
533 from the current instruction with a size of
\var{delta
} bytes.
536 \begin{opcodedesc
}{SETUP_EXCEPT
}{delta
}
537 Pushes a try block from a try-except clause onto the block stack.
538 \var{delta
} points to the first except block.
541 \begin{opcodedesc
}{SETUP_FINALLY
}{delta
}
542 Pushes a try block from a try-except clause onto the block stack.
543 \var{delta
} points to the finally block.
546 \begin{opcodedesc
}{LOAD_FAST
}{var_num
}
547 Pushes a reference to the local
\code{co_varnames
[\var{var_num
}]} onto
551 \begin{opcodedesc
}{STORE_FAST
}{var_num
}
552 Stores TOS into the local
\code{co_varnames
[\var{var_num
}]}.
555 \begin{opcodedesc
}{DELETE_FAST
}{var_num
}
556 Deletes local
\code{co_varnames
[\var{var_num
}]}.
559 \begin{opcodedesc
}{SET_LINENO
}{lineno
}
560 Sets the current line number to
\var{lineno
}.
563 \begin{opcodedesc
}{RAISE_VARARGS
}{argc
}
564 Raises an exception.
\var{argc
} indicates the number of parameters
565 to the raise statement, ranging from
0 to
3. The handler will find
566 the traceback as TOS2, the parameter as TOS1, and the exception
570 \begin{opcodedesc
}{CALL_FUNCTION
}{argc
}
571 Calls a function. The low byte of
\var{argc
} indicates the number of
572 positional parameters, the high byte the number of keyword parameters.
573 On the stack, the opcode finds the keyword parameters first. For each
574 keyword argument, the value is on top of the key. Below the keyword
575 parameters, the positional parameters are on the stack, with the
576 right-most parameter on top. Below the parameters, the function object
577 to call is on the stack.
580 \begin{opcodedesc
}{MAKE_FUNCTION
}{argc
}
581 Pushes a new function object on the stack. TOS is the code associated
582 with the function. The function object is defined to have
\var{argc
}
583 default parameters, which are found below TOS.
586 \begin{opcodedesc
}{BUILD_SLICE
}{argc
}
587 Pushes a slice object on the stack.
\var{argc
} must be
2 or
3. If it
588 is
2,
\code{slice(TOS1, TOS)
} is pushed; if it is
3,
589 \code{slice(TOS2, TOS1, TOS)
} is pushed.
590 See the
\code{slice()
}\bifuncindex{slice
} built-in function for more
594 \begin{opcodedesc
}{EXTENDED_ARG
}{ext
}
595 Prefixes any opcode which has an argument too big to fit into the
596 default two bytes.
\var{ext
} holds two additional bytes which, taken
597 together with the subsequent opcode's argument, comprise a four-byte
598 argument,
\var{ext
} being the two most-significant bytes.
601 \begin{opcodedesc
}{CALL_FUNCTION_VAR
}{argc
}
602 Calls a function.
\var{argc
} is interpreted as in
\code{CALL_FUNCTION
}.
603 The top element on the stack contains the variable argument list, followed
604 by keyword and positional arguments.
607 \begin{opcodedesc
}{CALL_FUNCTION_KW
}{argc
}
608 Calls a function.
\var{argc
} is interpreted as in
\code{CALL_FUNCTION
}.
609 The top element on the stack contains the keyword arguments dictionary,
610 followed by explicit keyword and positional arguments.
613 \begin{opcodedesc
}{CALL_FUNCTION_VAR_KW
}{argc
}
614 Calls a function.
\var{argc
} is interpreted as in
615 \code{CALL_FUNCTION
}. The top element on the stack contains the
616 keyword arguments dictionary, followed by the variable-arguments
617 tuple, followed by explicit keyword and positional arguments.