Cobra -- Super dis

Source: Internet
Author: User

In the standard Python library, many libraries are used for python compilation and decompilation (ER, to be exact, disassembly ). These libraries are listed in the "Python language services" section of "Python Library Reference" in the python documentation. Dis is one of them. The official description is "discycler for python byte code"

In layman's terms, DIS is the sequence of bytecode commands in the compiled results of Python.Anti-Assembler. The running mechanism of python is essentially the same as that of Java and C #. It is a stack-based virtual machine that has its own defined bytecode commands. Now that we have bytecode instructions, we can use these bytecode commands to write them into learning just like an assembly language. In C #, we can directly use msil to write. net programs. Java also has open-source compilers such as Jasmin. Python does not provide such a assembler, but this does not prevent python from providing an anti-assembler such as DIS in the standard library, after all, I am very interested in bytecode commands that drive the underlying operations of Python.

But unfortunately, DIS is not a very convenient library. In DIS, many methods are provided, which are similar to each other. The most commonly used method is dis (called through DIS. Dis). The official description is as follows:

For a module, it disassembles all functions.

For a class, it disassembles all methods.

For a single code sequence, it prints one line per byte code instruction.

It seems very powerful. modules, classes, and functions can be decompiled. However, in practice, you will find it inconvenient to use. Consider the following example:

Def F ():
Pass

Def g ():
Print 'Hello world'
Def func ():
Print 'this is a nested function'

Class myclass (object ):
Def _ init _ (Self ):
Pass

A = 1
A + = 1
Print

If we want to view the module-level bytecode command, it seems that we can do the following:

Import demo
Import dis
Dis. Dis (DEMO)

Unfortunately, this is not the case. The disassembly result is to disassemble all the methods in the Demo module. The result is as follows:

Disassembly of F:
2 0 load_const 0 (none)
3 return_value

Disassembly of G:
5 0 load_const 1 ('Hello World ')
3 print_item
4 print_newline

6 5 load_const 2 (<code object fun at 00bcabf0, file "demo. py", line 6>)
8 make_function 0
11 store_fast 0 (fun)
14 load_const 0 (none)
17 return_value

However, we want to view the bytecode instructions for expressions such as "def F ()" and "A + = 1". Dis cannot easily see the expected target. Of course, we can still see the bytecode command of "A + = 1" using DIS, by first configuring the demo. py is read in the file mode, and the read string is compiled into a code object using builtin's compile, and then the code object is decompiled using dis. You can see that this will be very troublesome.

On the other hand, if you want to view the bytecode commands of myclass. _ init _ and the embedded function fun, the process will be very complicated. During my "Python source code analysis" process, I need a convenient tool to conveniently and quickly see the bytecode commands corresponding to any function, class, and module. This tool is my own SDIs (Super dis ). In SDIs, the following functions are used:

 

Import DIS as pydis
Import types

Code = none
Def read (filename ):
F = open (filename)
Content = f. Read ()
Global Code
Code = compile (content, filename, 'exec ')
F. Close ()

Def find_code (Code, name ):
For item in code. co_consts:
If isinstance (item, types. codetype ):
If item. co_name = Name:
Return item
Return none

Def DIS (code_name = none ):
If code_name is none:
CO = Code
Pydis. Dis (CO)
Return
Names = code_name.split (".")
CO = Code
For name in names:
CO = find_code (CO, name)
If not CO:
Print '% s is not a valid name' % code_name
If CO:
Print ("Byte Code for % s" % code_name). Center (60 ,'*')
Pydis. Dis (CO)

The principle is very simple. SDIs reads and compiles Python source files. We have learned from the Python source code analysis series that the compilation result is a code object. In the code object, contains a co_consts with other code objects. The nested structure of classes, functions, and modules in the demo. py layer. The Code object also presents the same nested structure in co_consts. With this tool, it is much easier to disassemble python. The following are several examples:

Python 2.5 (r25: 51908, SEP 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on Win32
Type "help", "Copyright", "Credits" or "License" for more information.
>>> Import SDIs
>>> Import demo
2
>>> SDIs. Read ('demo. py ')
>>> SDIs. Dis () # This is a module-level disassembly result.
1 0 load_const 0 (<code object f at 00be7530, file "demo. py", line 1>)
3 make_function 0
6 store_name 0 (f)

4 9 load_const 1 (<code object g at 00be7650, file "demo. py", line 4>)
12 make_function 0
15 store_name 1 (g)

9 18 load_const 2 ('myclass ')
21 load_name 2 (object)
24 build_tuple 1
27 load_const 3 (<code object myclass at 00be76e0, fi
Le "demo. py", line 9>)
30 make_function 0
33 call_function 0
36 build_class
37 store_name 3 (myclass)

13 40 load_const 4 (1)
43 store_name 4 ()

14 46 load_name 4 ()
49 load_const 4 (1)
52 inplace_add
53 store_name 4 ()

15 56 load_name 4 ()
59 print_item
60 print_newline
61 load_const 5 (none)
64 return_value
>>>
>>> SDIs. Dis ('myclass. _ init __')
* *********** Byte code for myclass. _ init __*************
11 0 load_const 0 (none)
3 return_value
>>>
>>> SDIs. Dis ('G. fun ')
* ***************** Byte code for G. fun *******************
7 0 load_const 0 (none)
3 return_value
>>>

Cobra will use SDIs to decompile and display Python source files.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.