一图看懂 dis 模块:将 python 字节码反汇编为助记符,资料整理+笔记(大全)

本文介绍了Python的dis模块,该模块用于将Python字节码反汇编为人类可读的助记符,便于代码分析。文章列出了模块的函数、类以及相关统计信息,包括常量、函数和类的方法,如disassemble()和Instruction类等,是理解Python内部机制的宝贵资源。
摘要由CSDN通过智能技术生成

本文由 大侠(AhcaoZhu)原创,转载请声明。
链接: https://blog.csdn.net/Ahcao2008

Alt

🧊摘要

  • 全文介绍python的 dis 模块(将 python 字节码反汇编为助记符)、函数、类及类的方法和属性。
  • 它通过代码抓取并经AI智能翻译和人工校对。
  • 是一部不可多得的权威字典类工具书。它是系列集的一部分。后续陆续发布、敬请关注。【原创:AhcaoZhu大侠】

🧊模块图

dis-module

dis
	◆sys
	◆types
	◆collections
	◆io

🧊类关系图

dis-class

◆object
	◆collections.deque
	dis.Bytecode
	◆frame
	◆mappingproxy
	◆member_descriptor
	◆operator.itemgetter
	◆tuple
		dis._Instruction
			dis.Instruction

🧊模块全展开

☘️【dis】

dis, fullname=dis, file=dis.py

将Python字节码反汇编为助记符。

🔵统计

序号类别数量
1int5
4str5
5tuple2
6list10
8dict3
9module4
10class3
11function21
12builtin_function_or_method1
13residual2
14system9
15private15
16all56

🔵常量

🌿int

1 HAVE_ARGUMENT 90
2 EXTENDED_ARG 144
3 FORMAT_VALUE 155
4 _OPNAME_WIDTH 20
5 _OPARG_WIDTH 5

🌿tuple

6 cmp_op (‘<’, ‘<=’, ‘==’, ‘!=’, ‘>’, ‘>=’, ‘in’, ‘not in’, ‘is’, ‘is not’, ‘exception match’, ‘BAD’)
7 _have_code (<class ‘method’>, <class ‘function’>, <class ‘code’>, <class ‘classmethod’>, <class ‘staticmethod’>, <class ‘type’>)

🌿list

8 hasconst [100]
9 hasname [90, 91, 95, 96, 97, 98, 101, 106, 108, 109, 116, 160]
10 hasjrel [93, 110, 120, 121, 122, 143, 154]
11 hasjabs [111, 112, 113, 114, 115, 119]
12 haslocal [124, 125, 126]
13 hascompare [107]
14 hasfree [135, 136, 137, 138, 148]
15 opname [‘<0>’, ‘POP_TOP’, ‘ROT_TWO’, ‘ROT_THREE’, ‘DUP_TOP’, ‘DUP_TOP_TWO’, ‘<6>’, ‘<7>’, ‘<8>’, ‘NOP’, ‘UNARY_POSITIVE’, 'UNAR…
16 hasnargs []

🌿dict

17 opmap {‘POP_TOP’: 1, ‘ROT_TWO’: 2, ‘ROT_THREE’: 3, ‘DUP_TOP’: 4, ‘DUP_TOP_TWO’: 5, ‘NOP’: 9, ‘UNARY_POSITIVE’: 10, 'UNARY_NEGA…
18 COMPILER_FLAG_NAMES {1: ‘OPTIMIZED’, 2: ‘NEWLOCALS’, 4: ‘VARARGS’, 8: ‘VARKEYWORDS’, 16: ‘NESTED’, 32: ‘GENERATOR’, 64: ‘NOFREE’, 128: 'CORO…

🔵模块

🌿19 sys

sys, fullname=sys, file=

🌿20 types

types, fullname=types, file=types.py

🌿21 collections

collections, fullname=collections, file=collections_init_.py

🌿22 io

io, fullname=io, file=io.py

🔵函数

🌿23 _try_compile(source, name)

_try_compile(source, name), module=dis, line:21 at dis.py

尝试编译给定的源代码,首先作为表达式编译,如果第一种方法失败,然后作为语句编译。
实用程序函数接受函数中的字符串,否则会期望代码对象

🌿24 dis(x=None, *, file=None, depth=None)

dis(x=None, *, file=None, depth=None), module=dis, line:34 at dis.py

反汇编类、方法、函数和其他编译过的对象。
在没有参数的情况下,反汇编最后一个回溯。
编译对象目前包括生成器对象、异步生成器对象和协程对象,所有这些对象都将其代码对象存储在一个特殊属性中。

🌿25 distb(tb=None, *, file=None)

distb(tb=None, *, file=None), module=dis, line:79 at dis.py

反汇编回溯(默认:最后一次回溯)。

🌿26 pretty_flags(flags)

pretty_flags(flags), module=dis, line:105 at dis.py

返回代码标志的漂亮表示。

🌿27 _get_code_object(x)

_get_code_object(x), module=dis, line:119 at dis.py

帮助处理方法、编译或原始代码对象和字符串。

🌿28 code_info(x)

code_info(x), module=dis, line:142 at dis.py

方法、函数或代码的格式化细节。

🌿29 _format_code_info(co)

_format_code_info(co), module=dis, line:146 at dis.py

🌿30 show_code(co, *, file=None)

show_code(co, *, file=None), module=dis, line:177 at dis.py

打印方法、函数或代码的详细信息到 *file*。如果没有提供 *file*,则输出在 stdout 上打印。

🌿31 get_instructions(x, *, first_line=None)

get_instructions(x, *, first_line=None), module=dis, line:251 at dis.py

方法、函数或代码中操作码的迭代器生成一系列指令命名元组,给出所提供代码中每个操作的详细信息。
如果 *first_line* 不是None,它表示反汇编代码中应该报告的第一个源行的行号。
否则,源行信息(如果有的话)将直接从反汇编代码对象中获取。

🌿32 _get_const_info(const_index, const_list)

_get_const_info(const_index, const_list), module=dis, line:273 at dis.py

如果定义了常量列表,则返回解引用常量及其报告。
否则返回常量索引及其repr()。

🌿33 _get_name_info(name_index, name_list)

_get_name_info(name_index, name_list), module=dis, line:285 at dis.py

如果定义了名称列表,则返回解引用的名称作为value和repr。
否则返回名称索引及其repr()。

🌿34 _get_instructions_bytes(code, varnames=None, names=None, constants=None, cells=None, linestarts=None, line_offset=0)

_get_instructions_bytes(code, varnames=None, names=None, constants=None, cells=None, linestarts=None, line_offset=0), module=dis, line:301 at dis.py

迭代字节码字符串中的指令。
生成指令命名元组序列,给出每个操作码的详细信息。
关于代码运行时环境的附加信息(例如变量名、常量)可以使用可选参数指定。

🌿35 disassemble(co, lasti=-1, *, file=None)

disassemble(co, lasti=-1, *, file=None), module=dis, line:352 at dis.py

反汇编代码对象。

🌿36 _disassemble_recursive(co, *, file=None, depth=None)

_disassemble_recursive(co, *, file=None, depth=None), module=dis, line:359 at dis.py

🌿37 _disassemble_bytes(code, lasti=-1, varnames=None, names=None, constants=None, cells=None, linestarts=None, *, file=None, line_offset=0)

_disassemble_bytes(code, lasti=-1, varnames=None, names=None, constants=None, cells=None, linestarts=None, *, file=None, line_offset=0), module=dis, line:370 at dis.py

🌿38 _disassemble_str(source, **kwargs)

_disassemble_str(source, **kwargs), module=dis, line:400 at dis.py

编译源字符串,然后反汇编代码对象。

🌿39 disassemble(co, lasti=-1, *, file=None)

disassemble(co, lasti=-1, *, file=None), module=dis, line:352 at dis.py

反汇编代码对象。

🌿40 _unpack_opargs(code)

_unpack_opargs(code), module=dis, line:406 at dis.py

🌿41 findlabels(code)

findlabels(code), module=dis, line:417 at dis.py

检测字节码中作为跳转目标的所有偏移量。
返回偏移量列表。

🌿42 findlinestarts(code)

findlinestarts(code), module=dis, line:436 at dis.py

查找字节码中的偏移量,这些偏移量是源代码中的行开始。
生成对(offset, lineno),如 Python/compile.c 中所述。

🌿43 _test()

_test(), module=dis, line:522 at dis.py

简单的测试程序反汇编文件。

🔵类

🌿44 dis._Instruction

_Instruction, dis._Instruction, module=dis, line:-1 at dis.py

    _Instruction(opname, opcode, arg, argval, argrepr, offset, starts_line, is_jump_target)
property

1 arg=<property object at 0x000002B5F57FAB38> kind:property type:property class:<class ‘dis._Instruction’>
2 argrepr=<property object at 0x000002B5F57FABD8> kind:property type:property class:<class ‘dis._Instruction’>
3 argval=<property object at 0x000002B5F57FAB88> kind:property type:property class:<class ‘dis._Instruction’>
4 is_jump_target=<property object at 0x000002B5F57FACC8> kind:property type:property class:<class ‘dis._Instruction’>
5 offset=<property object at 0x000002B5F57FAC28> kind:property type:property class:<class ‘dis._Instruction’>
6 opcode=<property object at 0x000002B5F57FAAE8> kind:property type:property class:<class ‘dis._Instruction’>
7 opname=<property object at 0x000002B5F57FAA98> kind:property type:property class:<class ‘dis._Instruction’>
8 starts_line=<property object at 0x000002B5F57FAC78> kind:property type:property class:<class ‘dis._Instruction’>

🌿45 dis.Instruction

Instruction, dis.Instruction, module=dis, line:199 at dis.py

字节码操作的详细信息
    opname          -操作的人类可读名称
    opcode          -操作的数字代码
    arg             -操作的数字参数(如果有),否则为None
    argval          -解析的参数值(如果已知),否则与arg相同
    argrepr         -操作参数的人类可读描述
    offset          -字节码序列中操作的起始索引
    starts_line     -由该操作码开始的行(如果有),否则为None
    is_jump_target  -如果其他代码跳转到这里,否则为False

🌿46 dis.Bytecode

Bytecode, dis.Bytecode, module=dis, line:461 at dis.py

用函数、方法、其他编译对象、代码字符串或代码对象(由compile()返回)实例化它。
对其进行迭代将产生字节码操作作为指令实例。
method
1 dis(self)

kind=method class=Bytecode objtype=function line:504 at …\lib\dis.py

返回字节码操作的格式化视图。
2 info(self)

kind=method class=Bytecode objtype=function line:500 at …\lib\dis.py

返回关于代码对象的格式化信息。
class method
3 from_traceback(cls, tb)

kind=class method class=Bytecode objtype=classmethod line:494 at …\lib\dis.py

将函数转换为类方法。

🔵内嵌函数或方法

47 stack_effect

🔵私有或局部

48 _have_code (<class ‘method’>, <class ‘function’>, <class ‘code’>, <class ‘classmethod’>, <class ‘staticmethod’>, <class ‘type’>)
49 _try_compile <function _try_compile at 0x000002B5F348B288>
50 _get_code_object <function _get_code_object at 0x000002B5F57F2D38>
51 _format_code_info <function _format_code_info at 0x000002B5F57F8048>
52 _Instruction <class ‘dis._Instruction’>
53 _OPNAME_WIDTH 20
54 _OPARG_WIDTH 5
55 _get_const_info <function _get_const_info at 0x000002B5F57F8798>
56 _get_name_info <function _get_name_info at 0x000002B5F57F8828>
57 _get_instructions_bytes <function _get_instructions_bytes at 0x000002B5F57F88B8>
58 _disassemble_recursive <function _disassemble_recursive at 0x000002B5F57F89D8>
59 _disassemble_bytes <function _disassemble_bytes at 0x000002B5F57F8A68>
60 _disassemble_str <function _disassemble_str at 0x000002B5F57F8AF8>
61 _unpack_opargs <function _unpack_opargs at 0x000002B5F57F8B88>
62 _test <function _test at 0x000002B5F57F8D38>

☘️【sys】

sys, fullname=sys

☘️【types】

types, fullname=types, file=types.py

☘️【collections】

collections, fullname=collections, file=collections_init_.py

☘️【io】

io, fullname=io, file=io.py

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AhcaoZhu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值