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.
|
---|
85 | \end{datadesc}
|
---|
86 |
|
---|
87 | \begin{datadesc}{hasconst}
|
---|
88 | Sequence of byte codes that have a constant parameter.
|
---|
89 | \end{datadesc}
|
---|
90 |
|
---|
91 | \begin{datadesc}{hasfree}
|
---|
92 | Sequence of byte codes that access a free variable.
|
---|
93 | \end{datadesc}
|
---|
94 |
|
---|
95 | \begin{datadesc}{hasname}
|
---|
96 | Sequence of byte codes that access an attribute by name.
|
---|
97 | \end{datadesc}
|
---|
98 |
|
---|
99 | \begin{datadesc}{hasjrel}
|
---|
100 | Sequence of byte codes that have a relative jump target.
|
---|
101 | \end{datadesc}
|
---|
102 |
|
---|
103 | \begin{datadesc}{hasjabs}
|
---|
104 | Sequence of byte codes that have an absolute jump target.
|
---|
105 | \end{datadesc}
|
---|
106 |
|
---|
107 | \begin{datadesc}{haslocal}
|
---|
108 | Sequence of byte codes that access a local variable.
|
---|
109 | \end{datadesc}
|
---|
110 |
|
---|
111 | \begin{datadesc}{hascompare}
|
---|
112 | Sequence of byte codes of Boolean operations.
|
---|
113 | \end{datadesc}
|
---|
114 |
|
---|
115 | \subsection{Python Byte Code Instructions}
|
---|
116 | \label{bytecodes}
|
---|
117 |
|
---|
118 | The Python compiler currently generates the following byte code
|
---|
119 | instructions.
|
---|
120 |
|
---|
121 | \setindexsubitem{(byte code insns)}
|
---|
122 |
|
---|
123 | \begin{opcodedesc}{STOP_CODE}{}
|
---|
124 | Indicates end-of-code to the compiler, not used by the interpreter.
|
---|
125 | \end{opcodedesc}
|
---|
126 |
|
---|
127 | \begin{opcodedesc}{NOP}{}
|
---|
128 | Do nothing code. Used as a placeholder by the bytecode optimizer.
|
---|
129 | \end{opcodedesc}
|
---|
130 |
|
---|
131 | \begin{opcodedesc}{POP_TOP}{}
|
---|
132 | Removes the top-of-stack (TOS) item.
|
---|
133 | \end{opcodedesc}
|
---|
134 |
|
---|
135 | \begin{opcodedesc}{ROT_TWO}{}
|
---|
136 | Swaps the two top-most stack items.
|
---|
137 | \end{opcodedesc}
|
---|
138 |
|
---|
139 | \begin{opcodedesc}{ROT_THREE}{}
|
---|
140 | Lifts second and third stack item one position up, moves top down
|
---|
141 | to position three.
|
---|
142 | \end{opcodedesc}
|
---|
143 |
|
---|
144 | \begin{opcodedesc}{ROT_FOUR}{}
|
---|
145 | Lifts second, third and forth stack item one position up, moves top down to
|
---|
146 | position four.
|
---|
147 | \end{opcodedesc}
|
---|
148 |
|
---|
149 | \begin{opcodedesc}{DUP_TOP}{}
|
---|
150 | Duplicates the reference on top of the stack.
|
---|
151 | \end{opcodedesc}
|
---|
152 |
|
---|
153 | Unary Operations take the top of the stack, apply the operation, and
|
---|
154 | push the result back on the stack.
|
---|
155 |
|
---|
156 | \begin{opcodedesc}{UNARY_POSITIVE}{}
|
---|
157 | Implements \code{TOS = +TOS}.
|
---|
158 | \end{opcodedesc}
|
---|
159 |
|
---|
160 | \begin{opcodedesc}{UNARY_NEGATIVE}{}
|
---|
161 | Implements \code{TOS = -TOS}.
|
---|
162 | \end{opcodedesc}
|
---|
163 |
|
---|
164 | \begin{opcodedesc}{UNARY_NOT}{}
|
---|
165 | Implements \code{TOS = not TOS}.
|
---|
166 | \end{opcodedesc}
|
---|
167 |
|
---|
168 | \begin{opcodedesc}{UNARY_CONVERT}{}
|
---|
169 | Implements \code{TOS = `TOS`}.
|
---|
170 | \end{opcodedesc}
|
---|
171 |
|
---|
172 | \begin{opcodedesc}{UNARY_INVERT}{}
|
---|
173 | Implements \code{TOS = \~{}TOS}.
|
---|
174 | \end{opcodedesc}
|
---|
175 |
|
---|
176 | \begin{opcodedesc}{GET_ITER}{}
|
---|
177 | Implements \code{TOS = iter(TOS)}.
|
---|
178 | \end{opcodedesc}
|
---|
179 |
|
---|
180 | Binary operations remove the top of the stack (TOS) and the second top-most
|
---|
181 | stack item (TOS1) from the stack. They perform the operation, and put the
|
---|
182 | result back on the stack.
|
---|
183 |
|
---|
184 | \begin{opcodedesc}{BINARY_POWER}{}
|
---|
185 | Implements \code{TOS = TOS1 ** TOS}.
|
---|
186 | \end{opcodedesc}
|
---|
187 |
|
---|
188 | \begin{opcodedesc}{BINARY_MULTIPLY}{}
|
---|
189 | Implements \code{TOS = TOS1 * TOS}.
|
---|
190 | \end{opcodedesc}
|
---|
191 |
|
---|
192 | \begin{opcodedesc}{BINARY_DIVIDE}{}
|
---|
193 | Implements \code{TOS = TOS1 / TOS} when
|
---|
194 | \code{from __future__ import division} is not in effect.
|
---|
195 | \end{opcodedesc}
|
---|
196 |
|
---|
197 | \begin{opcodedesc}{BINARY_FLOOR_DIVIDE}{}
|
---|
198 | Implements \code{TOS = TOS1 // TOS}.
|
---|
199 | \end{opcodedesc}
|
---|
200 |
|
---|
201 | \begin{opcodedesc}{BINARY_TRUE_DIVIDE}{}
|
---|
202 | Implements \code{TOS = TOS1 / TOS} when
|
---|
203 | \code{from __future__ import division} is in effect.
|
---|
204 | \end{opcodedesc}
|
---|
205 |
|
---|
206 | \begin{opcodedesc}{BINARY_MODULO}{}
|
---|
207 | Implements \code{TOS = TOS1 \%{} TOS}.
|
---|
208 | \end{opcodedesc}
|
---|
209 |
|
---|
210 | \begin{opcodedesc}{BINARY_ADD}{}
|
---|
211 | Implements \code{TOS = TOS1 + TOS}.
|
---|
212 | \end{opcodedesc}
|
---|
213 |
|
---|
214 | \begin{opcodedesc}{BINARY_SUBTRACT}{}
|
---|
215 | Implements \code{TOS = TOS1 - TOS}.
|
---|
216 | \end{opcodedesc}
|
---|
217 |
|
---|
218 | \begin{opcodedesc}{BINARY_SUBSCR}{}
|
---|
219 | Implements \code{TOS = TOS1[TOS]}.
|
---|
220 | \end{opcodedesc}
|
---|
221 |
|
---|
222 | \begin{opcodedesc}{BINARY_LSHIFT}{}
|
---|
223 | Implements \code{TOS = TOS1 <\code{}< TOS}.
|
---|
224 | \end{opcodedesc}
|
---|
225 |
|
---|
226 | \begin{opcodedesc}{BINARY_RSHIFT}{}
|
---|
227 | Implements \code{TOS = TOS1 >\code{}> TOS}.
|
---|
228 | \end{opcodedesc}
|
---|
229 |
|
---|
230 | \begin{opcodedesc}{BINARY_AND}{}
|
---|
231 | Implements \code{TOS = TOS1 \&\ TOS}.
|
---|
232 | \end{opcodedesc}
|
---|
233 |
|
---|
234 | \begin{opcodedesc}{BINARY_XOR}{}
|
---|
235 | Implements \code{TOS = TOS1 \^\ TOS}.
|
---|
236 | \end{opcodedesc}
|
---|
237 |
|
---|
238 | \begin{opcodedesc}{BINARY_OR}{}
|
---|
239 | Implements \code{TOS = TOS1 | TOS}.
|
---|
240 | \end{opcodedesc}
|
---|
241 |
|
---|
242 | In-place operations are like binary operations, in that they remove TOS and
|
---|
243 | TOS1, and push the result back on the stack, but the operation is done
|
---|
244 | in-place when TOS1 supports it, and the resulting TOS may be (but does not
|
---|
245 | have to be) the original TOS1.
|
---|
246 |
|
---|
247 | \begin{opcodedesc}{INPLACE_POWER}{}
|
---|
248 | Implements in-place \code{TOS = TOS1 ** TOS}.
|
---|
249 | \end{opcodedesc}
|
---|
250 |
|
---|
251 | \begin{opcodedesc}{INPLACE_MULTIPLY}{}
|
---|
252 | Implements in-place \code{TOS = TOS1 * TOS}.
|
---|
253 | \end{opcodedesc}
|
---|
254 |
|
---|
255 | \begin{opcodedesc}{INPLACE_DIVIDE}{}
|
---|
256 | Implements in-place \code{TOS = TOS1 / TOS} when
|
---|
257 | \code{from __future__ import division} is not in effect.
|
---|
258 | \end{opcodedesc}
|
---|
259 |
|
---|
260 | \begin{opcodedesc}{INPLACE_FLOOR_DIVIDE}{}
|
---|
261 | Implements in-place \code{TOS = TOS1 // TOS}.
|
---|
262 | \end{opcodedesc}
|
---|
263 |
|
---|
264 | \begin{opcodedesc}{INPLACE_TRUE_DIVIDE}{}
|
---|
265 | Implements in-place \code{TOS = TOS1 / TOS} when
|
---|
266 | \code{from __future__ import division} is in effect.
|
---|
267 | \end{opcodedesc}
|
---|
268 |
|
---|
269 | \begin{opcodedesc}{INPLACE_MODULO}{}
|
---|
270 | Implements in-place \code{TOS = TOS1 \%{} TOS}.
|
---|
271 | \end{opcodedesc}
|
---|
272 |
|
---|
273 | \begin{opcodedesc}{INPLACE_ADD}{}
|
---|
274 | Implements in-place \code{TOS = TOS1 + TOS}.
|
---|
275 | \end{opcodedesc}
|
---|
276 |
|
---|
277 | \begin{opcodedesc}{INPLACE_SUBTRACT}{}
|
---|
278 | Implements in-place \code{TOS = TOS1 - TOS}.
|
---|
279 | \end{opcodedesc}
|
---|
280 |
|
---|
281 | \begin{opcodedesc}{INPLACE_LSHIFT}{}
|
---|
282 | Implements in-place \code{TOS = TOS1 <\code{}< TOS}.
|
---|
283 | \end{opcodedesc}
|
---|
284 |
|
---|
285 | \begin{opcodedesc}{INPLACE_RSHIFT}{}
|
---|
286 | Implements in-place \code{TOS = TOS1 >\code{}> TOS}.
|
---|
287 | \end{opcodedesc}
|
---|
288 |
|
---|
289 | \begin{opcodedesc}{INPLACE_AND}{}
|
---|
290 | Implements in-place \code{TOS = TOS1 \&\ TOS}.
|
---|
291 | \end{opcodedesc}
|
---|
292 |
|
---|
293 | \begin{opcodedesc}{INPLACE_XOR}{}
|
---|
294 | Implements in-place \code{TOS = TOS1 \^\ TOS}.
|
---|
295 | \end{opcodedesc}
|
---|
296 |
|
---|
297 | \begin{opcodedesc}{INPLACE_OR}{}
|
---|
298 | Implements in-place \code{TOS = TOS1 | TOS}.
|
---|
299 | \end{opcodedesc}
|
---|
300 |
|
---|
301 | The slice opcodes take up to three parameters.
|
---|
302 |
|
---|
303 | \begin{opcodedesc}{SLICE+0}{}
|
---|
304 | Implements \code{TOS = TOS[:]}.
|
---|
305 | \end{opcodedesc}
|
---|
306 |
|
---|
307 | \begin{opcodedesc}{SLICE+1}{}
|
---|
308 | Implements \code{TOS = TOS1[TOS:]}.
|
---|
309 | \end{opcodedesc}
|
---|
310 |
|
---|
311 | \begin{opcodedesc}{SLICE+2}{}
|
---|
312 | Implements \code{TOS = TOS1[:TOS]}.
|
---|
313 | \end{opcodedesc}
|
---|
314 |
|
---|
315 | \begin{opcodedesc}{SLICE+3}{}
|
---|
316 | Implements \code{TOS = TOS2[TOS1:TOS]}.
|
---|
317 | \end{opcodedesc}
|
---|
318 |
|
---|
319 | Slice assignment needs even an additional parameter. As any statement,
|
---|
320 | they put nothing on the stack.
|
---|
321 |
|
---|
322 | \begin{opcodedesc}{STORE_SLICE+0}{}
|
---|
323 | Implements \code{TOS[:] = TOS1}.
|
---|
324 | \end{opcodedesc}
|
---|
325 |
|
---|
326 | \begin{opcodedesc}{STORE_SLICE+1}{}
|
---|
327 | Implements \code{TOS1[TOS:] = TOS2}.
|
---|
328 | \end{opcodedesc}
|
---|
329 |
|
---|
330 | \begin{opcodedesc}{STORE_SLICE+2}{}
|
---|
331 | Implements \code{TOS1[:TOS] = TOS2}.
|
---|
332 | \end{opcodedesc}
|
---|
333 |
|
---|
334 | \begin{opcodedesc}{STORE_SLICE+3}{}
|
---|
335 | Implements \code{TOS2[TOS1:TOS] = TOS3}.
|
---|
336 | \end{opcodedesc}
|
---|
337 |
|
---|
338 | \begin{opcodedesc}{DELETE_SLICE+0}{}
|
---|
339 | Implements \code{del TOS[:]}.
|
---|
340 | \end{opcodedesc}
|
---|
341 |
|
---|
342 | \begin{opcodedesc}{DELETE_SLICE+1}{}
|
---|
343 | Implements \code{del TOS1[TOS:]}.
|
---|
344 | \end{opcodedesc}
|
---|
345 |
|
---|
346 | \begin{opcodedesc}{DELETE_SLICE+2}{}
|
---|
347 | Implements \code{del TOS1[:TOS]}.
|
---|
348 | \end{opcodedesc}
|
---|
349 |
|
---|
350 | \begin{opcodedesc}{DELETE_SLICE+3}{}
|
---|
351 | Implements \code{del TOS2[TOS1:TOS]}.
|
---|
352 | \end{opcodedesc}
|
---|
353 |
|
---|
354 | \begin{opcodedesc}{STORE_SUBSCR}{}
|
---|
355 | Implements \code{TOS1[TOS] = TOS2}.
|
---|
356 | \end{opcodedesc}
|
---|
357 |
|
---|
358 | \begin{opcodedesc}{DELETE_SUBSCR}{}
|
---|
359 | Implements \code{del TOS1[TOS]}.
|
---|
360 | \end{opcodedesc}
|
---|
361 |
|
---|
362 | Miscellaneous opcodes.
|
---|
363 |
|
---|
364 | \begin{opcodedesc}{PRINT_EXPR}{}
|
---|
365 | Implements the expression statement for the interactive mode. TOS is
|
---|
366 | removed from the stack and printed. In non-interactive mode, an
|
---|
367 | expression statement is terminated with \code{POP_STACK}.
|
---|
368 | \end{opcodedesc}
|
---|
369 |
|
---|
370 | \begin{opcodedesc}{PRINT_ITEM}{}
|
---|
371 | Prints TOS to the file-like object bound to \code{sys.stdout}. There
|
---|
372 | is one such instruction for each item in the \keyword{print} statement.
|
---|
373 | \end{opcodedesc}
|
---|
374 |
|
---|
375 | \begin{opcodedesc}{PRINT_ITEM_TO}{}
|
---|
376 | Like \code{PRINT_ITEM}, but prints the item second from TOS to the
|
---|
377 | file-like object at TOS. This is used by the extended print statement.
|
---|
378 | \end{opcodedesc}
|
---|
379 |
|
---|
380 | \begin{opcodedesc}{PRINT_NEWLINE}{}
|
---|
381 | Prints a new line on \code{sys.stdout}. This is generated as the
|
---|
382 | last operation of a \keyword{print} statement, unless the statement
|
---|
383 | ends with a comma.
|
---|
384 | \end{opcodedesc}
|
---|
385 |
|
---|
386 | \begin{opcodedesc}{PRINT_NEWLINE_TO}{}
|
---|
387 | Like \code{PRINT_NEWLINE}, but prints the new line on the file-like
|
---|
388 | object on the TOS. This is used by the extended print statement.
|
---|
389 | \end{opcodedesc}
|
---|
390 |
|
---|
391 | \begin{opcodedesc}{BREAK_LOOP}{}
|
---|
392 | Terminates a loop due to a \keyword{break} statement.
|
---|
393 | \end{opcodedesc}
|
---|
394 |
|
---|
395 | \begin{opcodedesc}{CONTINUE_LOOP}{target}
|
---|
396 | Continues a loop due to a \keyword{continue} statement. \var{target}
|
---|
397 | is the address to jump to (which should be a \code{FOR_ITER}
|
---|
398 | instruction).
|
---|
399 | \end{opcodedesc}
|
---|
400 |
|
---|
401 | \begin{opcodedesc}{LIST_APPEND}{}
|
---|
402 | Calls \code{list.append(TOS1, TOS)}. Used to implement list comprehensions.
|
---|
403 | \end{opcodedesc}
|
---|
404 |
|
---|
405 | \begin{opcodedesc}{LOAD_LOCALS}{}
|
---|
406 | Pushes a reference to the locals of the current scope on the stack.
|
---|
407 | This is used in the code for a class definition: After the class body
|
---|
408 | is evaluated, the locals are passed to the class definition.
|
---|
409 | \end{opcodedesc}
|
---|
410 |
|
---|
411 | \begin{opcodedesc}{RETURN_VALUE}{}
|
---|
412 | Returns with TOS to the caller of the function.
|
---|
413 | \end{opcodedesc}
|
---|
414 |
|
---|
415 | \begin{opcodedesc}{YIELD_VALUE}{}
|
---|
416 | Pops \code{TOS} and yields it from a generator.
|
---|
417 | \end{opcodedesc}
|
---|
418 |
|
---|
419 | \begin{opcodedesc}{IMPORT_STAR}{}
|
---|
420 | Loads all symbols not starting with \character{_} directly from the module TOS
|
---|
421 | to the local namespace. The module is popped after loading all names.
|
---|
422 | This opcode implements \code{from module import *}.
|
---|
423 | \end{opcodedesc}
|
---|
424 |
|
---|
425 | \begin{opcodedesc}{EXEC_STMT}{}
|
---|
426 | Implements \code{exec TOS2,TOS1,TOS}. The compiler fills
|
---|
427 | missing optional parameters with \code{None}.
|
---|
428 | \end{opcodedesc}
|
---|
429 |
|
---|
430 | \begin{opcodedesc}{POP_BLOCK}{}
|
---|
431 | Removes one block from the block stack. Per frame, there is a
|
---|
432 | stack of blocks, denoting nested loops, try statements, and such.
|
---|
433 | \end{opcodedesc}
|
---|
434 |
|
---|
435 | \begin{opcodedesc}{END_FINALLY}{}
|
---|
436 | Terminates a \keyword{finally} clause. The interpreter recalls
|
---|
437 | whether the exception has to be re-raised, or whether the function
|
---|
438 | returns, and continues with the outer-next block.
|
---|
439 | \end{opcodedesc}
|
---|
440 |
|
---|
441 | \begin{opcodedesc}{BUILD_CLASS}{}
|
---|
442 | Creates a new class object. TOS is the methods dictionary, TOS1
|
---|
443 | the tuple of the names of the base classes, and TOS2 the class name.
|
---|
444 | \end{opcodedesc}
|
---|
445 |
|
---|
446 | All of the following opcodes expect arguments. An argument is two
|
---|
447 | bytes, with the more significant byte last.
|
---|
448 |
|
---|
449 | \begin{opcodedesc}{STORE_NAME}{namei}
|
---|
450 | Implements \code{name = TOS}. \var{namei} is the index of \var{name}
|
---|
451 | in the attribute \member{co_names} of the code object.
|
---|
452 | The compiler tries to use \code{STORE_LOCAL} or \code{STORE_GLOBAL}
|
---|
453 | if possible.
|
---|
454 | \end{opcodedesc}
|
---|
455 |
|
---|
456 | \begin{opcodedesc}{DELETE_NAME}{namei}
|
---|
457 | Implements \code{del name}, where \var{namei} is the index into
|
---|
458 | \member{co_names} attribute of the code object.
|
---|
459 | \end{opcodedesc}
|
---|
460 |
|
---|
461 | \begin{opcodedesc}{UNPACK_SEQUENCE}{count}
|
---|
462 | Unpacks TOS into \var{count} individual values, which are put onto
|
---|
463 | the stack right-to-left.
|
---|
464 | \end{opcodedesc}
|
---|
465 |
|
---|
466 | %\begin{opcodedesc}{UNPACK_LIST}{count}
|
---|
467 | %This opcode is obsolete.
|
---|
468 | %\end{opcodedesc}
|
---|
469 |
|
---|
470 | %\begin{opcodedesc}{UNPACK_ARG}{count}
|
---|
471 | %This opcode is obsolete.
|
---|
472 | %\end{opcodedesc}
|
---|
473 |
|
---|
474 | \begin{opcodedesc}{DUP_TOPX}{count}
|
---|
475 | Duplicate \var{count} items, keeping them in the same order. Due to
|
---|
476 | implementation limits, \var{count} should be between 1 and 5 inclusive.
|
---|
477 | \end{opcodedesc}
|
---|
478 |
|
---|
479 | \begin{opcodedesc}{STORE_ATTR}{namei}
|
---|
480 | Implements \code{TOS.name = TOS1}, where \var{namei} is the index
|
---|
481 | of name in \member{co_names}.
|
---|
482 | \end{opcodedesc}
|
---|
483 |
|
---|
484 | \begin{opcodedesc}{DELETE_ATTR}{namei}
|
---|
485 | Implements \code{del TOS.name}, using \var{namei} as index into
|
---|
486 | \member{co_names}.
|
---|
487 | \end{opcodedesc}
|
---|
488 |
|
---|
489 | \begin{opcodedesc}{STORE_GLOBAL}{namei}
|
---|
490 | Works as \code{STORE_NAME}, but stores the name as a global.
|
---|
491 | \end{opcodedesc}
|
---|
492 |
|
---|
493 | \begin{opcodedesc}{DELETE_GLOBAL}{namei}
|
---|
494 | Works as \code{DELETE_NAME}, but deletes a global name.
|
---|
495 | \end{opcodedesc}
|
---|
496 |
|
---|
497 | %\begin{opcodedesc}{UNPACK_VARARG}{argc}
|
---|
498 | %This opcode is obsolete.
|
---|
499 | %\end{opcodedesc}
|
---|
500 |
|
---|
501 | \begin{opcodedesc}{LOAD_CONST}{consti}
|
---|
502 | Pushes \samp{co_consts[\var{consti}]} onto the stack.
|
---|
503 | \end{opcodedesc}
|
---|
504 |
|
---|
505 | \begin{opcodedesc}{LOAD_NAME}{namei}
|
---|
506 | Pushes the value associated with \samp{co_names[\var{namei}]} onto the stack.
|
---|
507 | \end{opcodedesc}
|
---|
508 |
|
---|
509 | \begin{opcodedesc}{BUILD_TUPLE}{count}
|
---|
510 | Creates a tuple consuming \var{count} items from the stack, and pushes
|
---|
511 | the resulting tuple onto the stack.
|
---|
512 | \end{opcodedesc}
|
---|
513 |
|
---|
514 | \begin{opcodedesc}{BUILD_LIST}{count}
|
---|
515 | Works as \code{BUILD_TUPLE}, but creates a list.
|
---|
516 | \end{opcodedesc}
|
---|
517 |
|
---|
518 | \begin{opcodedesc}{BUILD_MAP}{zero}
|
---|
519 | Pushes a new empty dictionary object onto the stack. The argument is
|
---|
520 | ignored and set to zero by the compiler.
|
---|
521 | \end{opcodedesc}
|
---|
522 |
|
---|
523 | \begin{opcodedesc}{LOAD_ATTR}{namei}
|
---|
524 | Replaces TOS with \code{getattr(TOS, co_names[\var{namei}])}.
|
---|
525 | \end{opcodedesc}
|
---|
526 |
|
---|
527 | \begin{opcodedesc}{COMPARE_OP}{opname}
|
---|
528 | Performs a Boolean operation. The operation name can be found
|
---|
529 | in \code{cmp_op[\var{opname}]}.
|
---|
530 | \end{opcodedesc}
|
---|
531 |
|
---|
532 | \begin{opcodedesc}{IMPORT_NAME}{namei}
|
---|
533 | Imports the module \code{co_names[\var{namei}]}. The module object is
|
---|
534 | pushed onto the stack. The current namespace is not affected: for a
|
---|
535 | proper import statement, a subsequent \code{STORE_FAST} instruction
|
---|
536 | modifies the namespace.
|
---|
537 | \end{opcodedesc}
|
---|
538 |
|
---|
539 | \begin{opcodedesc}{IMPORT_FROM}{namei}
|
---|
540 | Loads the attribute \code{co_names[\var{namei}]} from the module found in
|
---|
541 | TOS. The resulting object is pushed onto the stack, to be subsequently
|
---|
542 | stored by a \code{STORE_FAST} instruction.
|
---|
543 | \end{opcodedesc}
|
---|
544 |
|
---|
545 | \begin{opcodedesc}{JUMP_FORWARD}{delta}
|
---|
546 | Increments byte code counter by \var{delta}.
|
---|
547 | \end{opcodedesc}
|
---|
548 |
|
---|
549 | \begin{opcodedesc}{JUMP_IF_TRUE}{delta}
|
---|
550 | If TOS is true, increment the byte code counter by \var{delta}. TOS is
|
---|
551 | left on the stack.
|
---|
552 | \end{opcodedesc}
|
---|
553 |
|
---|
554 | \begin{opcodedesc}{JUMP_IF_FALSE}{delta}
|
---|
555 | If TOS is false, increment the byte code counter by \var{delta}. TOS
|
---|
556 | is not changed.
|
---|
557 | \end{opcodedesc}
|
---|
558 |
|
---|
559 | \begin{opcodedesc}{JUMP_ABSOLUTE}{target}
|
---|
560 | Set byte code counter to \var{target}.
|
---|
561 | \end{opcodedesc}
|
---|
562 |
|
---|
563 | \begin{opcodedesc}{FOR_ITER}{delta}
|
---|
564 | \code{TOS} is an iterator. Call its \method{next()} method. If this
|
---|
565 | yields a new value, push it on the stack (leaving the iterator below
|
---|
566 | it). If the iterator indicates it is exhausted \code{TOS} is
|
---|
567 | popped, and the byte code counter is incremented by \var{delta}.
|
---|
568 | \end{opcodedesc}
|
---|
569 |
|
---|
570 | %\begin{opcodedesc}{FOR_LOOP}{delta}
|
---|
571 | %This opcode is obsolete.
|
---|
572 | %\end{opcodedesc}
|
---|
573 |
|
---|
574 | %\begin{opcodedesc}{LOAD_LOCAL}{namei}
|
---|
575 | %This opcode is obsolete.
|
---|
576 | %\end{opcodedesc}
|
---|
577 |
|
---|
578 | \begin{opcodedesc}{LOAD_GLOBAL}{namei}
|
---|
579 | Loads the global named \code{co_names[\var{namei}]} onto the stack.
|
---|
580 | \end{opcodedesc}
|
---|
581 |
|
---|
582 | %\begin{opcodedesc}{SET_FUNC_ARGS}{argc}
|
---|
583 | %This opcode is obsolete.
|
---|
584 | %\end{opcodedesc}
|
---|
585 |
|
---|
586 | \begin{opcodedesc}{SETUP_LOOP}{delta}
|
---|
587 | Pushes a block for a loop onto the block stack. The block spans
|
---|
588 | from the current instruction with a size of \var{delta} bytes.
|
---|
589 | \end{opcodedesc}
|
---|
590 |
|
---|
591 | \begin{opcodedesc}{SETUP_EXCEPT}{delta}
|
---|
592 | Pushes a try block from a try-except clause onto the block stack.
|
---|
593 | \var{delta} points to the first except block.
|
---|
594 | \end{opcodedesc}
|
---|
595 |
|
---|
596 | \begin{opcodedesc}{SETUP_FINALLY}{delta}
|
---|
597 | Pushes a try block from a try-except clause onto the block stack.
|
---|
598 | \var{delta} points to the finally block.
|
---|
599 | \end{opcodedesc}
|
---|
600 |
|
---|
601 | \begin{opcodedesc}{LOAD_FAST}{var_num}
|
---|
602 | Pushes a reference to the local \code{co_varnames[\var{var_num}]} onto
|
---|
603 | the stack.
|
---|
604 | \end{opcodedesc}
|
---|
605 |
|
---|
606 | \begin{opcodedesc}{STORE_FAST}{var_num}
|
---|
607 | Stores TOS into the local \code{co_varnames[\var{var_num}]}.
|
---|
608 | \end{opcodedesc}
|
---|
609 |
|
---|
610 | \begin{opcodedesc}{DELETE_FAST}{var_num}
|
---|
611 | Deletes local \code{co_varnames[\var{var_num}]}.
|
---|
612 | \end{opcodedesc}
|
---|
613 |
|
---|
614 | \begin{opcodedesc}{LOAD_CLOSURE}{i}
|
---|
615 | Pushes a reference to the cell contained in slot \var{i} of the
|
---|
616 | cell and free variable storage. The name of the variable is
|
---|
617 | \code{co_cellvars[\var{i}]} if \var{i} is less than the length of
|
---|
618 | \var{co_cellvars}. Otherwise it is
|
---|
619 | \code{co_freevars[\var{i} - len(co_cellvars)]}.
|
---|
620 | \end{opcodedesc}
|
---|
621 |
|
---|
622 | \begin{opcodedesc}{LOAD_DEREF}{i}
|
---|
623 | Loads the cell contained in slot \var{i} of the cell and free variable
|
---|
624 | storage. Pushes a reference to the object the cell contains on the
|
---|
625 | stack.
|
---|
626 | \end{opcodedesc}
|
---|
627 |
|
---|
628 | \begin{opcodedesc}{STORE_DEREF}{i}
|
---|
629 | Stores TOS into the cell contained in slot \var{i} of the cell and
|
---|
630 | free variable storage.
|
---|
631 | \end{opcodedesc}
|
---|
632 |
|
---|
633 | \begin{opcodedesc}{SET_LINENO}{lineno}
|
---|
634 | This opcode is obsolete.
|
---|
635 | \end{opcodedesc}
|
---|
636 |
|
---|
637 | \begin{opcodedesc}{RAISE_VARARGS}{argc}
|
---|
638 | Raises an exception. \var{argc} indicates the number of parameters
|
---|
639 | to the raise statement, ranging from 0 to 3. The handler will find
|
---|
640 | the traceback as TOS2, the parameter as TOS1, and the exception
|
---|
641 | as TOS.
|
---|
642 | \end{opcodedesc}
|
---|
643 |
|
---|
644 | \begin{opcodedesc}{CALL_FUNCTION}{argc}
|
---|
645 | Calls a function. The low byte of \var{argc} indicates the number of
|
---|
646 | positional parameters, the high byte the number of keyword parameters.
|
---|
647 | On the stack, the opcode finds the keyword parameters first. For each
|
---|
648 | keyword argument, the value is on top of the key. Below the keyword
|
---|
649 | parameters, the positional parameters are on the stack, with the
|
---|
650 | right-most parameter on top. Below the parameters, the function object
|
---|
651 | to call is on the stack.
|
---|
652 | \end{opcodedesc}
|
---|
653 |
|
---|
654 | \begin{opcodedesc}{MAKE_FUNCTION}{argc}
|
---|
655 | Pushes a new function object on the stack. TOS is the code associated
|
---|
656 | with the function. The function object is defined to have \var{argc}
|
---|
657 | default parameters, which are found below TOS.
|
---|
658 | \end{opcodedesc}
|
---|
659 |
|
---|
660 | \begin{opcodedesc}{MAKE_CLOSURE}{argc}
|
---|
661 | Creates a new function object, sets its \var{func_closure} slot, and
|
---|
662 | pushes it on the stack. TOS is the code associated with the function.
|
---|
663 | If the code object has N free variables, the next N items on the stack
|
---|
664 | are the cells for these variables. The function also has \var{argc}
|
---|
665 | default parameters, where are found before the cells.
|
---|
666 | \end{opcodedesc}
|
---|
667 |
|
---|
668 | \begin{opcodedesc}{BUILD_SLICE}{argc}
|
---|
669 | Pushes a slice object on the stack. \var{argc} must be 2 or 3. If it
|
---|
670 | is 2, \code{slice(TOS1, TOS)} is pushed; if it is 3,
|
---|
671 | \code{slice(TOS2, TOS1, TOS)} is pushed.
|
---|
672 | See the \code{slice()}\bifuncindex{slice} built-in function for more
|
---|
673 | information.
|
---|
674 | \end{opcodedesc}
|
---|
675 |
|
---|
676 | \begin{opcodedesc}{EXTENDED_ARG}{ext}
|
---|
677 | Prefixes any opcode which has an argument too big to fit into the
|
---|
678 | default two bytes. \var{ext} holds two additional bytes which, taken
|
---|
679 | together with the subsequent opcode's argument, comprise a four-byte
|
---|
680 | argument, \var{ext} being the two most-significant bytes.
|
---|
681 | \end{opcodedesc}
|
---|
682 |
|
---|
683 | \begin{opcodedesc}{CALL_FUNCTION_VAR}{argc}
|
---|
684 | Calls a function. \var{argc} is interpreted as in \code{CALL_FUNCTION}.
|
---|
685 | The top element on the stack contains the variable argument list, followed
|
---|
686 | by keyword and positional arguments.
|
---|
687 | \end{opcodedesc}
|
---|
688 |
|
---|
689 | \begin{opcodedesc}{CALL_FUNCTION_KW}{argc}
|
---|
690 | Calls a function. \var{argc} is interpreted as in \code{CALL_FUNCTION}.
|
---|
691 | The top element on the stack contains the keyword arguments dictionary,
|
---|
692 | followed by explicit keyword and positional arguments.
|
---|
693 | \end{opcodedesc}
|
---|
694 |
|
---|
695 | \begin{opcodedesc}{CALL_FUNCTION_VAR_KW}{argc}
|
---|
696 | Calls a function. \var{argc} is interpreted as in
|
---|
697 | \code{CALL_FUNCTION}. The top element on the stack contains the
|
---|
698 | keyword arguments dictionary, followed by the variable-arguments
|
---|
699 | tuple, followed by explicit keyword and positional arguments.
|
---|
700 | \end{opcodedesc}
|
---|
701 |
|
---|
702 | \begin{opcodedesc}{HAVE_ARGUMENT}{}
|
---|
703 | This is not really an opcode. It identifies the dividing line between
|
---|
704 | opcodes which don't take arguments \code{< HAVE_ARGUMENT} and those which do
|
---|
705 | \code{>= HAVE_ARGUMENT}.
|
---|
706 | \end{opcodedesc}
|
---|