dis --- Python 字節(jié)碼反匯編器?

Source code: Lib/dis.py


dis 模塊通過反匯編支持CPython的 bytecode 分析。該模塊作為輸入的 CPython 字節(jié)碼在文件 Include/opcode.h 中定義,并由編譯器和解釋器使用。

CPython implementation detail: 字節(jié)碼是 CPython 解釋器的實現(xiàn)細(xì)節(jié)。不保證不會在Python版本之間添加、刪除或更改字節(jié)碼。不應(yīng)考慮將此模塊的跨 Python VM 或 Python 版本的使用。

在 3.6 版更改: 每條指令使用2個字節(jié)。以前字節(jié)數(shù)因指令而異。

在 3.11 版更改: Some instructions are accompanied by one or more inline cache entries, which take the form of CACHE instructions. These instructions are hidden by default, but can be shown by passing show_caches=True to any dis utility.

示例:給出函數(shù) myfunc():

def myfunc(alist):
    return len(alist)

可以使用以下命令顯示 myfunc() 的反匯編

>>>
>>> dis.dis(myfunc)
  1           0 RESUME                   0

  2           2 PUSH_NULL
              4 LOAD_GLOBAL              1 (NULL + len)
              6 LOAD_FAST                0 (alist)
              8 CALL                     1
             18 RETURN_VALUE

("2" 是行號)。

字節(jié)碼分析?

3.4 新版功能.

字節(jié)碼分析 API 允許將 Python 代碼片段包裝在 Bytecode 對象中,以便輕松訪問已編譯代碼的詳細(xì)信息。

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

分析的字節(jié)碼對應(yīng)于函數(shù)、生成器、異步生成器、協(xié)程、方法、源代碼字符串或代碼對象(由 compile() 返回)。

這是下面列出的許多函數(shù)的便利包裝,最值得注意的是 get_instructions() ,迭代于 Bytecode 的實例產(chǎn)生字節(jié)碼操作 Instruction 的實例。

如果 first_line 不是 None ,則表示應(yīng)該為反匯編代碼中的第一個源代碼行報告的行號。否則,源行信息(如果有的話)直接來自反匯編的代碼對象。

如果 current_offset 不是 None ,則它指的是反匯編代碼中的指令偏移量。設(shè)置它意味著 dis() 將針對指定的操作碼顯示“當(dāng)前指令”標(biāo)記。

classmethod from_traceback(tb, *, show_caches=False)?

從給定回溯構(gòu)造一個 Bytecode 實例,將設(shè)置 current_offset 為異常負(fù)責(zé)的指令。

codeobj?

已編譯的代碼對象。

first_line?

代碼對象的第一個源代碼行(如果可用)

dis()?

返回字節(jié)碼操作的格式化視圖(與 dis.dis() 打印相同,但作為多行字符串返回)。

info()?

返回帶有關(guān)于代碼對象的詳細(xì)信息的格式化多行字符串,如 code_info() 。

在 3.7 版更改: 現(xiàn)在可以處理協(xié)程和異步生成器對象。

在 3.11 版更改: Added the show_caches parameter.

示例:

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

分析函數(shù)?

dis 模塊還定義了以下分析函數(shù),它們將輸入直接轉(zhuǎn)換為所需的輸出。如果只執(zhí)行單個操作,它們可能很有用,因此中間分析對象沒用:

dis.code_info(x)?

返回格式化的多行字符串,其包含詳細(xì)代碼對象信息的用于被提供的函數(shù)、生成器、異步生成器、協(xié)程、方法、源代碼字符串或代碼對象。

請注意,代碼信息字符串的確切內(nèi)容是高度依賴于實現(xiàn)的,它們可能會在Python VM或Python版本中任意更改。

3.2 新版功能.

在 3.7 版更改: 現(xiàn)在可以處理協(xié)程和異步生成器對象。

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

將提供的函數(shù)、方法。源代碼字符串或代碼對象的詳細(xì)代碼對象信息打印到 file (如果未指定 file ,則為 sys.stdout )。

這是 print(code_info(x), file=file) 的便捷簡寫,用于在解釋器提示符下進(jìn)行交互式探索。

3.2 新版功能.

在 3.4 版更改: 添加 file 形參。

dis.dis(x=None, *, file=None, depth=None, show_caches=False)?

反匯編 x 對象。 x 可以表示模塊、類、方法、函數(shù)、生成器、異步生成器、協(xié)程、代碼對象、源代碼字符串或原始字節(jié)碼的字節(jié)序列。對于模塊,它會反匯編所有功能。對于一個類,它反匯編所有方法(包括類和靜態(tài)方法)。對于代碼對象或原始字節(jié)碼序列,它每字節(jié)碼指令打印一行。它還遞歸地反匯編嵌套代碼對象(推導(dǎo)式代碼,生成器表達(dá)式和嵌套函數(shù),以及用于構(gòu)建嵌套類的代碼)。在被反匯編之前,首先使用 compile() 內(nèi)置函數(shù)將字符串編譯為代碼對象。如果未提供任何對象,則此函數(shù)會反匯編最后一次回溯。

如果提供的話,反匯編將作為文本寫入提供的 file 參數(shù),否則寫入 sys.stdout

遞歸的最大深度受 depth 限制,除非它是 None 。 depth=0 表示沒有遞歸。

在 3.4 版更改: 添加 file 形參。

在 3.7 版更改: 實現(xiàn)了遞歸反匯編并添加了 depth 參數(shù)。

在 3.7 版更改: 現(xiàn)在可以處理協(xié)程和異步生成器對象。

在 3.11 版更改: Added the show_caches parameter.

dis.distb(tb=None, *, file=None, show_caches=False)?

如果沒有傳遞,則使用最后一個回溯來反匯編回溯的堆棧頂部函數(shù)。 指示了導(dǎo)致異常的指令。

如果提供的話,反匯編將作為文本寫入提供的 file 參數(shù),否則寫入 sys.stdout 。

在 3.4 版更改: 添加 file 形參。

在 3.11 版更改: Added the show_caches parameter.

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

反匯編代碼對象,如果提供了 lasti ,則指示最后一條指令。輸出分為以下幾列:

  1. 行號,用于每行的第一條指令

  2. 當(dāng)前指令,表示為 --> ,

  3. 一個標(biāo)記的指令,用 >> 表示,

  4. 指令的地址,

  5. 操作碼名稱,

  6. 操作參數(shù),和

  7. 括號中參數(shù)的解釋。

參數(shù)解釋識別本地和全局變量名稱、常量值、分支目標(biāo)和比較運算符。

如果提供的話,反匯編將作為文本寫入提供的 file 參數(shù),否則寫入 sys.stdout 。

在 3.4 版更改: 添加 file 形參。

在 3.11 版更改: Added the show_caches parameter.

dis.get_instructions(x, *, first_line=None, show_caches=False)?

在所提供的函數(shù)、方法、源代碼字符串或代碼對象中的指令上返回一個迭代器。

迭代器生成一系列 Instruction ,命名為元組,提供所提供代碼中每個操作的詳細(xì)信息。

如果 first_line 不是 None ,則表示應(yīng)該為反匯編代碼中的第一個源代碼行報告的行號。否則,源行信息(如果有的話)直接來自反匯編的代碼對象。

3.4 新版功能.

在 3.11 版更改: Added the show_caches parameter.

dis.findlinestarts(code)?

此生成器函數(shù)使用代碼對象 codeco_firstlinenoco_lnotab 屬性來查找源代碼中行開頭的偏移量。它們生成為 (offset, lineno) 對。請參閱 objects/lnotab_notes.txt ,了解 co_lnotab 格式以及如何解碼它。

在 3.6 版更改: 行號可能會減少。 以前,他們總是在增加。

dis.findlabels(code)?

檢測作為跳轉(zhuǎn)目標(biāo)的原始編譯后字節(jié)碼字符串 code 中的所有偏移量,并返回這些偏移量的列表。

dis.stack_effect(opcode, oparg=None, *, jump=None)?

使用參數(shù) oparg 計算 opcode 的堆棧效果。

如果代碼有一個跳轉(zhuǎn)目標(biāo)并且 jumpTrue ,則 drag_effect() 將返回跳轉(zhuǎn)的堆棧效果。如果 jumpFalse ,它將返回不跳躍的堆棧效果。如果 jumpNone (默認(rèn)值),它將返回兩種情況的最大堆棧效果。

3.4 新版功能.

在 3.8 版更改: 添加 jump 參數(shù)。

Python字節(jié)碼說明?

get_instructions() 函數(shù)和 Bytecode 類提供字節(jié)碼指令的詳細(xì)信息的 Instruction 實例:

class dis.Instruction?

字節(jié)碼操作的詳細(xì)信息

opcode?

操作的數(shù)字代碼,對應(yīng)于下面列出的操作碼值和 操作碼集合 中的字節(jié)碼值。

opname?

人類可讀的操作名稱

arg?

操作的數(shù)字參數(shù)(如果有的話),否則為 None

argval?

resolved arg value (if any), otherwise None

argrepr?

human readable description of operation argument (if any), otherwise an empty string.

offset?

在字節(jié)碼序列中啟動操作索引

starts_line?

行由此操作碼(如果有)啟動,否則為 None

is_jump_target?

如果其他代碼跳到這里,則為 True ,否則為 False

positions?

dis.Positions object holding the start and end locations that are covered by this instruction.

3.4 新版功能.

在 3.11 版更改: Field positions is added.

class dis.Positions?

In case the information is not available, some fields might be None.

lineno?
end_lineno?
col_offset?
end_col_offset?

3.11 新版功能.

Python編譯器當(dāng)前生成以下字節(jié)碼指令。

一般指令

NOP?

Do nothing code. Used as a placeholder by the bytecode optimizer, and to generate line tracing events.

POP_TOP?

刪除堆棧頂部(TOS)項。

COPY(i)?

Push the i-th item to the top of the stack. The item is not removed from its original location.

3.11 新版功能.

SWAP(i)?

Swap TOS with the item at position i.

3.11 新版功能.

一元操作

一元操作獲取堆棧頂部元素,應(yīng)用操作,并將結(jié)果推回堆棧。

UNARY_POSITIVE?

實現(xiàn) TOS = +TOS 。

UNARY_NEGATIVE?

實現(xiàn) TOS = -TOS 。

UNARY_NOT?

實現(xiàn) TOS = not TOS 。

UNARY_INVERT?

實現(xiàn) TOS = ~TOS 。

GET_ITER?

實現(xiàn) TOS = iter(TOS) 。

GET_YIELD_FROM_ITER?

如果 TOS 是一個 generator iteratorcoroutine 對象則保持原樣。否則實現(xiàn) TOS = iter(TOS) 。

3.5 新版功能.

Binary and in-place operations

二元操作從堆棧中刪除堆棧頂部(TOS)和第二個最頂層堆棧項(TOS1)。 它們執(zhí)行操作,并將結(jié)果放回堆棧。

就地操作就像二元操作,因為它們刪除了TOS和TOS1,并將結(jié)果推回到堆棧上,但是當(dāng)TOS1支持它時,操作就地完成,并且產(chǎn)生的TOS可能是(但不一定) 原來的TOS1。

BINARY_OP(op)?

Implements the binary and in-place operators (depending on the value of op).

3.11 新版功能.

BINARY_SUBSCR?

實現(xiàn) TOS = TOS1[TOS] 。

STORE_SUBSCR?

實現(xiàn) TOS1[TOS] = TOS2

DELETE_SUBSCR?

實現(xiàn) del TOS1[TOS] 。

協(xié)程操作碼

GET_AWAITABLE(where)?

實現(xiàn) TOS = get_awaitable(TOS) ,其中 get_awaitable(o) 返回 o 如果 o 是一個有 CO_ITERABLE_COROUTINE 標(biāo)志的協(xié)程對象或生成器對象,否則解析 o.__await__ 。

If the where operand is nonzero, it indicates where the instruction occurs:

  • 1 After a call to __aenter__

  • 2 After a call to __aexit__

3.5 新版功能.

在 3.11 版更改: Previously, this instruction did not have an oparg.

GET_AITER?

實現(xiàn) TOS = TOS.__aiter__() 。

3.5 新版功能.

在 3.7 版更改: 已經(jīng)不再支持從 __aiter__ 返回可等待對象。

GET_ANEXT?

實現(xiàn) PUSH(get_awaitable(TOS.__anext__())) 。參見 GET_AWAITABLE 獲取更多 get_awaitable 的細(xì)節(jié)

3.5 新版功能.

END_ASYNC_FOR?

Terminates an async for loop. Handles an exception raised when awaiting a next item. If TOS is StopAsyncIteration pop 3 values from the stack and restore the exception state using the second of them. Otherwise re-raise the exception using the value from the stack. An exception handler block is removed from the block stack.

3.8 新版功能:

在 3.11 版更改: Exception representation on the stack now consist of one, not three, items.

BEFORE_ASYNC_WITH?

從棧頂對象解析 __aenter____aexit__ 。將 __aexit____aenter__() 的結(jié)果推入堆棧。

3.5 新版功能.

其他操作碼

PRINT_EXPR?

實現(xiàn)交互模式的表達(dá)式語句。TOS從堆棧中被移除并打印。在非交互模式下,表達(dá)式語句以 POP_TOP 終止。

SET_ADD(i)?

調(diào)用 set.add(TOS1[-i], TOS) 。 用于實現(xiàn)集合推導(dǎo)。

LIST_APPEND(i)?

調(diào)用 list.append(TOS1[-i], TOS)。 用于實現(xiàn)列表推導(dǎo)式。

MAP_ADD(i)?

調(diào)用 dict.__setitem__(TOS1[-i], TOS1, TOS) 。 用于實現(xiàn)字典推導(dǎo)。

3.1 新版功能.

在 3.8 版更改: 映射值為 TOS ,映射鍵為 TOS1 。之前,它們被顛倒了。

對于所有 SET_ADD 、 LIST_APPENDMAP_ADD 指令,當(dāng)彈出添加的值或鍵值對時,容器對象保留在堆棧上,以便它可用于循環(huán)的進(jìn)一步迭代。

RETURN_VALUE?

返回 TOS 到函數(shù)的調(diào)用者。

YIELD_VALUE?

彈出 TOS 并從一個 generator 生成它。

在 3.11 版更改: oparg set to be the stack depth, for efficient handling on frames.

YIELD_FROM?

彈出 TOS 并將其委托給它作為 generator 的子迭代器。

3.3 新版功能.

SETUP_ANNOTATIONS?

檢查 __annotations__ 是否在 locals() 中定義,如果沒有,它被設(shè)置為空 dict 。只有在類或模塊體靜態(tài)地包含 variable annotations 時才會發(fā)出此操作碼。

3.6 新版功能.

IMPORT_STAR?

將所有不以 '_' 開頭的符號直接從模塊 TOS 加載到局部命名空間。加載所有名稱后彈出該模塊。這個操作碼實現(xiàn)了 from module import * 。

POP_EXCEPT?

Pops a value from the stack, which is used to restore the exception state.

在 3.11 版更改: Exception representation on the stack now consist of one, not three, items.

RERAISE?

Re-raises the exception currently on top of the stack. If oparg is non-zero, pops an additional value from the stack which is used to set f_lasti of the current frame.

3.9 新版功能.

在 3.11 版更改: Exception representation on the stack now consist of one, not three, items.

PUSH_EXC_INFO?

Pops a value from the stack. Pushes the current exception to the top of the stack. Pushes the value originally popped back to the stack. Used in exception handlers.

3.11 新版功能.

CHECK_EXC_MATCH?

Performs exception matching for except. Tests whether the TOS1 is an exception matching TOS. Pops TOS and pushes the boolean result of the test.

3.11 新版功能.

CHECK_EG_MATCH?

Performs exception matching for except*. Applies split(TOS) on the exception group representing TOS1.

In case of a match, pops two items from the stack and pushes the non-matching subgroup (None in case of full match) followed by the matching subgroup. When there is no match, pops one item (the match type) and pushes None.

3.11 新版功能.

PREP_RERAISE_STAR?

Combines the raised and reraised exceptions list from TOS, into an exception group to propagate from a try-except* block. Uses the original exception group from TOS1 to reconstruct the structure of reraised exceptions. Pops two items from the stack and pushes the exception to reraise or None if there isn't one.

3.11 新版功能.

WITH_EXCEPT_START?

Calls the function in position 4 on the stack with arguments (type, val, tb) representing the exception at the top of the stack. Used to implement the call context_manager.__exit__(*exc_info()) when an exception has occurred in a with statement.

3.9 新版功能.

在 3.11 版更改: The __exit__ function is in position 4 of the stack rather than 7. Exception representation on the stack now consist of one, not three, items.

LOAD_ASSERTION_ERROR?

AssertionError 推入棧頂。 由 assert 語句使用。

3.9 新版功能.

LOAD_BUILD_CLASS?

Pushes builtins.__build_class__() onto the stack. It is later called to construct a class.

BEFORE_WITH(delta)?

This opcode performs several operations before a with block starts. First, it loads __exit__() from the context manager and pushes it onto the stack for later use by WITH_EXCEPT_START. Then, __enter__() is called. Finally, the result of calling the __enter__() method is pushed onto the stack.

3.11 新版功能.

GET_LEN?

len(TOS) 推入棧頂。

3.10 新版功能.

MATCH_MAPPING?

如果 TOS 是 collections.abc.Mapping 的實例(或者更準(zhǔn)確地說:如果在它的 tp_flags 中設(shè)置了 Py_TPFLAGS_MAPPING 旗標(biāo)),則將 True 推入棧頂。 否則,推入 False

3.10 新版功能.

MATCH_SEQUENCE?

如果 TOS 是 collections.abc.Sequence 的實例而 不是 str/bytes/bytearray 的實例(或者更準(zhǔn)確地說:如果在它的 tp_flags 中設(shè)置了 Py_TPFLAGS_SEQUENCE 旗標(biāo)),則將 True 推入棧頂。 否則 ,推入 False。

3.10 新版功能.

MATCH_KEYS?

TOS is a tuple of mapping keys, and TOS1 is the match subject. If TOS1 contains all of the keys in TOS, push a tuple containing the corresponding values. Otherwise, push None.

3.10 新版功能.

在 3.11 版更改: Previously, this instruction also pushed a boolean value indicating success (True) or failure (False).

STORE_NAME(namei)?

實現(xiàn) name = TOSnameiname 在代碼對象的 co_names 屬性中的索引。 在可能的情況下,編譯器會嘗試使用 STORE_FASTSTORE_GLOBAL

DELETE_NAME(namei)?

實現(xiàn) del name ,其中 namei 是代碼對象的 co_names 屬性的索引。

UNPACK_SEQUENCE(count)?

將 TOS 解包為 count 個單獨的值,它們將按從右至左的順序被放入堆棧。

UNPACK_EX(counts)?

實現(xiàn)使用帶星號的目標(biāo)進(jìn)行賦值:將 TOS 中的可迭代對象解包為單獨的值,其中值的總數(shù)可以小于可迭代對象中的項數(shù):新值之一將是由所有剩余項構(gòu)成的列表。

counts 的低字節(jié)是列表值之前的值的數(shù)量,counts 中的高字節(jié)則是之后的值的數(shù)量。 結(jié)果值會按從右至左的順序入棧。

STORE_ATTR(namei)?

實現(xiàn) TOS.name = TOS1,其中 namei 是 name 在 co_names 中的索引號。

DELETE_ATTR(namei)?

實現(xiàn) del TOS.name,使用 namei 作為 co_names 中的索引號。

STORE_GLOBAL(namei)?

類似于 STORE_NAME 但會將 name 存儲為全局變量。

DELETE_GLOBAL(namei)?

類似于 DELETE_NAME 但會刪除一個全局變量。

LOAD_CONST(consti)?

co_consts[consti] 推入棧頂。

LOAD_NAME(namei)?

將與 co_names[namei] 相關(guān)聯(lián)的值推入棧頂。

BUILD_TUPLE(count)?

創(chuàng)建一個使用了來自棧的 count 個項的元組,并將結(jié)果元組推入棧頂。

BUILD_LIST(count)?

類似于 BUILD_TUPLE 但會創(chuàng)建一個列表。

BUILD_SET(count)?

類似于 BUILD_TUPLE 但會創(chuàng)建一個集合。

BUILD_MAP(count)?

將一個新字典對象推入棧頂。 彈出 2 * count 項使得字典包含 count 個條目: {..., TOS3: TOS2, TOS1: TOS}。

在 3.5 版更改: 字典是根據(jù)棧中的項創(chuàng)建而不是創(chuàng)建一個預(yù)設(shè)大小包含 count 項的空字典。

BUILD_CONST_KEY_MAP(count)?

BUILD_MAP 版本專用于常量鍵。 彈出的棧頂元素包含一個由鍵構(gòu)成的元組,然后從 TOS1 開始從構(gòu)建字典的值中彈出 count 個值。

3.6 新版功能.

BUILD_STRING(count)?

拼接 count 個來自棧的字符串并將結(jié)果字符串推入棧頂。

3.6 新版功能.

LIST_TO_TUPLE?

從堆棧中彈出一個列表并推入一個包含相同值的元組。

3.9 新版功能.

LIST_EXTEND(i)?

調(diào)用 list.extend(TOS1[-i], TOS)。 用于構(gòu)建列表。

3.9 新版功能.

SET_UPDATE(i)?

調(diào)用 set.update(TOS1[-i], TOS)。 用于構(gòu)建集合。

3.9 新版功能.

DICT_UPDATE(i)?

調(diào)用 dict.update(TOS1[-i], TOS)。 用于構(gòu)建字典。

3.9 新版功能.

DICT_MERGE(i)?

類似于 DICT_UPDATE 但對于重復(fù)的鍵會引發(fā)異常。

3.9 新版功能.

LOAD_ATTR(namei)?

將 TOS 替換為 getattr(TOS, co_names[namei])。

COMPARE_OP(opname)?

執(zhí)行布爾運算操作。 操作名稱可在 cmp_op[opname] 中找到。

IS_OP(invert)?

執(zhí)行 is 比較,或者如果 invert 為 1 則執(zhí)行 is not。

3.9 新版功能.

CONTAINS_OP(invert)?

執(zhí)行 in 比較,或者如果 invert 為 1 則執(zhí)行 not in

3.9 新版功能.

IMPORT_NAME(namei)?

導(dǎo)入模塊 co_names[namei]。 會彈出 TOS 和 TOS1 以提供 fromlistlevel 參數(shù)給 __import__()。 模塊對象會被推入棧頂。 當(dāng)前命名空間不受影響:對于一條標(biāo)準(zhǔn) import 語句,會執(zhí)行后續(xù)的 STORE_FAST 指令來修改命名空間。

IMPORT_FROM(namei)?

從在 TOS 內(nèi)找到的模塊中加載屬性 co_names[namei]。 結(jié)果對象會被推入棧頂,以便由后續(xù)的 STORE_FAST 指令來保存。

JUMP_FORWARD(delta)?

將字節(jié)碼計數(shù)器的值增加 delta

JUMP_BACKWARD(delta)?

Decrements bytecode counter by delta. Checks for interrupts.

3.11 新版功能.

JUMP_BACKWARD_NO_INTERRUPT(delta)?

Decrements bytecode counter by delta. Does not check for interrupts.

3.11 新版功能.

POP_JUMP_FORWARD_IF_TRUE(delta)?

If TOS is true, increments the bytecode counter by delta. TOS is popped.

3.11 新版功能.

POP_JUMP_BACKWARD_IF_TRUE(delta)?

If TOS is true, decrements the bytecode counter by delta. TOS is popped.

3.11 新版功能.

POP_JUMP_FORWARD_IF_FALSE(delta)?

If TOS is false, increments the bytecode counter by delta. TOS is popped.

3.11 新版功能.

POP_JUMP_BACKWARD_IF_FALSE(delta)?

If TOS is false, decrements the bytecode counter by delta. TOS is popped.

3.11 新版功能.

POP_JUMP_FORWARD_IF_NOT_NONE(delta)?

If TOS is not None, increments the bytecode counter by delta. TOS is popped.

3.11 新版功能.

POP_JUMP_BACKWARD_IF_NOT_NONE(delta)?

If TOS is not None, decrements the bytecode counter by delta. TOS is popped.

3.11 新版功能.

POP_JUMP_FORWARD_IF_NONE(delta)?

If TOS is None, increments the bytecode counter by delta. TOS is popped.

3.11 新版功能.

POP_JUMP_BACKWARD_IF_NONE(delta)?

If TOS is None, decrements the bytecode counter by delta. TOS is popped.

3.11 新版功能.

JUMP_IF_TRUE_OR_POP(delta)?

If TOS is true, increments the bytecode counter by delta and leaves TOS on the stack. Otherwise (TOS is false), TOS is popped.

3.1 新版功能.

在 3.11 版更改: The oparg is now a relative delta rather than an absolute target.

JUMP_IF_FALSE_OR_POP(delta)?

If TOS is false, increments the bytecode counter by delta and leaves TOS on the stack. Otherwise (TOS is true), TOS is popped.

3.1 新版功能.

在 3.11 版更改: The oparg is now a relative delta rather than an absolute target.

FOR_ITER(delta)?

TOS 是一個 iterator。 請調(diào)用其 __next__() 方法。 如果此操作產(chǎn)生了一個新值,則將其推入棧頂(將迭代器留在其下方)。 如果迭代器提示已耗盡,TOS 會被彈出,并且字節(jié)碼計數(shù)器將增加 delta。

LOAD_GLOBAL(namei)?

Loads the global named co_names[namei>>1] onto the stack.

在 3.11 版更改: If the low bit of namei is set, then a NULL is pushed to the stack before the global variable.

LOAD_FAST(var_num)?

將指向局部對象 co_varnames[var_num] 的引用推入棧頂。

STORE_FAST(var_num)?

將 TOS 存放到局部對象 co_varnames[var_num]。

DELETE_FAST(var_num)?

移除局部對象 co_varnames[var_num]

MAKE_CELL(i)?

Creates a new cell in slot i. If that slot is empty then that value is stored into the new cell.

3.11 新版功能.

LOAD_CLOSURE(i)?

Pushes a reference to the cell contained in slot i of the "fast locals" storage. The name of the variable is co_fastlocalnames[i].

Note that LOAD_CLOSURE is effectively an alias for LOAD_FAST. It exists to keep bytecode a little more readable.

在 3.11 版更改: i is no longer offset by the length of co_varnames.

LOAD_DEREF(i)?

Loads the cell contained in slot i of the "fast locals" storage. Pushes a reference to the object the cell contains on the stack.

在 3.11 版更改: i is no longer offset by the length of co_varnames.

LOAD_CLASSDEREF(i)?

類似于 LOAD_DEREF 但在查詢單元之前會首先檢查局部對象字典。 這被用于加載類語句體中的自由變量。

3.4 新版功能.

在 3.11 版更改: i is no longer offset by the length of co_varnames.

STORE_DEREF(i)?

Stores TOS into the cell contained in slot i of the "fast locals" storage.

在 3.11 版更改: i is no longer offset by the length of co_varnames.

DELETE_DEREF(i)?

Empties the cell contained in slot i of the "fast locals" storage. Used by the del statement.

3.2 新版功能.

在 3.11 版更改: i is no longer offset by the length of co_varnames.

COPY_FREE_VARS(n)?

Copies the n free variables from the closure into the frame. Removes the need for special code on the caller's side when calling closures.

3.11 新版功能.

RAISE_VARARGS(argc)?

使用 raise 語句的 3 種形式之一引發(fā)異常,具體形式取決于 argc 的值:

  • 0: raise (重新引發(fā)之前的異常)

  • 1: raise TOS (在 TOS 上引發(fā)異常實例或類型)

  • 2: raise TOS1 from TOS (在 TOS1 上引發(fā)異常實例或類型并將 __cause__ 設(shè)為 TOS)

CALL(argc)?

Calls a callable object with the number of arguments specified by argc, including the named arguments specified by the preceding KW_NAMES, if any. On the stack are (in ascending order), either:

  • NULL

  • The callable

  • The positional arguments

  • The named arguments

or:

  • The callable

  • self

  • The remaining positional arguments

  • The named arguments

argc is the total of the positional and named arguments, excluding self when a NULL is not present.

CALL 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.

3.11 新版功能.

CALL_FUNCTION_EX(flags)?

調(diào)用一個可調(diào)用對象并附帶位置參數(shù)和關(guān)鍵字參數(shù)變量集合。 如果設(shè)置了 flags 的最低位,則棧頂包含一個由額外關(guān)鍵字參數(shù)組成的映射對象。 在調(diào)用該可調(diào)用對象之前,映射對象和可迭代對象會被分別“解包”并將它們的內(nèi)容分別作為關(guān)鍵字參數(shù)和位置參數(shù)傳入。 CALL_FUNCTION_EX 會中棧中彈出所有參數(shù)及可調(diào)用對象,附帶這些參數(shù)調(diào)用該可調(diào)用對象,并將可調(diào)用對象所返回的返回值推入棧頂。

3.6 新版功能.

LOAD_METHOD(namei)?

Loads a method named co_names[namei] 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 (self) by CALL when calling the unbound method. Otherwise, NULL and the object return by the attribute lookup are pushed.

3.7 新版功能.

PUSH_NULL?

Pushes a NULL to the stack. Used in the call sequence to match the NULL pushed by LOAD_METHOD for non-method calls.

3.11 新版功能.

KW_NAMES(i)?

Prefixes CALL. Stores a reference to co_consts[consti] into an internal variable for use by CALL. co_consts[consti] must be a tuple of strings.

3.11 新版功能.

MAKE_FUNCTION(flags)?

將一個新函數(shù)對象推入棧頂。 從底端到頂端,如果參數(shù)帶有指定的旗標(biāo)值則所使用的棧必須由這些值組成。

  • 0x01 一個默認(rèn)值的元組,用于按位置排序的僅限位置形參以及位置或關(guān)鍵字形參

  • 0x02 一個僅限關(guān)鍵字形參的默認(rèn)值的字典

  • 0x04 一個包含形參標(biāo)注的字符串元組。

  • 0x08 一個包含用于自由變量的單元的元組,生成一個閉包

  • 與函數(shù)相關(guān)聯(lián)的代碼 (在 TOS1)

  • 函數(shù)的 qualified name (在 TOS)

在 3.10 版更改: 旗標(biāo)值 0x04 是一個字符串元組而非字典。

BUILD_SLICE(argc)?

將一個切片對象推入棧頂。 argc 必須為 2 或 3。 如果為 2,則推入 slice(TOS1, TOS);如果為 3,則推入 slice(TOS2, TOS1, TOS)。 請參閱 slice() 內(nèi)置函數(shù)了解詳細(xì)信息。

EXTENDED_ARG(ext)?

為任意帶有大到無法放入默認(rèn)的單字節(jié)的參數(shù)的操作碼添加前綴。 ext 存放一個附加字節(jié)作為參數(shù)中的高比特位。 對于每個操作碼,最多允許三個 EXTENDED_ARG 前綴,構(gòu)成兩字節(jié)到三字節(jié)的參數(shù)。

FORMAT_VALUE(flags)?

用于實現(xiàn)格式化字面值字符串(f-字符串)。 從棧中彈出一個可選的 fmt_spec,然后是一個必須的 value。 flags 的解讀方式如下:

  • (flags & 0x03) == 0x00: value 按原樣格式化。

  • (flags & 0x03) == 0x01: 在格式化 value 之前調(diào)用其 str()

  • (flags & 0x03) == 0x02: 在格式化 value 之前調(diào)用其 repr()。

  • (flags & 0x03) == 0x03: 在格式化 value 之前調(diào)用其 ascii()

  • (flags & 0x04) == 0x04: 從棧中彈出 fmt_spec 并使用它,否則使用空的 fmt_spec

使用 PyObject_Format() 執(zhí)行格式化。 結(jié)果會被推入棧頂。

3.6 新版功能.

MATCH_CLASS(count)?

TOS 是一個包含關(guān)鍵字屬性名稱的元組,TOS1 是要匹配的類,而 TOS2 是匹配目標(biāo)。 count 是位置子模式的數(shù)量。

Pop TOS, TOS1, and TOS2. If TOS2 is an instance of TOS1 and has the positional and keyword attributes required by count and TOS, push a tuple of extracted attributes. Otherwise, push None.

3.10 新版功能.

在 3.11 版更改: Previously, this instruction also pushed a boolean value indicating success (True) or failure (False).

RESUME(where)?

A no-op. Performs internal tracing, debugging and optimization checks.

The where operand marks where the RESUME occurs:

  • 0 The start of a function

  • 1 After a yield expression

  • 2 After a yield from expression

  • 3 After an await expression

3.11 新版功能.

RETURN_GENERATOR?

Create a generator, coroutine, or async generator from the current frame. Clear the current frame and return the newly created generator.

3.11 新版功能.

SEND?

Sends None to the sub-generator of this generator. Used in yield from and await statements.

3.11 新版功能.

ASYNC_GEN_WRAP?

Wraps the value on top of the stack in an async_generator_wrapped_value. Used to yield in async generators.

3.11 新版功能.

HAVE_ARGUMENT?

這不是一個真正的操作碼。 它用于標(biāo)明使用參數(shù)和不使用參數(shù)的操作碼 (分別為 < HAVE_ARGUMENT>= HAVE_ARGUMENT) 之間的分隔線。

在 3.6 版更改: 現(xiàn)在每條指令都帶有參數(shù),但操作碼 < HAVE_ARGUMENT 會忽略它。 之前僅限操作碼 >= HAVE_ARGUMENT 帶有參數(shù)。

操作碼集合?

提供這些集合用于字節(jié)碼指令的自動內(nèi)?。?/p>

dis.opname?

操作名稱的序列,可使用字節(jié)碼來索引。

dis.opmap?

映射操作名稱到字節(jié)碼的字典

dis.cmp_op?

所有比較操作名稱的序列。

dis.hasconst?

訪問常量的字節(jié)碼序列。

dis.hasfree?

訪問自由變量的字節(jié)碼序列(請注意這里所說的‘自由’是指在當(dāng)前作用域中被內(nèi)部作用域所引用的名稱,或在外部作用域中被此作用域所引用的名稱。 它 并不 包括對全局或內(nèi)置作用域的引用)。

dis.hasname?

按名稱訪問屬性的字節(jié)碼序列。

dis.hasjrel?

具有相對跳轉(zhuǎn)目標(biāo)的字節(jié)碼序列。

dis.hasjabs?

具有絕對跳轉(zhuǎn)目標(biāo)的字節(jié)碼序列。

dis.haslocal?

訪問局部變量的字節(jié)碼序列。

dis.hascompare?

布爾運算的字節(jié)碼序列。