32.13. pickletools
- 腌菜开发者的工具¶
源代码: Lib / pickletools.py
此模块包含与pickle
模块的详细细节相关的各种常量,有关实现的一些冗长注释,以及一些用于分析pickled数据的有用函数。该模块的内容对于正在处理pickle
的Python核心开发人员很有用;普通用户的pickle
模块可能找不到pickletools
模块相关。
32.13.1. Command line usage¶
版本3.2中的新功能。
从命令行调用时,python -m pickletools
将反汇编一个或多个pickle文件的内容。注意,如果你想看到存储在pickle中的Python对象,而不是pickle格式的细节,你可以使用-m pickle t0 >改为。
但是,当要检查的pickle文件来自不受信任的来源时,-m pickletools
是一个更安全的选项, pickle字节码。
For example, with a tuple (1, 2)
pickled in file x.pickle
:
$ python -m pickle x.pickle
(1, 2)
$ python -m pickletools x.pickle
0: \x80 PROTO 3
2: K BININT1 1
4: K BININT1 2
6: \x86 TUPLE2
7: q BINPUT 0
9: . STOP
highest protocol among opcodes = 2
32.13.2. Programmatic Interface¶
-
pickletools.
dis
(pickle, out=None, memo=None, indentlevel=4, annotate=0)¶ - Outputs a symbolic disassembly of the pickle to the file-like object out, defaulting to
sys.stdout
. pickle can be a string or a file-like object. memo can be a Python dictionary that will be used as the pickle’s memo; it can be used to perform disassemblies across multiple pickles created by the same pickler. Successive levels, indicated byMARK
opcodes in the stream, are indented by indentlevel spaces. If a nonzero value is given to annotate, each opcode in the output is annotated with a short description. The value of annotate is used as a hint for the column where annotation should start.版本3.2中的新功能: 注释参数。
-
pickletools.
genops
(pickle)¶ 在pickle中的所有操作码上提供iterator,返回
(操作码, arg, pos)
三元组。opcode是OpcodeInfo
类的实例; arg是操作码参数的解码值,作为Python对象; pos是此操作码所在的位置。pickle可以是字符串或类似文件的对象。
-
pickletools.
optimize
(picklestring)¶ 在删除未使用的
PUT
操作码后,返回一个新的等效pickle字符串。优化的泡菜更短,需要更少的传输时间,需要更少的存储空间,更有效地解开。