/Doc/library/dis.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 752 lines · 364 code · 388 blank · 0 comment · 0 complexity · 0119c386e55ac2b53a87a804231e9f2c MD5 · raw file

  1. :mod:`dis` --- Disassembler for Python bytecode
  2. ===============================================
  3. .. module:: dis
  4. :synopsis: Disassembler for Python bytecode.
  5. The :mod:`dis` module supports the analysis of Python :term:`bytecode` by disassembling
  6. it. Since there is no Python assembler, this module defines the Python assembly
  7. language. The Python bytecode which this module takes as an input is defined
  8. in the file :file:`Include/opcode.h` and used by the compiler and the
  9. interpreter.
  10. Example: Given the function :func:`myfunc`::
  11. def myfunc(alist):
  12. return len(alist)
  13. the following command can be used to get the disassembly of :func:`myfunc`::
  14. >>> dis.dis(myfunc)
  15. 2 0 LOAD_GLOBAL 0 (len)
  16. 3 LOAD_FAST 0 (alist)
  17. 6 CALL_FUNCTION 1
  18. 9 RETURN_VALUE
  19. (The "2" is a line number).
  20. The :mod:`dis` module defines the following functions and constants:
  21. .. function:: dis([bytesource])
  22. Disassemble the *bytesource* object. *bytesource* can denote either a module, a
  23. class, a method, a function, or a code object. For a module, it disassembles
  24. all functions. For a class, it disassembles all methods. For a single code
  25. sequence, it prints one line per bytecode instruction. If no object is
  26. provided, it disassembles the last traceback.
  27. .. function:: distb([tb])
  28. Disassembles the top-of-stack function of a traceback, using the last traceback
  29. if none was passed. The instruction causing the exception is indicated.
  30. .. function:: disassemble(code[, lasti])
  31. Disassembles a code object, indicating the last instruction if *lasti* was
  32. provided. The output is divided in the following columns:
  33. #. the line number, for the first instruction of each line
  34. #. the current instruction, indicated as ``-->``,
  35. #. a labelled instruction, indicated with ``>>``,
  36. #. the address of the instruction,
  37. #. the operation code name,
  38. #. operation parameters, and
  39. #. interpretation of the parameters in parentheses.
  40. The parameter interpretation recognizes local and global variable names,
  41. constant values, branch targets, and compare operators.
  42. .. function:: disco(code[, lasti])
  43. A synonym for disassemble. It is more convenient to type, and kept for
  44. compatibility with earlier Python releases.
  45. .. data:: opname
  46. Sequence of operation names, indexable using the bytecode.
  47. .. data:: opmap
  48. Dictionary mapping bytecodes to operation names.
  49. .. data:: cmp_op
  50. Sequence of all compare operation names.
  51. .. data:: hasconst
  52. Sequence of bytecodes that have a constant parameter.
  53. .. data:: hasfree
  54. Sequence of bytecodes that access a free variable.
  55. .. data:: hasname
  56. Sequence of bytecodes that access an attribute by name.
  57. .. data:: hasjrel
  58. Sequence of bytecodes that have a relative jump target.
  59. .. data:: hasjabs
  60. Sequence of bytecodes that have an absolute jump target.
  61. .. data:: haslocal
  62. Sequence of bytecodes that access a local variable.
  63. .. data:: hascompare
  64. Sequence of bytecodes of Boolean operations.
  65. .. _bytecodes:
  66. Python Bytecode Instructions
  67. ----------------------------
  68. The Python compiler currently generates the following bytecode instructions.
  69. .. opcode:: STOP_CODE ()
  70. Indicates end-of-code to the compiler, not used by the interpreter.
  71. .. opcode:: NOP ()
  72. Do nothing code. Used as a placeholder by the bytecode optimizer.
  73. .. opcode:: POP_TOP ()
  74. Removes the top-of-stack (TOS) item.
  75. .. opcode:: ROT_TWO ()
  76. Swaps the two top-most stack items.
  77. .. opcode:: ROT_THREE ()
  78. Lifts second and third stack item one position up, moves top down to position
  79. three.
  80. .. opcode:: ROT_FOUR ()
  81. Lifts second, third and forth stack item one position up, moves top down to
  82. position four.
  83. .. opcode:: DUP_TOP ()
  84. Duplicates the reference on top of the stack.
  85. Unary Operations take the top of the stack, apply the operation, and push the
  86. result back on the stack.
  87. .. opcode:: UNARY_POSITIVE ()
  88. Implements ``TOS = +TOS``.
  89. .. opcode:: UNARY_NEGATIVE ()
  90. Implements ``TOS = -TOS``.
  91. .. opcode:: UNARY_NOT ()
  92. Implements ``TOS = not TOS``.
  93. .. opcode:: UNARY_CONVERT ()
  94. Implements ``TOS = `TOS```.
  95. .. opcode:: UNARY_INVERT ()
  96. Implements ``TOS = ~TOS``.
  97. .. opcode:: GET_ITER ()
  98. Implements ``TOS = iter(TOS)``.
  99. Binary operations remove the top of the stack (TOS) and the second top-most
  100. stack item (TOS1) from the stack. They perform the operation, and put the
  101. result back on the stack.
  102. .. opcode:: BINARY_POWER ()
  103. Implements ``TOS = TOS1 ** TOS``.
  104. .. opcode:: BINARY_MULTIPLY ()
  105. Implements ``TOS = TOS1 * TOS``.
  106. .. opcode:: BINARY_DIVIDE ()
  107. Implements ``TOS = TOS1 / TOS`` when ``from __future__ import division`` is not
  108. in effect.
  109. .. opcode:: BINARY_FLOOR_DIVIDE ()
  110. Implements ``TOS = TOS1 // TOS``.
  111. .. opcode:: BINARY_TRUE_DIVIDE ()
  112. Implements ``TOS = TOS1 / TOS`` when ``from __future__ import division`` is in
  113. effect.
  114. .. opcode:: BINARY_MODULO ()
  115. Implements ``TOS = TOS1 % TOS``.
  116. .. opcode:: BINARY_ADD ()
  117. Implements ``TOS = TOS1 + TOS``.
  118. .. opcode:: BINARY_SUBTRACT ()
  119. Implements ``TOS = TOS1 - TOS``.
  120. .. opcode:: BINARY_SUBSCR ()
  121. Implements ``TOS = TOS1[TOS]``.
  122. .. opcode:: BINARY_LSHIFT ()
  123. Implements ``TOS = TOS1 << TOS``.
  124. .. opcode:: BINARY_RSHIFT ()
  125. Implements ``TOS = TOS1 >> TOS``.
  126. .. opcode:: BINARY_AND ()
  127. Implements ``TOS = TOS1 & TOS``.
  128. .. opcode:: BINARY_XOR ()
  129. Implements ``TOS = TOS1 ^ TOS``.
  130. .. opcode:: BINARY_OR ()
  131. Implements ``TOS = TOS1 | TOS``.
  132. In-place operations are like binary operations, in that they remove TOS and
  133. TOS1, and push the result back on the stack, but the operation is done in-place
  134. when TOS1 supports it, and the resulting TOS may be (but does not have to be)
  135. the original TOS1.
  136. .. opcode:: INPLACE_POWER ()
  137. Implements in-place ``TOS = TOS1 ** TOS``.
  138. .. opcode:: INPLACE_MULTIPLY ()
  139. Implements in-place ``TOS = TOS1 * TOS``.
  140. .. opcode:: INPLACE_DIVIDE ()
  141. Implements in-place ``TOS = TOS1 / TOS`` when ``from __future__ import
  142. division`` is not in effect.
  143. .. opcode:: INPLACE_FLOOR_DIVIDE ()
  144. Implements in-place ``TOS = TOS1 // TOS``.
  145. .. opcode:: INPLACE_TRUE_DIVIDE ()
  146. Implements in-place ``TOS = TOS1 / TOS`` when ``from __future__ import
  147. division`` is in effect.
  148. .. opcode:: INPLACE_MODULO ()
  149. Implements in-place ``TOS = TOS1 % TOS``.
  150. .. opcode:: INPLACE_ADD ()
  151. Implements in-place ``TOS = TOS1 + TOS``.
  152. .. opcode:: INPLACE_SUBTRACT ()
  153. Implements in-place ``TOS = TOS1 - TOS``.
  154. .. opcode:: INPLACE_LSHIFT ()
  155. Implements in-place ``TOS = TOS1 << TOS``.
  156. .. opcode:: INPLACE_RSHIFT ()
  157. Implements in-place ``TOS = TOS1 >> TOS``.
  158. .. opcode:: INPLACE_AND ()
  159. Implements in-place ``TOS = TOS1 & TOS``.
  160. .. opcode:: INPLACE_XOR ()
  161. Implements in-place ``TOS = TOS1 ^ TOS``.
  162. .. opcode:: INPLACE_OR ()
  163. Implements in-place ``TOS = TOS1 | TOS``.
  164. The slice opcodes take up to three parameters.
  165. .. opcode:: SLICE_NONE ()
  166. Implements ``TOS = TOS[:]``.
  167. .. opcode:: SLICE_LEFT ()
  168. Implements ``TOS = TOS1[TOS:]``.
  169. .. opcode:: SLICE_RIGHT ()
  170. Implements ``TOS = TOS1[:TOS]``.
  171. .. opcode:: SLICE_BOTH ()
  172. Implements ``TOS = TOS2[TOS1:TOS]``.
  173. Slice assignment needs even an additional parameter. As any statement, they put
  174. nothing on the stack.
  175. .. opcode:: STORE_SLICE_NONE ()
  176. Implements ``TOS[:] = TOS1``.
  177. .. opcode:: STORE_SLICE_LEFT ()
  178. Implements ``TOS1[TOS:] = TOS2``.
  179. .. opcode:: STORE_SLICE_RIGHT ()
  180. Implements ``TOS1[:TOS] = TOS2``.
  181. .. opcode:: STORE_SLICE_BOTH ()
  182. Implements ``TOS2[TOS1:TOS] = TOS3``.
  183. .. opcode:: DELETE_SLICE_NONE ()
  184. Implements ``del TOS[:]``.
  185. .. opcode:: DELETE_SLICE_LEFT ()
  186. Implements ``del TOS1[TOS:]``.
  187. .. opcode:: DELETE_SLICE_RIGHT ()
  188. Implements ``del TOS1[:TOS]``.
  189. .. opcode:: DELETE_SLICE_BOTH ()
  190. Implements ``del TOS2[TOS1:TOS]``.
  191. .. opcode:: STORE_SUBSCR ()
  192. Implements ``TOS1[TOS] = TOS2``.
  193. .. opcode:: DELETE_SUBSCR ()
  194. Implements ``del TOS1[TOS]``.
  195. Miscellaneous opcodes.
  196. .. opcode:: BREAK_LOOP ()
  197. Terminates a loop due to a :keyword:`break` statement.
  198. .. opcode:: CONTINUE_LOOP (target)
  199. Continues a loop due to a :keyword:`continue` statement. *target* is the
  200. address to jump to (which should be a ``FOR_ITER`` instruction).
  201. .. opcode:: LIST_APPEND ()
  202. Calls ``list.append(TOS1, TOS)``. Used to implement list comprehensions.
  203. .. opcode:: RETURN_VALUE ()
  204. Returns with TOS to the caller of the function.
  205. .. opcode:: YIELD_VALUE ()
  206. Pops ``TOS`` and yields it from a :term:`generator`.
  207. .. opcode:: POP_BLOCK ()
  208. Removes one block from the block stack. Per frame, there is a stack of blocks,
  209. denoting nested loops, try statements, and such.
  210. .. opcode:: END_FINALLY ()
  211. Terminates a :keyword:`finally` clause. The interpreter recalls whether the
  212. exception has to be re-raised, or whether the function returns, and continues
  213. with the outer-next block.
  214. .. opcode:: WITH_CLEANUP ()
  215. Cleans up the stack when a :keyword:`with` statement block exits. On top of
  216. the stack are 1--3 values indicating how/why the finally clause was entered:
  217. * TOP = ``None``
  218. * (TOP, SECOND) = (``WHY_{RETURN,CONTINUE}``), retval
  219. * TOP = ``WHY_*``; no retval below it
  220. * (TOP, SECOND, THIRD) = exc_info()
  221. Under them is EXIT, the context manager's :meth:`__exit__` bound method.
  222. In the last case, ``EXIT(TOP, SECOND, THIRD)`` is called, otherwise
  223. ``EXIT(None, None, None)``.
  224. EXIT is removed from the stack, leaving the values above it in the same
  225. order. In addition, if the stack represents an exception, *and* the function
  226. call returns a 'true' value, this information is "zapped", to prevent
  227. ``END_FINALLY`` from re-raising the exception. (But non-local gotos should
  228. still be resumed.)
  229. .. XXX explain the WHY stuff!
  230. All of the following opcodes expect arguments. An argument is 31 bits.
  231. .. opcode:: STORE_NAME (namei)
  232. Implements ``name = TOS``. *namei* is the index of *name* in the attribute
  233. :attr:`co_names` of the code object. The compiler tries to use ``STORE_FAST``
  234. or ``STORE_GLOBAL`` if possible.
  235. .. opcode:: DELETE_NAME (namei)
  236. Implements ``del name``, where *namei* is the index into :attr:`co_names`
  237. attribute of the code object.
  238. .. opcode:: UNPACK_SEQUENCE (count)
  239. Unpacks TOS into *count* individual values, which are put onto the stack
  240. right-to-left.
  241. .. opcode:: DUP_TOP_TWO ()
  242. Duplicate 2 items, keeping them in the same order.
  243. .. opcode:: DUP_TOP_THREE ()
  244. Duplicate 3 items, keeping them in the same order.
  245. .. opcode:: STORE_ATTR (namei)
  246. Implements ``TOS.name = TOS1``, where *namei* is the index of name in
  247. :attr:`co_names`.
  248. .. opcode:: DELETE_ATTR (namei)
  249. Implements ``del TOS.name``, using *namei* as index into :attr:`co_names`.
  250. .. opcode:: STORE_GLOBAL (namei)
  251. Works as ``STORE_NAME``, but stores the name as a global.
  252. .. opcode:: DELETE_GLOBAL (namei)
  253. Works as ``DELETE_NAME``, but deletes a global name.
  254. .. opcode:: LOAD_CONST (consti)
  255. Pushes ``co_consts[consti]`` onto the stack.
  256. .. opcode:: LOAD_NAME (namei)
  257. Pushes the value associated with ``co_names[namei]`` onto the stack.
  258. .. opcode:: BUILD_TUPLE (count)
  259. Creates a tuple consuming *count* items from the stack, and pushes the resulting
  260. tuple onto the stack.
  261. .. opcode:: BUILD_LIST (count)
  262. Works as ``BUILD_TUPLE``, but creates a list.
  263. .. opcode:: BUILD_MAP (count)
  264. Pushes a new dictionary object onto the stack. The dictionary is pre-sized
  265. to hold *count* entries.
  266. .. opcode:: LOAD_ATTR (namei)
  267. Replaces TOS with ``getattr(TOS, co_names[namei])``.
  268. .. opcode:: COMPARE_OP (opname)
  269. Performs a Boolean operation. The operation name can be found in
  270. ``cmp_op[opname]``.
  271. .. opcode:: JUMP_FORWARD (delta)
  272. Increments bytecode counter by *delta*.
  273. .. opcode:: POP_JUMP_IF_TRUE (target)
  274. If TOS is true, sets the bytecode counter to *target*. TOS is popped.
  275. .. opcode:: POP_JUMP_IF_FALSE (target)
  276. If TOS is false, sets the bytecode counter to *target*. TOS is popped.
  277. .. opcode:: JUMP_IF_TRUE_OR_POP (target)
  278. If TOS is true, sets the bytecode counter to *target* and leaves TOS
  279. on the stack. Otherwise (TOS is false), TOS is popped.
  280. .. opcode:: JUMP_IF_FALSE_OR_POP (target)
  281. If TOS is false, sets the bytecode counter to *target* and leaves
  282. TOS on the stack. Otherwise (TOS is true), TOS is popped.
  283. .. opcode:: JUMP_ABSOLUTE (target)
  284. Set bytecode counter to *target*.
  285. .. opcode:: FOR_ITER (delta)
  286. ``TOS`` is an :term:`iterator`. Call its :meth:`next` method. If this
  287. yields a new value, push it on the stack (leaving the iterator below it). If
  288. the iterator indicates it is exhausted ``TOS`` is popped, and the bytecode
  289. counter is incremented by *delta*.
  290. .. opcode:: LOAD_GLOBAL (namei)
  291. Loads the global named ``co_names[namei]`` onto the stack.
  292. .. opcode:: SETUP_LOOP (delta)
  293. Pushes a block for a loop onto the block stack. The block spans from the
  294. current instruction with a size of *delta* bytes.
  295. .. opcode:: SETUP_EXCEPT (delta)
  296. Pushes a try block from a try-except clause onto the block stack. *delta* points
  297. to the first except block.
  298. .. opcode:: SETUP_FINALLY (delta)
  299. Pushes a try block from a try-except clause onto the block stack. *delta* points
  300. to the finally block.
  301. .. opcode:: STORE_MAP ()
  302. Store a key and value pair in a dictionary. Pops the key and value while leaving
  303. the dictionary on the stack.
  304. .. opcode:: LOAD_FAST (var_num)
  305. Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
  306. .. opcode:: STORE_FAST (var_num)
  307. Stores TOS into the local ``co_varnames[var_num]``.
  308. .. opcode:: DELETE_FAST (var_num)
  309. Deletes local ``co_varnames[var_num]``.
  310. .. opcode:: LOAD_CLOSURE (i)
  311. Pushes a reference to the cell contained in slot *i* of the cell and free
  312. variable storage. The name of the variable is ``co_cellvars[i]`` if *i* is
  313. less than the length of *co_cellvars*. Otherwise it is ``co_freevars[i -
  314. len(co_cellvars)]``.
  315. .. opcode:: LOAD_DEREF (i)
  316. Loads the cell contained in slot *i* of the cell and free variable storage.
  317. Pushes a reference to the object the cell contains on the stack.
  318. .. opcode:: STORE_DEREF (i)
  319. Stores TOS into the cell contained in slot *i* of the cell and free variable
  320. storage.
  321. .. opcode:: SET_LINENO (lineno)
  322. This opcode is obsolete.
  323. .. opcode:: RAISE_VARARGS_XXX ()
  324. Raises an exception. *XXX* is *ZERO*, *ONE*, *TWO*, or *THREE* and
  325. indicates the number of parameters to the raise statement. The
  326. handler will find the traceback as TOS2, the parameter as TOS1, and
  327. the exception as TOS.
  328. .. opcode:: CALL_FUNCTION (argc)
  329. Calls a function. The low byte of *argc* indicates the number of positional
  330. parameters, the high byte the number of keyword parameters. On the stack, the
  331. opcode finds the keyword parameters first. For each keyword argument, the value
  332. is on top of the key. Below the keyword parameters, the positional parameters
  333. are on the stack, with the right-most parameter on top. Below the parameters,
  334. the function object to call is on the stack. Pops all function arguments, and
  335. the function itself off the stack, and pushes the return value.
  336. .. opcode:: MAKE_CLOSURE (argc)
  337. Creates a new function object, sets its *func_closure* slot, and pushes it on
  338. the stack. TOS is the code associated with the function, TOS1 the tuple
  339. containing cells for the closure's free variables. The function also has
  340. *argc* default parameters, which are found below the cells.
  341. .. opcode:: BUILD_SLICE_TWO ()
  342. .. index:: builtin: slice
  343. Pushes ``slice(TOS1, TOS)`` on the stack. See the :func:`slice`
  344. built-in function for more information.
  345. .. opcode:: BUILD_SLICE_THREE ()
  346. .. index:: builtin: slice
  347. Pushes ``slice(TOS2, TOS1, TOS)`` on the stack. See the
  348. :func:`slice` built-in function for more information.
  349. .. opcode:: CALL_FUNCTION_VAR (argc)
  350. Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
  351. on the stack contains the variable argument list, followed by keyword and
  352. positional arguments.
  353. .. opcode:: CALL_FUNCTION_KW (argc)
  354. Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top element
  355. on the stack contains the keyword arguments dictionary, followed by explicit
  356. keyword and positional arguments.
  357. .. opcode:: CALL_FUNCTION_VAR_KW (argc)
  358. Calls a function. *argc* is interpreted as in ``CALL_FUNCTION``. The top
  359. element on the stack contains the keyword arguments dictionary, followed by the
  360. variable-arguments tuple, followed by explicit keyword and positional arguments.
  361. .. opcode:: HAVE_ARGUMENT ()
  362. This is not really an opcode. It identifies the dividing line between opcodes
  363. which don't take arguments ``< HAVE_ARGUMENT`` and those which do ``>=
  364. HAVE_ARGUMENT``.