“Python/docs/3.9/library/dis”的版本间差异

来自菜鸟教程
Python/docs/3.9/library/dis
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:dis — Python 字节码的反汇编器 — Python 文档}}
 
<div id="module-dis" class="section">
 
<div id="module-dis" class="section">
  
 
<span id="dis-disassembler-for-python-bytecode"></span>
 
<span id="dis-disassembler-for-python-bytecode"></span>
= [[#module-dis|<code>dis</code>]] --- Disassembler for Python bytecode =
+
= dis Python 字节码的反汇编器 =
  
'''Source code:''' [https://github.com/python/cpython/tree/3.9/Lib/dis.py Lib/dis.py]
+
'''源代码:''' [[#id1|<span id="id2" class="problematic">:source:`Lib/dis.py`</span>]]
  
The [[#module-dis|<code>dis</code>]] module supports the analysis of CPython [[../../glossary#term-bytecode|<span class="xref std std-term">bytecode</span>]] by
 
disassembling it. The CPython bytecode which this module takes as an input is
 
defined in the file <code>Include/opcode.h</code> and used by the compiler and the
 
interpreter.
 
  
<div class="impl-detail compound">
+
-----
  
'''CPython implementation detail:''' Bytecode is an implementation detail of the CPython interpreter. No
+
[[#module-dis|dis]] 模块支持对 CPython [[../../glossary#term-bytecode|bytecode]] 进行反汇编分析。 该模块作为输入的 CPython 字节码在文件 <code>Include/opcode.h</code> 中定义,并由编译器和解释器使用。
guarantees are made that bytecode will not be added, removed, or changed
 
between versions of Python. Use of this module should not be considered to
 
work across Python VMs or Python releases.
 
  
<div class="compound-last versionchanged">
+
示例:给定函数 <code>myfunc()</code>
 
 
<span class="versionmodified changed">在 3.6 版更改: </span>Use 2 bytes for each instruction. Previously the number of bytes varied
 
by instruction.
 
 
 
 
 
</div>
 
 
 
</div>
 
Example: Given the function <code>myfunc()</code>:
 
  
 
<div class="highlight-python3 notranslate">
 
<div class="highlight-python3 notranslate">
第33行: 第18行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>def myfunc(alist):
+
<syntaxhighlight lang="python3">def myfunc(alist):
     return len(alist)</pre>
+
     return len(alist)</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
the following command can be used to display the disassembly of
+
以下命令可用于显示<code>myfunc()</code>的反汇编:
<code>myfunc()</code>:
 
  
 
<div class="highlight-python3 notranslate">
 
<div class="highlight-python3 notranslate">
第46行: 第30行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>&gt;&gt;&gt; dis.dis(myfunc)
+
<syntaxhighlight lang="python3">>>> dis.dis(myfunc)
 
   2          0 LOAD_GLOBAL              0 (len)
 
   2          0 LOAD_GLOBAL              0 (len)
 
               2 LOAD_FAST                0 (alist)
 
               2 LOAD_FAST                0 (alist)
 
               4 CALL_FUNCTION            1
 
               4 CALL_FUNCTION            1
               6 RETURN_VALUE</pre>
+
               6 RETURN_VALUE</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
(The &quot;2&quot; is a line number).
+
(“2”是行号)。
  
 
<div id="bytecode-analysis" class="section">
 
<div id="bytecode-analysis" class="section">
  
== Bytecode analysis ==
+
== 字节码分析 ==
  
 
<div class="versionadded">
 
<div class="versionadded">
  
<span class="versionmodified added">3.4 新版功能.</span>
+
<span class="versionmodified added">3.4 版中的新功能。</span>
  
  
 
</div>
 
</div>
The bytecode analysis API allows pieces of Python code to be wrapped in a
+
字节码分析 API 允许将 Python 代码片段包装在 [[#dis.Bytecode|Bytecode]] 对象中,以便轻松访问已编译代码的详细信息。
[[#dis.Bytecode|<code>Bytecode</code>]] object that provides easy access to details of the compiled
 
code.
 
  
 
<dl>
 
<dl>
<dt>''class'' <code>dis.</code><code>Bytecode</code><span class="sig-paren">(</span>''<span class="n">x</span>'', ''<span class="o">*</span>'', ''<span class="n">first_line</span><span class="o">=</span><span class="default_value">None</span>'', ''<span class="n">current_offset</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span></dt>
+
<dt>''<span class="pre">class</span>'' <span class="sig-prename descclassname"><span class="pre">dis.</span></span><span class="sig-name descname"><span class="pre">Bytecode</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">x</span></span>'', ''<span class="o"><span class="pre">*</span></span>'', ''<span class="n"><span class="pre">first_line</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>'', ''<span class="n"><span class="pre">current_offset</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Analyse the bytecode corresponding to a function, generator, asynchronous
+
<dd><p>分析与函数、生成器、异步生成器、协程、方法、源代码字符串或代码对象(由 [[../functions#compile|compile()]] 返回)对应的字节码。</p>
generator, coroutine, method, string of source code, or a code object (as
+
<p>这是对下面列出的许多函数的便利包装,最显着的是 [[#dis.get_instructions|get_instructions()]],因为迭代 [[#dis.Bytecode|Bytecode]] 实例将字节码操作生成为 [[#dis.Instruction|Instruction]] 实例.</p>
returned by [[../functions#compile|<code>compile()</code>]]).</p>
+
<p>如果 ''first_line'' 不是 <code>None</code>,则表示反汇编代码中第一个源代码行应报告的行号。 否则,源行信息(如果有)直接从反汇编的代码对象中获取。</p>
<p>This is a convenience wrapper around many of the functions listed below, most
+
<p>如果 ''current_offset'' 不是 <code>None</code>,则指的是反汇编代码中的指令偏移量。 设置这意味着 [[#dis.Bytecode.dis|dis()]] 将针对指定的操作码显示“当前指令”标记。</p>
notably [[#dis.get_instructions|<code>get_instructions()</code>]], as iterating over a [[#dis.Bytecode|<code>Bytecode</code>]]
 
instance yields the bytecode operations as [[#dis.Instruction|<code>Instruction</code>]] instances.</p>
 
<p>If ''first_line'' is not <code>None</code>, it indicates the line number that should be
 
reported for the first source line in the disassembled code. Otherwise, the
 
source line information (if any) is taken directly from the disassembled code
 
object.</p>
 
<p>If ''current_offset'' is not <code>None</code>, it refers to an instruction offset in the
 
disassembled code. Setting this means [[#dis.Bytecode.dis|<code>dis()</code>]] will display a &quot;current
 
instruction&quot; marker against the specified opcode.</p>
 
 
<dl>
 
<dl>
<dt>''classmethod'' <code>from_traceback</code><span class="sig-paren">(</span>''<span class="n">tb</span>''<span class="sig-paren">)</span></dt>
+
<dt>''<span class="pre">classmethod</span>'' <span class="sig-name descname"><span class="pre">from_traceback</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">tb</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Construct a [[#dis.Bytecode|<code>Bytecode</code>]] instance from the given traceback, setting
+
<dd><p>从给定的回溯构造一个 [[#dis.Bytecode|Bytecode]] 实例,将 ''current_offset'' 设置为负责异常的指令。</p></dd></dl>
''current_offset'' to the instruction responsible for the exception.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt><code>codeobj</code></dt>
+
<dt><span class="sig-name descname"><span class="pre">codeobj</span></span></dt>
<dd><p>The compiled code object.</p></dd></dl>
+
<dd><p>编译后的代码对象。</p></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>first_line</code></dt>
+
<dt><span class="sig-name descname"><span class="pre">first_line</span></span></dt>
<dd><p>The first source line of the code object (if available)</p></dd></dl>
+
<dd><p>代码对象的第一行源代码(如果可用)</p></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>dis</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-name descname"><span class="pre">dis</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
<dd><p>Return a formatted view of the bytecode operations (the same as printed by
+
<dd><p>返回字节码操作的格式化视图(与 [[#dis.dis|dis.dis()]] 打印的相同,但作为多行字符串返回)。</p></dd></dl>
[[#dis.dis|<code>dis.dis()</code>]], but returned as a multi-line string).</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt><code>info</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-name descname"><span class="pre">info</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
<dd><p>Return a formatted multi-line string with detailed information about the
+
<dd><p>返回一个格式化的多行字符串,其中包含有关代码对象的详细信息,例如 [[#dis.code_info|code_info()]]</p></dd></dl>
code object, like [[#dis.code_info|<code>code_info()</code>]].</p></dd></dl>
 
  
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">3.7 版更改: </span>This can now handle coroutine and asynchronous generator objects.</p>
+
<p><span class="versionmodified changed"> 3.7 版更改: </span> 现在可以处理协程和异步生成器对象。</p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
Example:
+
例子:
  
 
<div class="highlight-python3 notranslate">
 
<div class="highlight-python3 notranslate">
第121行: 第91行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>&gt;&gt;&gt; bytecode = dis.Bytecode(myfunc)
+
<syntaxhighlight lang="python3">>>> bytecode = dis.Bytecode(myfunc)
&gt;&gt;&gt; for instr in bytecode:
+
>>> for instr in bytecode:
 
...    print(instr.opname)
 
...    print(instr.opname)
 
...
 
...
第128行: 第98行:
 
LOAD_FAST
 
LOAD_FAST
 
CALL_FUNCTION
 
CALL_FUNCTION
RETURN_VALUE</pre>
+
RETURN_VALUE</syntaxhighlight>
  
 
</div>
 
</div>
第137行: 第107行:
 
<div id="analysis-functions" class="section">
 
<div id="analysis-functions" class="section">
  
== Analysis functions ==
+
== 分析功能 ==
  
The [[#module-dis|<code>dis</code>]] module also defines the following analysis functions that convert
+
[[#module-dis|dis]] 模块还定义了以下分析函数,将输入直接转换为所需的输出。 如果仅执行单个操作,它们可能很有用,因此中间分析对象没有用:
the input directly to the desired output. They can be useful if only a single
 
operation is being performed, so the intermediate analysis object isn't useful:
 
  
 
<dl>
 
<dl>
<dt><code>dis.</code><code>code_info</code><span class="sig-paren">(</span>''<span class="n">x</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">dis.</span></span><span class="sig-name descname"><span class="pre">code_info</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">x</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Return a formatted multi-line string with detailed code object information
+
<dd><p>返回一个格式化的多行字符串,其中包含提供的函数、生成器、异步生成器、协程、方法、源代码字符串或代码对象的详细代码对象信息。</p>
for the supplied function, generator, asynchronous generator, coroutine,
+
<p>请注意,代码信息字符串的确切内容高度依赖于实现,并且它们可能会在 Python VM 或 Python 版本之间任意更改。</p>
method, source code string or code object.</p>
 
<p>Note that the exact contents of code info strings are highly implementation
 
dependent and they may change arbitrarily across Python VMs or Python
 
releases.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.2 新版功能.</span></p>
+
<p><span class="versionmodified added">3.2 版中的新功能。</span></p>
  
 
</div>
 
</div>
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">3.7 版更改: </span>This can now handle coroutine and asynchronous generator objects.</p>
+
<p><span class="versionmodified changed"> 3.7 版更改: </span> 现在可以处理协程和异步生成器对象。</p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>dis.</code><code>show_code</code><span class="sig-paren">(</span>''<span class="n">x</span>'', ''<span class="o">*</span>'', ''<span class="n">file</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">dis.</span></span><span class="sig-name descname"><span class="pre">show_code</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">x</span></span>'', ''<span class="o"><span class="pre">*</span></span>'', ''<span class="n"><span class="pre">file</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Print detailed code object information for the supplied function, method,
+
<dd><p>将提供的函数、方法、源代码字符串或代码对象的详细代码对象信息打印到 ''file''(如果未指定 ''file'',则为 <code>sys.stdout</code>)。</p>
source code string or code object to ''file'' (or <code>sys.stdout</code> if ''file''
+
<p>这是 <code>print(code_info(x), file=file)</code> 的便捷简写,用于在解释器提示下进行交互式探索。</p>
is not specified).</p>
 
<p>This is a convenient shorthand for <code>print(code_info(x), file=file)</code>,
 
intended for interactive exploration at the interpreter prompt.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.2 新版功能.</span></p>
+
<p><span class="versionmodified added">3.2 版中的新功能。</span></p>
  
 
</div>
 
</div>
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">3.4 版更改: </span>Added ''file'' parameter.</p>
+
<p><span class="versionmodified changed"> 3.4 版更改: </span> 添加 ''文件'' 参数。</p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>dis.</code><code>dis</code><span class="sig-paren">(</span>''<span class="n">x</span><span class="o">=</span><span class="default_value">None</span>'', ''<span class="o">*</span>'', ''<span class="n">file</span><span class="o">=</span><span class="default_value">None</span>'', ''<span class="n">depth</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">dis.</span></span><span class="sig-name descname"><span class="pre">dis</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">x</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>'', ''<span class="o"><span class="pre">*</span></span>'', ''<span class="n"><span class="pre">file</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>'', ''<span class="n"><span class="pre">depth</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Disassemble the ''x'' object. ''x'' can denote either a module, a class, a
+
<dd><p>拆解 ''x'' 对象。 ''x'' 可以表示模块、类、方法、函数、生成器、异步生成器、协程、代码对象、源代码字符串或原始字节码的字节序列。 对于一个模块,它反汇编了所有功能。 对于一个类,它反汇编所有方法(包括类和静态方法)。 对于代码对象或原始字节码序列,它为每个字节码指令打印一行。 它还递归地反汇编嵌套代码对象(推导式、生成器表达式和嵌套函数的代码,以及用于构建嵌套类的代码)。 在反汇编之前,首先使用 [[../functions#compile|compile()]] 内置函数将字符串编译为代码对象。 如果没有提供对象,这个函数反汇编最后的回溯。</p>
method, a function, a generator, an asynchronous generator, a coroutine,
+
<p>如果提供,反汇编将作为文本写入提供的 ''file'' 参数,否则写入 <code>sys.stdout</code></p>
a code object, a string of source code or a byte sequence of raw bytecode.
+
<p>递归的最大深度受 ''depth'' 的限制,除非它是 <code>None</code><code>depth=0</code> 表示没有递归。</p>
For a module, it disassembles all functions. For a class, it disassembles
 
all methods (including class and static methods). For a code object or
 
sequence of raw bytecode, it prints one line per bytecode instruction.
 
It also recursively disassembles nested code objects (the code of
 
comprehensions, generator expressions and nested functions, and the code
 
used for building nested classes).
 
Strings are first compiled to code objects with the [[../functions#compile|<code>compile()</code>]]
 
built-in function before being disassembled. If no object is provided, this
 
function disassembles the last traceback.</p>
 
<p>The disassembly is written as text to the supplied ''file'' argument if
 
provided and to <code>sys.stdout</code> otherwise.</p>
 
<p>The maximal depth of recursion is limited by ''depth'' unless it is <code>None</code>.
 
<code>depth=0</code> means no recursion.</p>
 
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">3.4 版更改: </span>Added ''file'' parameter.</p>
+
<p><span class="versionmodified changed"> 3.4 版更改: </span> 添加 ''文件'' 参数。</p>
  
 
</div>
 
</div>
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">3.7 版更改: </span>Implemented recursive disassembling and added ''depth'' parameter.</p>
+
<p><span class="versionmodified changed"> 3.7 版本更改: </span> 实现递归反汇编并添加 ''depth'' 参数。</p>
  
 
</div>
 
</div>
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">3.7 版更改: </span>This can now handle coroutine and asynchronous generator objects.</p>
+
<p><span class="versionmodified changed"> 3.7 版更改: </span> 现在可以处理协程和异步生成器对象。</p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>dis.</code><code>distb</code><span class="sig-paren">(</span>''<span class="n">tb</span><span class="o">=</span><span class="default_value">None</span>'', ''<span class="o">*</span>'', ''<span class="n">file</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">dis.</span></span><span class="sig-name descname"><span class="pre">distb</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">tb</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>'', ''<span class="o"><span class="pre">*</span></span>'', ''<span class="n"><span class="pre">file</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Disassemble the top-of-stack function of a traceback, using the last
+
<dd><p>反汇编回溯的栈顶函数,如果没有传递,则使用最后一个回溯。 指示导致异常的指令。</p>
traceback if none was passed. The instruction causing the exception is
+
<p>如果提供,反汇编将作为文本写入提供的 ''file'' 参数,否则写入 <code>sys.stdout</code></p>
indicated.</p>
 
<p>The disassembly is written as text to the supplied ''file'' argument if
 
provided and to <code>sys.stdout</code> otherwise.</p>
 
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">3.4 版更改: </span>Added ''file'' parameter.</p>
+
<p><span class="versionmodified changed"> 3.4 版更改: </span> 添加 ''文件'' 参数。</p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>dis.</code><code>disassemble</code><span class="sig-paren">(</span>''<span class="n">code</span>'', ''<span class="n">lasti</span><span class="o">=</span><span class="default_value">- 1</span>'', ''<span class="o">*</span>'', ''<span class="n">file</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span><br />
+
<dt><span class="sig-prename descclassname"><span class="pre">dis.</span></span><span class="sig-name descname"><span class="pre">disassemble</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">code</span></span>'', ''<span class="n"><span class="pre">lasti</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-</span> <span class="pre">1</span></span>'', ''<span class="o"><span class="pre">*</span></span>'', ''<span class="n"><span class="pre">file</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>''<span class="sig-paren">)</span><br />
<code>dis.</code><code>disco</code><span class="sig-paren">(</span>''<span class="n">code</span>'', ''<span class="n">lasti</span><span class="o">=</span><span class="default_value">- 1</span>'', ''<span class="o">*</span>'', ''<span class="n">file</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span></dt>
+
<span class="sig-prename descclassname"><span class="pre">dis.</span></span><span class="sig-name descname"><span class="pre">disco</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">code</span></span>'', ''<span class="n"><span class="pre">lasti</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-</span> <span class="pre">1</span></span>'', ''<span class="o"><span class="pre">*</span></span>'', ''<span class="n"><span class="pre">file</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Disassemble a code object, indicating the last instruction if ''lasti'' was
+
<dd><p>反汇编代码对象,如果提供了 ''lasti'',则指示最后一条指令。 输出分为以下列:</p>
provided. The output is divided in the following columns:</p>
 
 
<ol>
 
<ol>
<li><p>the line number, for the first instruction of each line</p></li>
+
<li><p>行号,用于每行的第一条指令</p></li>
<li><p>the current instruction, indicated as <code>--&gt;</code>,</p></li>
+
<li><p>当前指令,表示为<code>--&gt;</code></p></li>
<li><p>a labelled instruction, indicated with <code>&gt;&gt;</code>,</p></li>
+
<li><p>一个带标签的指令,用 <code>&gt;&gt;</code> 表示,</p></li>
<li><p>the address of the instruction,</p></li>
+
<li><p>指令的地址,</p></li>
<li><p>the operation code name,</p></li>
+
<li><p>操作代码名称,</p></li>
<li><p>operation parameters, and</p></li>
+
<li><p>操作参数,和</p></li>
<li><p>interpretation of the parameters in parentheses.</p></li></ol>
+
<li><p>括号中参数的解释。</p></li></ol>
  
<p>The parameter interpretation recognizes local and global variable names,
+
<p>参数解释识别局部和全局变量名称、常量值、分支目标和比较运算符。</p>
constant values, branch targets, and compare operators.</p>
+
<p>如果提供,反汇编将作为文本写入提供的 ''file'' 参数,否则写入 <code>sys.stdout</code></p>
<p>The disassembly is written as text to the supplied ''file'' argument if
 
provided and to <code>sys.stdout</code> otherwise.</p>
 
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">3.4 版更改: </span>Added ''file'' parameter.</p>
+
<p><span class="versionmodified changed"> 3.4 版更改: </span> 添加 ''文件'' 参数。</p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>dis.</code><code>get_instructions</code><span class="sig-paren">(</span>''<span class="n">x</span>'', ''<span class="o">*</span>'', ''<span class="n">first_line</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">dis.</span></span><span class="sig-name descname"><span class="pre">get_instructions</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">x</span></span>'', ''<span class="o"><span class="pre">*</span></span>'', ''<span class="n"><span class="pre">first_line</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Return an iterator over the instructions in the supplied function, method,
+
<dd><p>在提供的函数、方法、源代码字符串或代码对象中的指令上返回迭代器。</p>
source code string or code object.</p>
+
<p>迭代器生成一系列 [[#dis.Instruction|Instruction]] 命名元组,提供所提供代码中每个操作的详细信息。</p>
<p>The iterator generates a series of [[#dis.Instruction|<code>Instruction</code>]] named tuples giving
+
<p>如果 ''first_line'' 不是 <code>None</code>,则表示反汇编代码中第一个源代码行应报告的行号。 否则,源行信息(如果有)直接从反汇编的代码对象中获取。</p>
the details of each operation in the supplied code.</p>
 
<p>If ''first_line'' is not <code>None</code>, it indicates the line number that should be
 
reported for the first source line in the disassembled code. Otherwise, the
 
source line information (if any) is taken directly from the disassembled code
 
object.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.4 新版功能.</span></p>
+
<p><span class="versionmodified added">3.4 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>dis.</code><code>findlinestarts</code><span class="sig-paren">(</span>''<span class="n">code</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">dis.</span></span><span class="sig-name descname"><span class="pre">findlinestarts</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">code</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>This generator function uses the <code>co_firstlineno</code> and <code>co_lnotab</code>
+
<dd><p>此生成器函数使用代码对象 ''code'' 的 <code>co_firstlineno</code> <code>co_lnotab</code> 属性来查找作为源代码中行开头的偏移量。 它们生成为 <code>(offset, lineno)</code> 对。 请参阅 [[#id3|:source:`Objects/lnotab_notes.txt`]] 了解 <code>co_lnotab</code> 格式以及如何对其进行解码。</p>
attributes of the code object ''code'' to find the offsets which are starts of
 
lines in the source code. They are generated as <code>(offset, lineno)</code> pairs.
 
See [https://github.com/python/cpython/tree/3.9/Objects/lnotab_notes.txt Objects/lnotab_notes.txt] for the <code>co_lnotab</code> format and
 
how to decode it.</p>
 
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">3.6 版更改: </span>Line numbers can be decreasing. Before, they were always increasing.</p>
+
<p><span class="versionmodified changed"> 3.6 版更改: </span> 行号可以减少。 以前,它们总是在增加。</p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
; <code>dis.</code><code>findlabels</code><span class="sig-paren">(</span>''<span class="n">code</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">dis.</span></span><span class="sig-name descname"><span class="pre">findlabels</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">code</span></span>''<span class="sig-paren">)</span>
: Detect all offsets in the raw compiled bytecode string ''code'' which are jump targets, and return a list of these offsets.
+
: 检测原始编译字节码字符串 ''code'' 中作为跳转目标的所有偏移量,并返回这些偏移量的列表。
  
 
<dl>
 
<dl>
<dt><code>dis.</code><code>stack_effect</code><span class="sig-paren">(</span>''<span class="n">opcode</span>'', ''<span class="n">oparg</span><span class="o">=</span><span class="default_value">None</span>'', ''<span class="o">*</span>'', ''<span class="n">jump</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">dis.</span></span><span class="sig-name descname"><span class="pre">stack_effect</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">opcode</span></span>'', ''<span class="n"><span class="pre">oparg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>'', ''<span class="o"><span class="pre">*</span></span>'', ''<span class="n"><span class="pre">jump</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Compute the stack effect of ''opcode'' with argument ''oparg''.</p>
+
<dd><p>使用参数 ''oparg'' 计算 ''opcode'' 的堆栈效果。</p>
<p>If the code has a jump target and ''jump'' is <code>True</code>, [[#dis.stack_effect|<code>stack_effect()</code>]]
+
<p>如果代码有跳转目标且''jump''<code>True</code>,则[[#dis.stack_effect|stack_effect()]]会返回跳转的堆栈效果。 如果''jump''<code>False</code>,则返回不跳跃的叠加效果。 如果 ''jump'' <code>None</code>(默认),它将返回两种情况下的最大堆栈效果。</p>
will return the stack effect of jumping. If ''jump'' is <code>False</code>,
 
it will return the stack effect of not jumping. And if ''jump'' is
 
<code>None</code> (default), it will return the maximal stack effect of both cases.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.4 新版功能.</span></p>
+
<p><span class="versionmodified added">3.4 版中的新功能。</span></p>
  
 
</div>
 
</div>
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">3.8 版更改: </span>Added ''jump'' parameter.</p>
+
<p><span class="versionmodified changed"> 3.8 版更改: </span> 添加 ''jump'' 参数。</p>
  
 
</div></dd></dl>
 
</div></dd></dl>
第306行: 第236行:
  
 
<span id="bytecodes"></span>
 
<span id="bytecodes"></span>
== Python Bytecode Instructions ==
+
== Python 字节码说明 ==
 
 
The [[#dis.get_instructions|<code>get_instructions()</code>]] function and [[#dis.Bytecode|<code>Bytecode</code>]] class provide
 
details of bytecode instructions as [[#dis.Instruction|<code>Instruction</code>]] instances:
 
 
 
<dl>
 
<dt>''class'' <code>dis.</code><code>Instruction</code></dt>
 
<dd><p>Details for a bytecode operation</p>
 
<dl>
 
<dt><code>opcode</code></dt>
 
<dd><p>numeric code for operation, corresponding to the opcode values listed
 
below and the bytecode values in the [[#opcode-collections|<span class="std std-ref">Opcode collections</span>]].</p></dd></dl>
 
 
 
<dl>
 
<dt><code>opname</code></dt>
 
<dd><p>human readable name for operation</p></dd></dl>
 
 
 
<dl>
 
<dt><code>arg</code></dt>
 
<dd><p>numeric argument to operation (if any), otherwise <code>None</code></p></dd></dl>
 
  
<dl>
+
[[#dis.get_instructions|get_instructions()]] 函数和 [[#dis.Bytecode|Bytecode]] 类提供字节码指令的详细信息作为 [[#dis.Instruction|Instruction]] 实例:
<dt><code>argval</code></dt>
 
<dd><p>resolved arg value (if known), otherwise same as arg</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt><code>argrepr</code></dt>
+
<dt>''<span class="pre">class</span>'' <span class="sig-prename descclassname"><span class="pre">dis.</span></span><span class="sig-name descname"><span class="pre">Instruction</span></span></dt>
<dd><p>human readable description of operation argument</p></dd></dl>
+
<dd><p>字节码操作的详细信息</p>
 
 
 
<dl>
 
<dl>
<dt><code>offset</code></dt>
+
<dt><span class="sig-name descname"><span class="pre">opcode</span></span></dt>
<dd><p>start index of operation within bytecode sequence</p></dd></dl>
+
<dd><p>操作的数字代码,对应于下面列出的操作码值和 [[#opcode-collections|操作码集合]] 中的字节码值。</p></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>starts_line</code></dt>
+
<dt><span class="sig-name descname"><span class="pre">opname</span></span></dt>
<dd><p>line started by this opcode (if any), otherwise <code>None</code></p></dd></dl>
+
<dd><p>人类可读的操作名称</p></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>is_jump_target</code></dt>
+
<dt><span class="sig-name descname"><span class="pre">arg</span></span></dt>
<dd><p><code>True</code> if other code jumps to here, otherwise <code>False</code></p></dd></dl>
+
<dd><p>操作的数字参数(如果有),否则为 <code>None</code></p></dd></dl>
 
 
<div class="versionadded">
 
 
 
<p><span class="versionmodified added">3.4 新版功能.</span></p>
 
 
 
</div></dd></dl>
 
 
 
The Python compiler currently generates the following bytecode instructions.
 
 
 
'''General instructions'''
 
 
 
; <code>NOP</code>
 
: Do nothing code. Used as a placeholder by the bytecode optimizer.
 
 
 
; <code>POP_TOP</code>
 
: Removes the top-of-stack (TOS) item.
 
 
 
; <code>ROT_TWO</code>
 
: Swaps the two top-most stack items.
 
 
 
; <code>ROT_THREE</code>
 
: Lifts second and third stack item one position up, moves top down to position three.
 
  
 
<dl>
 
<dl>
<dt><code>ROT_FOUR</code></dt>
+
<dt><span class="sig-name descname"><span class="pre">argval</span></span></dt>
<dd><p>Lifts second, third and forth stack items one position up, moves top down
+
<dd><p>解析的 arg 值(如果已知),否则与 arg 相同</p></dd></dl>
to position four.</p>
 
<div class="versionadded">
 
 
 
<p><span class="versionmodified added">3.8 新版功能.</span></p>
 
 
 
</div></dd></dl>
 
  
 
<dl>
 
<dl>
<dt><code>DUP_TOP</code></dt>
+
<dt><span class="sig-name descname"><span class="pre">argrepr</span></span></dt>
<dd><p>Duplicates the reference on top of the stack.</p>
+
<dd><p>操作参数的人类可读描述</p></dd></dl>
<div class="versionadded">
 
 
 
<p><span class="versionmodified added">3.2 新版功能.</span></p>
 
 
 
</div></dd></dl>
 
  
 
<dl>
 
<dl>
<dt><code>DUP_TOP_TWO</code></dt>
+
<dt><span class="sig-name descname"><span class="pre">offset</span></span></dt>
<dd><p>Duplicates the two references on top of the stack, leaving them in the
+
<dd><p>字节码序列中操作的起始索引</p></dd></dl>
same order.</p>
 
<div class="versionadded">
 
 
 
<p><span class="versionmodified added">3.2 新版功能.</span></p>
 
 
 
</div></dd></dl>
 
 
 
'''Unary operations'''
 
 
 
Unary operations take the top of the stack, apply the operation, and push the
 
result back on the stack.
 
 
 
; <code>UNARY_POSITIVE</code>
 
: Implements <code>TOS = +TOS</code>.
 
 
 
; <code>UNARY_NEGATIVE</code>
 
: Implements <code>TOS = -TOS</code>.
 
 
 
; <code>UNARY_NOT</code>
 
: Implements <code>TOS = not TOS</code>.
 
 
 
; <code>UNARY_INVERT</code>
 
: Implements <code>TOS = ~TOS</code>.
 
 
 
; <code>GET_ITER</code>
 
: Implements <code>TOS = iter(TOS)</code>.
 
  
 
<dl>
 
<dl>
<dt><code>GET_YIELD_FROM_ITER</code></dt>
+
<dt><span class="sig-name descname"><span class="pre">starts_line</span></span></dt>
<dd><p>If <code>TOS</code> is a [[../../glossary#term-generator-iterator|<span class="xref std std-term">generator iterator</span>]] or [[../../glossary#term-coroutine|<span class="xref std std-term">coroutine</span>]] object
+
<dd><p>以此操作码开头的行(如果有),否则为 <code>None</code></p></dd></dl>
it is left as is. Otherwise, implements <code>TOS = iter(TOS)</code>.</p>
 
<div class="versionadded">
 
 
 
<p><span class="versionmodified added">3.5 新版功能.</span></p>
 
 
 
</div></dd></dl>
 
 
 
'''Binary operations'''
 
 
 
Binary operations remove the top of the stack (TOS) and the second top-most
 
stack item (TOS1) from the stack. They perform the operation, and put the
 
result back on the stack.
 
 
 
; <code>BINARY_POWER</code>
 
: Implements <code>TOS = TOS1 ** TOS</code>.
 
 
 
; <code>BINARY_MULTIPLY</code>
 
: Implements <code>TOS = TOS1 * TOS</code>.
 
  
 
<dl>
 
<dl>
<dt><code>BINARY_MATRIX_MULTIPLY</code></dt>
+
<dt><span class="sig-name descname"><span class="pre">is_jump_target</span></span></dt>
<dd><p>Implements <code>TOS = TOS1 @ TOS</code>.</p>
+
<dd><p><code>True</code> 如果其他代码跳转到这里,否则 <code>False</code></p></dd></dl>
<div class="versionadded">
 
 
 
<p><span class="versionmodified added">3.5 新版功能.</span></p>
 
 
 
</div></dd></dl>
 
 
 
; <code>BINARY_FLOOR_DIVIDE</code>
 
: Implements <code>TOS = TOS1 // TOS</code>.
 
 
 
; <code>BINARY_TRUE_DIVIDE</code>
 
: Implements <code>TOS = TOS1 / TOS</code>.
 
 
 
; <code>BINARY_MODULO</code>
 
: Implements <code>TOS = TOS1 % TOS</code>.
 
 
 
; <code>BINARY_ADD</code>
 
: Implements <code>TOS = TOS1 + TOS</code>.
 
 
 
; <code>BINARY_SUBTRACT</code>
 
: Implements <code>TOS = TOS1 - TOS</code>.
 
 
 
; <code>BINARY_SUBSCR</code>
 
: Implements <code>TOS = TOS1[TOS]</code>.
 
 
 
; <code>BINARY_LSHIFT</code>
 
: Implements <code>TOS = TOS1 &lt;&lt; TOS</code>.
 
 
 
; <code>BINARY_RSHIFT</code>
 
: Implements <code>TOS = TOS1 &gt;&gt; TOS</code>.
 
 
 
; <code>BINARY_AND</code>
 
: Implements <code>TOS = TOS1 &amp; TOS</code>.
 
 
 
; <code>BINARY_XOR</code>
 
: Implements <code>TOS = TOS1 ^ TOS</code>.
 
 
 
; <code>BINARY_OR</code>
 
: Implements <code>TOS = TOS1 | TOS</code>.
 
 
 
'''In-place operations'''
 
  
In-place operations are like binary operations, in that they remove TOS and
 
TOS1, and push the result back on the stack, but the operation is done in-place
 
when TOS1 supports it, and the resulting TOS may be (but does not have to be)
 
the original TOS1.
 
 
; <code>INPLACE_POWER</code>
 
: Implements in-place <code>TOS = TOS1 ** TOS</code>.
 
 
; <code>INPLACE_MULTIPLY</code>
 
: Implements in-place <code>TOS = TOS1 * TOS</code>.
 
 
<dl>
 
<dt><code>INPLACE_MATRIX_MULTIPLY</code></dt>
 
<dd><p>Implements in-place <code>TOS = TOS1 @ TOS</code>.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.5 新版功能.</span></p>
+
<p><span class="versionmodified added">3.4 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
; <code>INPLACE_FLOOR_DIVIDE</code>
+
Python 编译器当前生成以下字节码指令。
: Implements in-place <code>TOS = TOS1 // TOS</code>.
 
 
 
; <code>INPLACE_TRUE_DIVIDE</code>
 
: Implements in-place <code>TOS = TOS1 / TOS</code>.
 
 
 
; <code>INPLACE_MODULO</code>
 
: Implements in-place <code>TOS = TOS1 % TOS</code>.
 
 
 
; <code>INPLACE_ADD</code>
 
: Implements in-place <code>TOS = TOS1 + TOS</code>.
 
 
 
; <code>INPLACE_SUBTRACT</code>
 
: Implements in-place <code>TOS = TOS1 - TOS</code>.
 
  
; <code>INPLACE_LSHIFT</code>
+
'''一般说明'''
: Implements in-place <code>TOS = TOS1 &lt;&lt; TOS</code>.
 
  
; <code>INPLACE_RSHIFT</code>
+
'''一元运算'''
: Implements in-place <code>TOS = TOS1 &gt;&gt; TOS</code>.
 
  
; <code>INPLACE_AND</code>
+
一元操作取栈顶,应用操作,并将结果压回到栈上。
: Implements in-place <code>TOS = TOS1 &amp; TOS</code>.
 
  
; <code>INPLACE_XOR</code>
+
'''二元运算'''
: Implements in-place <code>TOS = TOS1 ^ TOS</code>.
 
  
; <code>INPLACE_OR</code>
+
二元运算从堆栈中删除堆栈顶部 (TOS) 和第二个最顶部堆栈项 (TOS1)。 他们执行操作,并将结果放回堆栈中。
: Implements in-place <code>TOS = TOS1 | TOS</code>.
 
  
; <code>STORE_SUBSCR</code>
+
'''就地操作'''
: Implements <code>TOS1[TOS] = TOS2</code>.
 
  
; <code>DELETE_SUBSCR</code>
+
就地操作类似于二元操作,因为它们删除 TOS 和 TOS1,并将结果推回到堆栈上,但是当 TOS1 支持时,操作就地完成,结果 TOS 可能(但没有是)原来的TOS1。
: Implements <code>del TOS1[TOS]</code>.
 
  
'''Coroutine opcodes'''
+
'''协程操作码'''
  
<dl>
+
'''杂项操作码'''
<dt><code>GET_AWAITABLE</code></dt>
 
<dd><p>Implements <code>TOS = get_awaitable(TOS)</code>, where <code>get_awaitable(o)</code>
 
returns <code>o</code> if <code>o</code> is a coroutine object or a generator object with
 
the CO_ITERABLE_COROUTINE flag, or resolves
 
<code>o.__await__</code>.</p>
 
<div class="versionadded">
 
  
<p><span class="versionmodified added">3.5 新版功能.</span></p>
+
对于所有的 [[#id5|:opcode:`SET_ADD`]], [[#id7|:opcode:`LIST_APPEND`]] 和 [[#id9|:opcode:`MAP_ADD`]] 指令,而添加的值或键/value 对被弹出,容器对象保留在堆栈上,以便它可用于循环的进一步迭代。
  
</div></dd></dl>
+
以下所有操作码都使用它们的参数。
  
<dl>
 
<dt><code>GET_AITER</code></dt>
 
<dd><p>Implements <code>TOS = TOS.__aiter__()</code>.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.5 新版功能.</span></p>
 
  
 
</div>
 
</div>
<div class="versionchanged">
+
<div id="opcode-collections" class="section">
  
<p><span class="versionmodified changed">在 3.7 版更改: </span>Returning awaitable objects from <code>__aiter__</code> is no longer
+
<span id="id11"></span>
supported.</p>
+
== 操作码集合 ==
  
</div></dd></dl>
+
这些集合用于自动检查字节码指令:
  
<dl>
+
; <span class="sig-prename descclassname"><span class="pre">dis.</span></span><span class="sig-name descname"><span class="pre">opname</span></span>
<dt><code>GET_ANEXT</code></dt>
+
: 操作名称序列,可使用字节码索引。
<dd><p>Implements <code>PUSH(get_awaitable(TOS.__anext__()))</code>. See <code>GET_AWAITABLE</code>
 
for details about <code>get_awaitable</code></p>
 
<div class="versionadded">
 
 
 
<p><span class="versionmodified added">3.5 新版功能.</span></p>
 
 
 
</div></dd></dl>
 
 
 
<dl>
 
<dt><code>END_ASYNC_FOR</code></dt>
 
<dd><p>Terminates an [[../../reference/compound_stmts#async-for|<code>async for</code>]] loop. Handles an exception raised
 
when awaiting a next item. If TOS is [[../exceptions#StopAsyncIteration|<code>StopAsyncIteration</code>]] pop 7
 
values from the stack and restore the exception state using the second
 
three of them. Otherwise re-raise the exception using the three values
 
from the stack. An exception handler block is removed from the block stack.</p>
 
<div class="versionadded">
 
 
 
<p><span class="versionmodified added">3.8 新版功能.</span></p>
 
 
 
</div></dd></dl>
 
 
 
<dl>
 
<dt><code>BEFORE_ASYNC_WITH</code></dt>
 
<dd><p>Resolves <code>__aenter__</code> and <code>__aexit__</code> from the object on top of the
 
stack. Pushes <code>__aexit__</code> and result of <code>__aenter__()</code> to the stack.</p>
 
<div class="versionadded">
 
  
<p><span class="versionmodified added">3.5 新版功能.</span></p>
+
; <span class="sig-prename descclassname"><span class="pre">dis.</span></span><span class="sig-name descname"><span class="pre">opmap</span></span>
 +
: 字典将操作名称映射到字节码。
  
</div></dd></dl>
+
; <span class="sig-prename descclassname"><span class="pre">dis.</span></span><span class="sig-name descname"><span class="pre">cmp_op</span></span>
 +
: 所有比较操作名称的顺序。
  
<dl>
+
; <span class="sig-prename descclassname"><span class="pre">dis.</span></span><span class="sig-name descname"><span class="pre">hasconst</span></span>
<dt><code>SETUP_ASYNC_WITH</code></dt>
+
: 访问常量的字节码序列。
<dd><p>Creates a new frame object.</p>
 
<div class="versionadded">
 
  
<p><span class="versionmodified added">3.5 新版功能.</span></p>
+
; <span class="sig-prename descclassname"><span class="pre">dis.</span></span><span class="sig-name descname"><span class="pre">hasfree</span></span>
 +
: 访问自由变量的字节码序列(请注意,此上下文中的“自由”指的是当前作用域中由内部作用域引用的名称或从此作用域引用的外部作用域中的名称。 它确实 ''不'' 包括对全局或内置范围的引用)。
  
</div></dd></dl>
+
; <span class="sig-prename descclassname"><span class="pre">dis.</span></span><span class="sig-name descname"><span class="pre">hasname</span></span>
 +
: 按名称访问属性的字节码序列。
  
'''Miscellaneous opcodes'''
+
; <span class="sig-prename descclassname"><span class="pre">dis.</span></span><span class="sig-name descname"><span class="pre">hasjrel</span></span>
 +
: 具有相对跳转目标的字节码序列。
  
; <code>PRINT_EXPR</code>
+
; <span class="sig-prename descclassname"><span class="pre">dis.</span></span><span class="sig-name descname"><span class="pre">hasjabs</span></span>
: Implements the expression statement for the interactive mode. TOS is removed from the stack and printed. In non-interactive mode, an expression statement is terminated with [[#opcode-POP_TOP|<code>POP_TOP</code>]].
+
: 具有绝对跳转目标的字节码序列。
  
; <code>SET_ADD</code><span class="sig-paren">(</span>''i''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">dis.</span></span><span class="sig-name descname"><span class="pre">haslocal</span></span>
: Calls <code>set.add(TOS1[-i], TOS)</code>. Used to implement set comprehensions.
+
: 访问局部变量的字节码序列。
  
; <code>LIST_APPEND</code><span class="sig-paren">(</span>''i''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">dis.</span></span><span class="sig-name descname"><span class="pre">hascompare</span></span>
: Calls <code>list.append(TOS1[-i], TOS)</code>. Used to implement list comprehensions.
+
: 布尔运算的字节码序列。
  
<dl>
 
<dt><code>MAP_ADD</code><span class="sig-paren">(</span>''i''<span class="sig-paren">)</span></dt>
 
<dd><p>Calls <code>dict.__setitem__(TOS1[-i], TOS1, TOS)</code>. Used to implement dict
 
comprehensions.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.1 新版功能.</span></p>
 
  
 
</div>
 
</div>
<div class="versionchanged">
 
 
<p><span class="versionmodified changed">在 3.8 版更改: </span>Map value is TOS and map key is TOS1. Before, those were reversed.</p>
 
 
</div></dd></dl>
 
 
For all of the [[#opcode-SET_ADD|<code>SET_ADD</code>]], [[#opcode-LIST_APPEND|<code>LIST_APPEND</code>]] and [[#opcode-MAP_ADD|<code>MAP_ADD</code>]]
 
instructions, while the added value or key/value pair is popped off, the
 
container object remains on the stack so that it is available for further
 
iterations of the loop.
 
 
; <code>RETURN_VALUE</code>
 
: Returns with TOS to the caller of the function.
 
 
; <code>YIELD_VALUE</code>
 
: Pops TOS and yields it from a [[../../glossary#term-generator|<span class="xref std std-term">generator</span>]].
 
 
<dl>
 
<dt><code>YIELD_FROM</code></dt>
 
<dd><p>Pops TOS and delegates to it as a subiterator from a [[../../glossary#term-generator|<span class="xref std std-term">generator</span>]].</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.3 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
<dl>
 
<dt><code>SETUP_ANNOTATIONS</code></dt>
 
<dd><p>Checks whether <code>__annotations__</code> is defined in <code>locals()</code>, if not it is
 
set up to an empty <code>dict</code>. This opcode is only emitted if a class
 
or module body contains [[../../glossary#term-variable-annotation|<span class="xref std std-term">variable annotations</span>]]
 
statically.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.6 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
; <code>IMPORT_STAR</code>
 
: Loads all symbols not starting with <code>'_'</code> directly from the module TOS to the local namespace. The module is popped after loading all names. This opcode implements <code>from module import *</code>.
 
 
; <code>POP_BLOCK</code>
 
: Removes one block from the block stack. Per frame, there is a stack of blocks, denoting [[../../reference/compound_stmts#try|<code>try</code>]] statements, and such.
 
 
; <code>POP_EXCEPT</code>
 
: Removes one block from the block stack. The popped block must be an exception handler block, as implicitly created when entering an except handler. In addition to popping extraneous values from the frame stack, the last three popped values are used to restore the exception state.
 
 
<dl>
 
<dt><code>RERAISE</code></dt>
 
<dd><p>Re-raises the exception currently on top of the stack.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.9 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
<dl>
 
<dt><code>WITH_EXCEPT_START</code></dt>
 
<dd><p>Calls the function in position 7 on the stack with the top three
 
items on the stack as arguments.
 
Used to implement the call <code>context_manager.__exit__(*exc_info())</code> when an exception
 
has occurred in a [[../../reference/compound_stmts#with|<code>with</code>]] statement.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.9 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
<dl>
 
<dt><code>LOAD_ASSERTION_ERROR</code></dt>
 
<dd><p>Pushes [[../exceptions#AssertionError|<code>AssertionError</code>]] onto the stack. Used by the [[../../reference/simple_stmts#assert|<code>assert</code>]]
 
statement.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.9 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
; <code>LOAD_BUILD_CLASS</code>
 
: Pushes <code>builtins.__build_class__()</code> onto the stack. It is later called by [[#opcode-CALL_FUNCTION|<code>CALL_FUNCTION</code>]] to construct a class.
 
 
<dl>
 
<dt><code>SETUP_WITH</code><span class="sig-paren">(</span>''delta''<span class="sig-paren">)</span></dt>
 
<dd><p>This opcode performs several operations before a with block starts. First,
 
it loads [[../../reference/datamodel#object|<code>__exit__()</code>]] from the context manager and pushes it onto
 
the stack for later use by <code>WITH_CLEANUP_START</code>. Then,
 
[[../../reference/datamodel#object|<code>__enter__()</code>]] is called, and a finally block pointing to ''delta''
 
is pushed. Finally, the result of calling the <code>__enter__()</code> method is pushed onto
 
the stack. The next opcode will either ignore it ([[#opcode-POP_TOP|<code>POP_TOP</code>]]), or
 
store it in (a) variable(s) ([[#opcode-STORE_FAST|<code>STORE_FAST</code>]], [[#opcode-STORE_NAME|<code>STORE_NAME</code>]], or
 
[[#opcode-UNPACK_SEQUENCE|<code>UNPACK_SEQUENCE</code>]]).</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.2 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
All of the following opcodes use their arguments.
 
 
; <code>STORE_NAME</code><span class="sig-paren">(</span>''namei''<span class="sig-paren">)</span>
 
: Implements <code>name = TOS</code>. ''namei'' is the index of ''name'' in the attribute <code>co_names</code> of the code object. The compiler tries to use [[#opcode-STORE_FAST|<code>STORE_FAST</code>]] or [[#opcode-STORE_GLOBAL|<code>STORE_GLOBAL</code>]] if possible.
 
 
; <code>DELETE_NAME</code><span class="sig-paren">(</span>''namei''<span class="sig-paren">)</span>
 
: Implements <code>del name</code>, where ''namei'' is the index into <code>co_names</code> attribute of the code object.
 
 
; <code>UNPACK_SEQUENCE</code><span class="sig-paren">(</span>''count''<span class="sig-paren">)</span>
 
: Unpacks TOS into ''count'' individual values, which are put onto the stack right-to-left.
 
 
<dl>
 
<dt><code>UNPACK_EX</code><span class="sig-paren">(</span>''counts''<span class="sig-paren">)</span></dt>
 
<dd><p>Implements assignment with a starred target: Unpacks an iterable in TOS into
 
individual values, where the total number of values can be smaller than the
 
number of items in the iterable: one of the new values will be a list of all
 
leftover items.</p>
 
<p>The low byte of ''counts'' is the number of values before the list value, the
 
high byte of ''counts'' the number of values after it. The resulting values
 
are put onto the stack right-to-left.</p></dd></dl>
 
 
; <code>STORE_ATTR</code><span class="sig-paren">(</span>''namei''<span class="sig-paren">)</span>
 
: Implements <code>TOS.name = TOS1</code>, where ''namei'' is the index of name in <code>co_names</code>.
 
 
; <code>DELETE_ATTR</code><span class="sig-paren">(</span>''namei''<span class="sig-paren">)</span>
 
: Implements <code>del TOS.name</code>, using ''namei'' as index into <code>co_names</code>.
 
 
; <code>STORE_GLOBAL</code><span class="sig-paren">(</span>''namei''<span class="sig-paren">)</span>
 
: Works as [[#opcode-STORE_NAME|<code>STORE_NAME</code>]], but stores the name as a global.
 
 
; <code>DELETE_GLOBAL</code><span class="sig-paren">(</span>''namei''<span class="sig-paren">)</span>
 
: Works as [[#opcode-DELETE_NAME|<code>DELETE_NAME</code>]], but deletes a global name.
 
 
; <code>LOAD_CONST</code><span class="sig-paren">(</span>''consti''<span class="sig-paren">)</span>
 
: Pushes <code>co_consts[consti]</code> onto the stack.
 
 
; <code>LOAD_NAME</code><span class="sig-paren">(</span>''namei''<span class="sig-paren">)</span>
 
: Pushes the value associated with <code>co_names[namei]</code> onto the stack.
 
 
; <code>BUILD_TUPLE</code><span class="sig-paren">(</span>''count''<span class="sig-paren">)</span>
 
: Creates a tuple consuming ''count'' items from the stack, and pushes the resulting tuple onto the stack.
 
 
; <code>BUILD_LIST</code><span class="sig-paren">(</span>''count''<span class="sig-paren">)</span>
 
: Works as [[#opcode-BUILD_TUPLE|<code>BUILD_TUPLE</code>]], but creates a list.
 
 
; <code>BUILD_SET</code><span class="sig-paren">(</span>''count''<span class="sig-paren">)</span>
 
: Works as [[#opcode-BUILD_TUPLE|<code>BUILD_TUPLE</code>]], but creates a set.
 
 
<dl>
 
<dt><code>BUILD_MAP</code><span class="sig-paren">(</span>''count''<span class="sig-paren">)</span></dt>
 
<dd><p>Pushes a new dictionary object onto the stack. Pops <code>2 * count</code> items
 
so that the dictionary holds ''count'' entries:
 
<code>{..., TOS3: TOS2, TOS1: TOS}</code>.</p>
 
<div class="versionchanged">
 
 
<p><span class="versionmodified changed">在 3.5 版更改: </span>The dictionary is created from stack items instead of creating an
 
empty dictionary pre-sized to hold ''count'' items.</p>
 
 
</div></dd></dl>
 
 
<dl>
 
<dt><code>BUILD_CONST_KEY_MAP</code><span class="sig-paren">(</span>''count''<span class="sig-paren">)</span></dt>
 
<dd><p>The version of [[#opcode-BUILD_MAP|<code>BUILD_MAP</code>]] specialized for constant keys. Pops the
 
top element on the stack which contains a tuple of keys, then starting from
 
<code>TOS1</code>, pops ''count'' values to form values in the built dictionary.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.6 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
<dl>
 
<dt><code>BUILD_STRING</code><span class="sig-paren">(</span>''count''<span class="sig-paren">)</span></dt>
 
<dd><p>Concatenates ''count'' strings from the stack and pushes the resulting string
 
onto the stack.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.6 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
<dl>
 
<dt><code>LIST_TO_TUPLE</code></dt>
 
<dd><p>Pops a list from the stack and pushes a tuple containing the same values.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.9 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
<dl>
 
<dt><code>LIST_EXTEND</code><span class="sig-paren">(</span>''i''<span class="sig-paren">)</span></dt>
 
<dd><p>Calls <code>list.extend(TOS1[-i], TOS)</code>. Used to build lists.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.9 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
<dl>
 
<dt><code>SET_UPDATE</code><span class="sig-paren">(</span>''i''<span class="sig-paren">)</span></dt>
 
<dd><p>Calls <code>set.update(TOS1[-i], TOS)</code>. Used to build sets.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.9 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
<dl>
 
<dt><code>DICT_UPDATE</code><span class="sig-paren">(</span>''i''<span class="sig-paren">)</span></dt>
 
<dd><p>Calls <code>dict.update(TOS1[-i], TOS)</code>. Used to build dicts.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.9 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
<dl>
 
<dt><code>DICT_MERGE</code></dt>
 
<dd><p>Like [[#opcode-DICT_UPDATE|<code>DICT_UPDATE</code>]] but raises an exception for duplicate keys.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.9 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
; <code>LOAD_ATTR</code><span class="sig-paren">(</span>''namei''<span class="sig-paren">)</span>
 
: Replaces TOS with <code>getattr(TOS, co_names[namei])</code>.
 
 
; <code>COMPARE_OP</code><span class="sig-paren">(</span>''opname''<span class="sig-paren">)</span>
 
: Performs a Boolean operation. The operation name can be found in <code>cmp_op[opname]</code>.
 
 
<dl>
 
<dt><code>IS_OP</code><span class="sig-paren">(</span>''invert''<span class="sig-paren">)</span></dt>
 
<dd><p>Performs <code>is</code> comparison, or <code>is not</code> if <code>invert</code> is 1.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.9 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
<dl>
 
<dt><code>CONTAINS_OP</code><span class="sig-paren">(</span>''invert''<span class="sig-paren">)</span></dt>
 
<dd><p>Performs <code>in</code> comparison, or <code>not in</code> if <code>invert</code> is 1.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.9 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
; <code>IMPORT_NAME</code><span class="sig-paren">(</span>''namei''<span class="sig-paren">)</span>
 
: Imports the module <code>co_names[namei]</code>. TOS and TOS1 are popped and provide the ''fromlist'' and ''level'' arguments of [[../functions#import__|<code>__import__()</code>]]. The module object is pushed onto the stack. The current namespace is not affected: for a proper import statement, a subsequent [[#opcode-STORE_FAST|<code>STORE_FAST</code>]] instruction modifies the namespace.
 
 
; <code>IMPORT_FROM</code><span class="sig-paren">(</span>''namei''<span class="sig-paren">)</span>
 
: Loads the attribute <code>co_names[namei]</code> from the module found in TOS. The resulting object is pushed onto the stack, to be subsequently stored by a [[#opcode-STORE_FAST|<code>STORE_FAST</code>]] instruction.
 
 
; <code>JUMP_FORWARD</code><span class="sig-paren">(</span>''delta''<span class="sig-paren">)</span>
 
: Increments bytecode counter by ''delta''.
 
 
<dl>
 
<dt><code>POP_JUMP_IF_TRUE</code><span class="sig-paren">(</span>''target''<span class="sig-paren">)</span></dt>
 
<dd><p>If TOS is true, sets the bytecode counter to ''target''. TOS is popped.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.1 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
<dl>
 
<dt><code>POP_JUMP_IF_FALSE</code><span class="sig-paren">(</span>''target''<span class="sig-paren">)</span></dt>
 
<dd><p>If TOS is false, sets the bytecode counter to ''target''. TOS is popped.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.1 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
<dl>
 
<dt><code>JUMP_IF_NOT_EXC_MATCH</code><span class="sig-paren">(</span>''target''<span class="sig-paren">)</span></dt>
 
<dd><p>Tests whether the second value on the stack is an exception matching TOS,
 
and jumps if it is not. Pops two values from the stack.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.9 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
<dl>
 
<dt><code>JUMP_IF_TRUE_OR_POP</code><span class="sig-paren">(</span>''target''<span class="sig-paren">)</span></dt>
 
<dd><p>If TOS is true, sets the bytecode counter to ''target'' and leaves TOS on the
 
stack. Otherwise (TOS is false), TOS is popped.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.1 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
<dl>
 
<dt><code>JUMP_IF_FALSE_OR_POP</code><span class="sig-paren">(</span>''target''<span class="sig-paren">)</span></dt>
 
<dd><p>If TOS is false, sets the bytecode counter to ''target'' and leaves TOS on the
 
stack. Otherwise (TOS is true), TOS is popped.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.1 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
; <code>JUMP_ABSOLUTE</code><span class="sig-paren">(</span>''target''<span class="sig-paren">)</span>
 
: Set bytecode counter to ''target''.
 
 
; <code>FOR_ITER</code><span class="sig-paren">(</span>''delta''<span class="sig-paren">)</span>
 
: TOS is an [[../../glossary#term-iterator|<span class="xref std std-term">iterator</span>]]. Call its [[../stdtypes#iterator|<code>__next__()</code>]] method. If this yields a new value, push it on the stack (leaving the iterator below it). If the iterator indicates it is exhausted, TOS is popped, and the byte code counter is incremented by ''delta''.
 
 
; <code>LOAD_GLOBAL</code><span class="sig-paren">(</span>''namei''<span class="sig-paren">)</span>
 
: Loads the global named <code>co_names[namei]</code> onto the stack.
 
 
; <code>SETUP_FINALLY</code><span class="sig-paren">(</span>''delta''<span class="sig-paren">)</span>
 
: Pushes a try block from a try-finally or try-except clause onto the block stack. ''delta'' points to the finally block or the first except block.
 
 
; <code>LOAD_FAST</code><span class="sig-paren">(</span>''var_num''<span class="sig-paren">)</span>
 
: Pushes a reference to the local <code>co_varnames[var_num]</code> onto the stack.
 
 
; <code>STORE_FAST</code><span class="sig-paren">(</span>''var_num''<span class="sig-paren">)</span>
 
: Stores TOS into the local <code>co_varnames[var_num]</code>.
 
 
; <code>DELETE_FAST</code><span class="sig-paren">(</span>''var_num''<span class="sig-paren">)</span>
 
: Deletes local <code>co_varnames[var_num]</code>.
 
 
; <code>LOAD_CLOSURE</code><span class="sig-paren">(</span>''i''<span class="sig-paren">)</span>
 
: Pushes a reference to the cell contained in slot ''i'' of the cell and free variable storage. The name of the variable is <code>co_cellvars[i]</code> if ''i'' is less than the length of ''co_cellvars''. Otherwise it is <code>co_freevars[i - len(co_cellvars)]</code>.
 
 
; <code>LOAD_DEREF</code><span class="sig-paren">(</span>''i''<span class="sig-paren">)</span>
 
: Loads the cell contained in slot ''i'' of the cell and free variable storage. Pushes a reference to the object the cell contains on the stack.
 
 
<dl>
 
<dt><code>LOAD_CLASSDEREF</code><span class="sig-paren">(</span>''i''<span class="sig-paren">)</span></dt>
 
<dd><p>Much like [[#opcode-LOAD_DEREF|<code>LOAD_DEREF</code>]] but first checks the locals dictionary before
 
consulting the cell. This is used for loading free variables in class
 
bodies.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.4 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
; <code>STORE_DEREF</code><span class="sig-paren">(</span>''i''<span class="sig-paren">)</span>
 
: Stores TOS into the cell contained in slot ''i'' of the cell and free variable storage.
 
 
<dl>
 
<dt><code>DELETE_DEREF</code><span class="sig-paren">(</span>''i''<span class="sig-paren">)</span></dt>
 
<dd><p>Empties the cell contained in slot ''i'' of the cell and free variable storage.
 
Used by the [[../../reference/simple_stmts#del|<code>del</code>]] statement.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.2 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
; <code>RAISE_VARARGS</code><span class="sig-paren">(</span>''argc''<span class="sig-paren">)</span>
 
: Raises an exception using one of the 3 forms of the <code>raise</code> statement, depending on the value of ''argc'':
 
;* 0: <code>raise</code> (re-raise previous exception)
 
;* 1: <code>raise TOS</code> (raise exception instance or type at <code>TOS</code>)
 
;* 2: <code>raise TOS1 from TOS</code> (raise exception instance or type at <code>TOS1</code> with <code>__cause__</code> set to <code>TOS</code>)
 
 
<dl>
 
<dt><code>CALL_FUNCTION</code><span class="sig-paren">(</span>''argc''<span class="sig-paren">)</span></dt>
 
<dd><p>Calls a callable object with positional arguments.
 
''argc'' indicates the number of positional arguments.
 
The top of the stack contains positional arguments, with the right-most
 
argument on top. Below the arguments is a callable object to call.
 
<code>CALL_FUNCTION</code> pops all arguments and the callable object off the stack,
 
calls the callable object with those arguments, and pushes the return value
 
returned by the callable object.</p>
 
<div class="versionchanged">
 
 
<p><span class="versionmodified changed">在 3.6 版更改: </span>This opcode is used only for calls with positional arguments.</p>
 
 
</div></dd></dl>
 
 
<dl>
 
<dt><code>CALL_FUNCTION_KW</code><span class="sig-paren">(</span>''argc''<span class="sig-paren">)</span></dt>
 
<dd><p>Calls a callable object with positional (if any) and keyword arguments.
 
''argc'' indicates the total number of positional and keyword arguments.
 
The top element on the stack contains a tuple with the names of the
 
keyword arguments, which must be strings.
 
Below that are the values for the keyword arguments,
 
in the order corresponding to the tuple.
 
Below that are positional arguments, with the right-most parameter on
 
top. Below the arguments is a callable object to call.
 
<code>CALL_FUNCTION_KW</code> pops all arguments and the callable object off the stack,
 
calls the callable object with those arguments, and pushes the return value
 
returned by the callable object.</p>
 
<div class="versionchanged">
 
 
<p><span class="versionmodified changed">在 3.6 版更改: </span>Keyword arguments are packed in a tuple instead of a dictionary,
 
''argc'' indicates the total number of arguments.</p>
 
 
</div></dd></dl>
 
 
<dl>
 
<dt><code>CALL_FUNCTION_EX</code><span class="sig-paren">(</span>''flags''<span class="sig-paren">)</span></dt>
 
<dd><p>Calls a callable object with variable set of positional and keyword
 
arguments. If the lowest bit of ''flags'' is set, the top of the stack
 
contains a mapping object containing additional keyword arguments.
 
Before the callable is called, the mapping object and iterable object
 
are each &quot;unpacked&quot; and their contents passed in as keyword and
 
positional arguments respectively.
 
<code>CALL_FUNCTION_EX</code> pops all arguments and the callable object off the stack,
 
calls the callable object with those arguments, and pushes the return value
 
returned by the callable object.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.6 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
<dl>
 
<dt><code>LOAD_METHOD</code><span class="sig-paren">(</span>''namei''<span class="sig-paren">)</span></dt>
 
<dd><p>Loads a method named <code>co_names[namei]</code> from the TOS object. TOS is popped.
 
This bytecode distinguishes two cases: if TOS has a method with the correct
 
name, the bytecode pushes the unbound method and TOS. TOS will be used as
 
the first argument (<code>self</code>) by [[#opcode-CALL_METHOD|<code>CALL_METHOD</code>]] when calling the
 
unbound method. Otherwise, <code>NULL</code> and the object return by the attribute
 
lookup are pushed.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.7 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
<dl>
 
<dt><code>CALL_METHOD</code><span class="sig-paren">(</span>''argc''<span class="sig-paren">)</span></dt>
 
<dd><p>Calls a method. ''argc'' is the number of positional arguments.
 
Keyword arguments are not supported. This opcode is designed to be used
 
with [[#opcode-LOAD_METHOD|<code>LOAD_METHOD</code>]]. Positional arguments are on top of the stack.
 
Below them, the two items described in [[#opcode-LOAD_METHOD|<code>LOAD_METHOD</code>]] are on the
 
stack (either <code>self</code> and an unbound method object or <code>NULL</code> and an
 
arbitrary callable). All of them are popped and the return value is pushed.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.7 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
; <code>MAKE_FUNCTION</code><span class="sig-paren">(</span>''flags''<span class="sig-paren">)</span>
 
: Pushes a new function object on the stack. From bottom to top, the consumed stack must consist of values if the argument carries a specified flag value
 
;* <code>0x01</code> a tuple of default values for positional-only and positional-or-keyword parameters in positional order
 
;* <code>0x02</code> a dictionary of keyword-only parameters' default values
 
;* <code>0x04</code> an annotation dictionary
 
;* <code>0x08</code> a tuple containing cells for free variables, making a closure
 
;* the code associated with the function (at TOS1)
 
;* the [[../../glossary#term-qualified-name|<span class="xref std std-term">qualified name</span>]] of the function (at TOS)
 
 
; <code>BUILD_SLICE</code><span class="sig-paren">(</span>''argc''<span class="sig-paren">)</span>
 
: Pushes a slice object on the stack. ''argc'' must be 2 or 3. If it is 2, <code>slice(TOS1, TOS)</code> is pushed; if it is 3, <code>slice(TOS2, TOS1, TOS)</code> is pushed. See the [[../functions#slice|<code>slice()</code>]] built-in function for more information.
 
 
; <code>EXTENDED_ARG</code><span class="sig-paren">(</span>''ext''<span class="sig-paren">)</span>
 
: Prefixes any opcode which has an argument too big to fit into the default one byte. ''ext'' holds an additional byte which act as higher bits in the argument. For each opcode, at most three prefixal <code>EXTENDED_ARG</code> are allowed, forming an argument from two-byte to four-byte.
 
 
<dl>
 
<dt><code>FORMAT_VALUE</code><span class="sig-paren">(</span>''flags''<span class="sig-paren">)</span></dt>
 
<dd><p>Used for implementing formatted literal strings (f-strings). Pops
 
an optional ''fmt_spec'' from the stack, then a required ''value''.
 
''flags'' is interpreted as follows:</p>
 
<ul>
 
<li><p><code>(flags &amp; 0x03) == 0x00</code>: ''value'' is formatted as-is.</p></li>
 
<li><p><code>(flags &amp; 0x03) == 0x01</code>: call [[../stdtypes#str|<code>str()</code>]] on ''value'' before
 
formatting it.</p></li>
 
<li><p><code>(flags &amp; 0x03) == 0x02</code>: call [[../functions#repr|<code>repr()</code>]] on ''value'' before
 
formatting it.</p></li>
 
<li><p><code>(flags &amp; 0x03) == 0x03</code>: call [[../functions#ascii|<code>ascii()</code>]] on ''value'' before
 
formatting it.</p></li>
 
<li><p><code>(flags &amp; 0x04) == 0x04</code>: pop ''fmt_spec'' from the stack and use
 
it, else use an empty ''fmt_spec''.</p></li></ul>
 
 
<p>Formatting is performed using <code>PyObject_Format()</code>. The
 
result is pushed on the stack.</p>
 
<div class="versionadded">
 
 
<p><span class="versionmodified added">3.6 新版功能.</span></p>
 
 
</div></dd></dl>
 
 
<dl>
 
<dt><code>HAVE_ARGUMENT</code></dt>
 
<dd><p>This is not really an opcode. It identifies the dividing line between
 
opcodes which don't use their argument and those that do
 
(<code>&lt; HAVE_ARGUMENT</code> and <code>&gt;= HAVE_ARGUMENT</code>, respectively).</p>
 
<div class="versionchanged">
 
 
<p><span class="versionmodified changed">在 3.6 版更改: </span>Now every instruction has an argument, but opcodes <code>&lt; HAVE_ARGUMENT</code>
 
ignore it. Before, only opcodes <code>&gt;= HAVE_ARGUMENT</code> had an argument.</p>
 
 
</div></dd></dl>
 
 
  
 
</div>
 
</div>
<div id="opcode-collections" class="section">
+
<div class="clearer">
  
<span id="id1"></span>
 
== Opcode collections ==
 
  
These collections are provided for automatic introspection of bytecode
 
instructions:
 
 
; <code>dis.</code><code>opname</code>
 
: Sequence of operation names, indexable using the bytecode.
 
 
; <code>dis.</code><code>opmap</code>
 
: Dictionary mapping operation names to bytecodes.
 
 
; <code>dis.</code><code>cmp_op</code>
 
: Sequence of all compare operation names.
 
 
; <code>dis.</code><code>hasconst</code>
 
: Sequence of bytecodes that access a constant.
 
 
; <code>dis.</code><code>hasfree</code>
 
: Sequence of bytecodes that access a free variable (note that 'free' in this context refers to names in the current scope that are referenced by inner scopes or names in outer scopes that are referenced from this scope. It does ''not'' include references to global or builtin scopes).
 
 
; <code>dis.</code><code>hasname</code>
 
: Sequence of bytecodes that access an attribute by name.
 
 
; <code>dis.</code><code>hasjrel</code>
 
: Sequence of bytecodes that have a relative jump target.
 
 
; <code>dis.</code><code>hasjabs</code>
 
: Sequence of bytecodes that have an absolute jump target.
 
 
; <code>dis.</code><code>haslocal</code>
 
: Sequence of bytecodes that access a local variable.
 
 
; <code>dis.</code><code>hascompare</code>
 
: Sequence of bytecodes of Boolean operations.
 
 
 
</div>
 
  
 
</div>
 
</div>
  
[[Category:Python 3.9 中文文档]]
+
[[Category:Python 3.9 文档]]

2021年10月31日 (日) 04:51的最新版本

dis — Python 字节码的反汇编器

源代码: :source:`Lib/dis.py`



dis 模块支持对 CPython bytecode 进行反汇编分析。 该模块作为输入的 CPython 字节码在文件 Include/opcode.h 中定义,并由编译器和解释器使用。

示例:给定函数 myfunc()

def myfunc(alist):
    return len(alist)

以下命令可用于显示myfunc()的反汇编:

>>> dis.dis(myfunc)
  2           0 LOAD_GLOBAL              0 (len)
              2 LOAD_FAST                0 (alist)
              4 CALL_FUNCTION            1
              6 RETURN_VALUE

(“2”是行号)。

字节码分析

3.4 版中的新功能。


字节码分析 API 允许将 Python 代码片段包装在 Bytecode 对象中,以便轻松访问已编译代码的详细信息。

class dis.Bytecode(x, *, first_line=None, current_offset=None)

分析与函数、生成器、异步生成器、协程、方法、源代码字符串或代码对象(由 compile() 返回)对应的字节码。

这是对下面列出的许多函数的便利包装,最显着的是 get_instructions(),因为迭代 Bytecode 实例将字节码操作生成为 Instruction 实例.

如果 first_line 不是 None,则表示反汇编代码中第一个源代码行应报告的行号。 否则,源行信息(如果有)直接从反汇编的代码对象中获取。

如果 current_offset 不是 None,则指的是反汇编代码中的指令偏移量。 设置这意味着 dis() 将针对指定的操作码显示“当前指令”标记。

classmethod from_traceback(tb)

从给定的回溯构造一个 Bytecode 实例,将 current_offset 设置为负责异常的指令。

codeobj

编译后的代码对象。

first_line

代码对象的第一行源代码(如果可用)

dis()

返回字节码操作的格式化视图(与 dis.dis() 打印的相同,但作为多行字符串返回)。

info()

返回一个格式化的多行字符串,其中包含有关代码对象的详细信息,例如 code_info()

3.7 版更改: 现在可以处理协程和异步生成器对象。

例子:

>>> bytecode = dis.Bytecode(myfunc)
>>> for instr in bytecode:
...     print(instr.opname)
...
LOAD_GLOBAL
LOAD_FAST
CALL_FUNCTION
RETURN_VALUE

分析功能

dis 模块还定义了以下分析函数,将输入直接转换为所需的输出。 如果仅执行单个操作,它们可能很有用,因此中间分析对象没有用:

dis.code_info(x)

返回一个格式化的多行字符串,其中包含提供的函数、生成器、异步生成器、协程、方法、源代码字符串或代码对象的详细代码对象信息。

请注意,代码信息字符串的确切内容高度依赖于实现,并且它们可能会在 Python VM 或 Python 版本之间任意更改。

3.2 版中的新功能。

3.7 版更改: 现在可以处理协程和异步生成器对象。

dis.show_code(x, *, file=None)

将提供的函数、方法、源代码字符串或代码对象的详细代码对象信息打印到 file(如果未指定 file,则为 sys.stdout)。

这是 print(code_info(x), file=file) 的便捷简写,用于在解释器提示下进行交互式探索。

3.2 版中的新功能。

3.4 版更改: 添加 文件 参数。

dis.dis(x=None, *, file=None, depth=None)

拆解 x 对象。 x 可以表示模块、类、方法、函数、生成器、异步生成器、协程、代码对象、源代码字符串或原始字节码的字节序列。 对于一个模块,它反汇编了所有功能。 对于一个类,它反汇编所有方法(包括类和静态方法)。 对于代码对象或原始字节码序列,它为每个字节码指令打印一行。 它还递归地反汇编嵌套代码对象(推导式、生成器表达式和嵌套函数的代码,以及用于构建嵌套类的代码)。 在反汇编之前,首先使用 compile() 内置函数将字符串编译为代码对象。 如果没有提供对象,这个函数反汇编最后的回溯。

如果提供,反汇编将作为文本写入提供的 file 参数,否则写入 sys.stdout

递归的最大深度受 depth 的限制,除非它是 Nonedepth=0 表示没有递归。

3.4 版更改: 添加 文件 参数。

3.7 版本更改: 实现递归反汇编并添加 depth 参数。

3.7 版更改: 现在可以处理协程和异步生成器对象。

dis.distb(tb=None, *, file=None)

反汇编回溯的栈顶函数,如果没有传递,则使用最后一个回溯。 指示导致异常的指令。

如果提供,反汇编将作为文本写入提供的 file 参数,否则写入 sys.stdout

3.4 版更改: 添加 文件 参数。

dis.disassemble(code, lasti=- 1, *, file=None)
dis.disco(code, lasti=- 1, *, file=None)

反汇编代码对象,如果提供了 lasti,则指示最后一条指令。 输出分为以下列:

  1. 行号,用于每行的第一条指令

  2. 当前指令,表示为-->

  3. 一个带标签的指令,用 >> 表示,

  4. 指令的地址,

  5. 操作代码名称,

  6. 操作参数,和

  7. 括号中参数的解释。

参数解释识别局部和全局变量名称、常量值、分支目标和比较运算符。

如果提供,反汇编将作为文本写入提供的 file 参数,否则写入 sys.stdout

3.4 版更改: 添加 文件 参数。

dis.get_instructions(x, *, first_line=None)

在提供的函数、方法、源代码字符串或代码对象中的指令上返回迭代器。

迭代器生成一系列 Instruction 命名元组,提供所提供代码中每个操作的详细信息。

如果 first_line 不是 None,则表示反汇编代码中第一个源代码行应报告的行号。 否则,源行信息(如果有)直接从反汇编的代码对象中获取。

3.4 版中的新功能。

dis.findlinestarts(code)

此生成器函数使用代码对象 codeco_firstlinenoco_lnotab 属性来查找作为源代码中行开头的偏移量。 它们生成为 (offset, lineno) 对。 请参阅 :source:`Objects/lnotab_notes.txt` 了解 co_lnotab 格式以及如何对其进行解码。

3.6 版更改: 行号可以减少。 以前,它们总是在增加。

dis.findlabels(code)
检测原始编译字节码字符串 code 中作为跳转目标的所有偏移量,并返回这些偏移量的列表。
dis.stack_effect(opcode, oparg=None, *, jump=None)

使用参数 oparg 计算 opcode 的堆栈效果。

如果代码有跳转目标且jumpTrue,则stack_effect()会返回跳转的堆栈效果。 如果jumpFalse,则返回不跳跃的叠加效果。 如果 jumpNone(默认),它将返回两种情况下的最大堆栈效果。

3.4 版中的新功能。

3.8 版更改: 添加 jump 参数。


Python 字节码说明

get_instructions() 函数和 Bytecode 类提供字节码指令的详细信息作为 Instruction 实例:

class dis.Instruction

字节码操作的详细信息

opcode

操作的数字代码,对应于下面列出的操作码值和 操作码集合 中的字节码值。

opname

人类可读的操作名称

arg

操作的数字参数(如果有),否则为 None

argval

解析的 arg 值(如果已知),否则与 arg 相同

argrepr

操作参数的人类可读描述

offset

字节码序列中操作的起始索引

starts_line

以此操作码开头的行(如果有),否则为 None

is_jump_target

True 如果其他代码跳转到这里,否则 False

3.4 版中的新功能。

Python 编译器当前生成以下字节码指令。

一般说明

一元运算

一元操作取栈顶,应用操作,并将结果压回到栈上。

二元运算

二元运算从堆栈中删除堆栈顶部 (TOS) 和第二个最顶部堆栈项 (TOS1)。 他们执行操作,并将结果放回堆栈中。

就地操作

就地操作类似于二元操作,因为它们删除 TOS 和 TOS1,并将结果推回到堆栈上,但是当 TOS1 支持时,操作就地完成,结果 TOS 可能(但没有是)原来的TOS1。

协程操作码

杂项操作码

对于所有的 :opcode:`SET_ADD`, :opcode:`LIST_APPEND`:opcode:`MAP_ADD` 指令,而添加的值或键/value 对被弹出,容器对象保留在堆栈上,以便它可用于循环的进一步迭代。

以下所有操作码都使用它们的参数。


操作码集合

这些集合用于自动检查字节码指令:

dis.opname
操作名称序列,可使用字节码索引。
dis.opmap
字典将操作名称映射到字节码。
dis.cmp_op
所有比较操作名称的顺序。
dis.hasconst
访问常量的字节码序列。
dis.hasfree
访问自由变量的字节码序列(请注意,此上下文中的“自由”指的是当前作用域中由内部作用域引用的名称或从此作用域引用的外部作用域中的名称。 它确实 包括对全局或内置范围的引用)。
dis.hasname
按名称访问属性的字节码序列。
dis.hasjrel
具有相对跳转目标的字节码序列。
dis.hasjabs
具有绝对跳转目标的字节码序列。
dis.haslocal
访问局部变量的字节码序列。
dis.hascompare
布尔运算的字节码序列。