5
5
----------
6
6
问题
7
7
----------
8
- You want to know in detail what your code is doing under the covers by disassembling
9
- it into lower-level byte code used by the interpreter.
8
+ 你想通过将你的代码反编译成低级的字节码来查看它底层的工作机制。
10
9
11
10
|
12
11
13
12
----------
14
13
解决方案
15
14
----------
16
- The dis module can be used to output a disassembly of any Python function. For
17
- example:
15
+ ``dis `` 模块可以被用来输出任何Python函数的反编译结果。例如:
18
16
19
17
.. code-block :: python
20
18
@@ -34,9 +32,8 @@ example:
34
32
----------
35
33
讨论
36
34
----------
37
- The dis module can be useful if you ever need to study what’s happening in your program
38
- at a very low level (e.g., if you’re trying to understand performance characteristics).
39
- The raw byte code interpreted by the dis() function is available on functions as follows:
35
+ 当你想要知道你的程序底层的运行机制的时候,``dis `` 模块是很有用的。比如如果你想试着理解性能特征。
36
+ 被 ``dis() `` 函数解析的原始字节码如下所示:
40
37
41
38
.. code-block :: python
42
39
@@ -46,8 +43,7 @@ The raw byte code interpreted by the dis() function is available on functions as
46
43
\x01\x00\x01d\x00\x00S"
47
44
>> >
48
45
49
- If you ever want to interpret this code yourself, you would need to use some of the
50
- constants defined in the opcode module. For example:
46
+ 如果你想自己解释这段代码,你需要使用一些在 ``opcode `` 模块中定义的常量。例如:
51
47
52
48
.. code-block :: python
53
49
@@ -60,9 +56,8 @@ constants defined in the opcode module. For example:
60
56
' LOAD_FAST'
61
57
>> >
62
58
63
- Ironically, there is no function in the dis module that makes it easy for you to process
64
- the byte code in a programmatic way. However, this generator function will take the raw
65
- byte code sequence and turn it into opcodes and arguments.
59
+ 奇怪的是,在 ``dis `` 模块中并没有函数让你以编程方式很容易的来处理字节码。
60
+ 不过,下面的生成器函数可以将原始字节码序列转换成 ``opcodes `` 和参数。
66
61
67
62
.. code-block :: python
68
63
@@ -86,15 +81,15 @@ byte code sequence and turn it into opcodes and arguments.
86
81
oparg = None
87
82
yield (op, oparg)
88
83
89
- To use this function, you would use code like this:
84
+ 使用方法如下:
90
85
91
86
.. code-block :: python
92
87
93
88
>> > for op, oparg in generate_opcodes(countdown.__code__ .co_code):
94
89
... print (op, opcode.opname[op], oparg)
95
90
96
- It’s a little-known fact, but you can replace the raw byte code of any function that you
97
- want. It takes a bit of work to do it, but here’s an example of what’s involved:
91
+ 这种方式很少有人知道,你可以利用它替换任何你想要替换的函数的原始字节码。
92
+ 下面我们用一个示例来演示整个过程:
98
93
99
94
.. code-block :: python
100
95
@@ -120,10 +115,6 @@ want. It takes a bit of work to do it, but here’s an example of what’s invol
120
115
>> > add(2 ,3 )
121
116
Segmentation fault
122
117
123
- Having the interpreter crash is a pretty likely outcome of pulling a crazy stunt like this.
124
- However, developers working on advanced optimization and metaprogramming tools
125
- might be inclined to rewrite byte code for real. This last part illustrates how to do it. See
118
+ 你可以像这样耍大招让解释器奔溃。但是,对于编写更高级优化和元编程工具的程序员来讲,
119
+ 他们可能真的需要重写字节码。本节最后的部分演示了这个是怎样做到的。你还可以参考另外一个类似的例子:
126
120
`this code on ActiveState <http://code.activestate.com/recipes/277940-decorator-for-bindingconstants-at-compile-time/ >`_
127
- for another example of such code in action.
128
-
129
-
0 commit comments