Skip to content

Commit a5fc60d

Browse files
committedMay 14, 2019
compile and uncompile
1 parent 50d3afa commit a5fc60d

11 files changed

+121
-2
lines changed
 

Diff for: ‎README.md

+4
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ python的强大之处有很大的一方面在于它有各种各样非常强大
269269

270270
## [geohash](content/geohash.md)
271271

272+
## [py_compile](content/py_compile.md)
273+
274+
## [uncompyle6](content/uncompyle6.md)
275+
272276
## [tools](content/tools.md)
273277

274278
## [Other_thing](content/other_thing.md)

Diff for: ‎code/compileall_demo.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# coding=utf-8
2+
3+
import compileall
4+
5+
print compileall.compile_file("test.py")
6+
print compileall.compile_dir("home")

Diff for: ‎code/home/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# coding=utf-8

Diff for: ‎code/home/index.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# coding=utf-8
2+
3+
4+
def index():
5+
return "home"

Diff for: ‎code/py_compile_demo.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# coding=utf-8
2+
3+
import py_compile
4+
5+
py_compile.compile("test.py")

Diff for: ‎code/test.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# coding=utf-8
2+
3+
print "test"

Diff for: ‎code/uncompyle6_demo.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# coding=utf-8
2+
3+
import uncompyle6
4+
5+
with open("test.py", "w") as f:
6+
print uncompyle6.uncompyle_file("test.pyc", f)

Diff for: ‎content/libtorrent.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ if __name__ == '__main__':
321321
```
322322
{
323323
"announce"="http://btfans.3322.org:8000/announce" ;tracker 服务器的URL(字符串)
324-
"announce-list"=["http://..","http://.."] ;备用tracker服务器列表(列表)
324+
"announce-list"=[["http://.."],["http://.."]] ;备用tracker服务器列表(列表),会覆盖 announce 字段
325325
"creation date"=1175204110 ;种子创建的时间,Unix标准时间格式
326326
"encoding"="utf-8" ;编码
327327
"comment"="备注"

Diff for: ‎content/pickle.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,10 @@ My name is Mary and I'm 21 years old
7979
'name': 'Xidian university',
8080
'num': 30000},
8181
'year': 68}
82-
```
82+
```
83+
84+
### 重点
85+
86+
pickle 与 json 的区别,json 的 dumps 和 loads 的返回都是字典,而 pickle 可以是对象,将对象 dumps 然后 loads 出来。
87+
88+
pickle 返回的对象是 完全的重现传入的对象,不仅仅是构造参数,类属性和实例属性。还有私有变量和挂载的的变量方法。

Diff for: ‎content/py_compile.md

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
## py_compile
2+
3+
将 py 文件编译为 pyc 文件,在前两年还说是别人都无法反编译出源文件,但是现在连[在线反编译网站](https://tool.lu/pyc/)都有了。
4+
5+
源代码保护还是需要再想想别的法子.
6+
7+
py_compile 是 python 自带的官方库,在单个 python 文件中边解释边运行,在 python 类库里引用运行的时候就会自动编译出 pyc 文件。
8+
9+
pyc 文件是跨平台,区分版本的。在 python 虚拟机中运行,和 Java 类似。
10+
11+
### 使用
12+
13+
```
14+
# coding=utf-8
15+
16+
import py_compile
17+
18+
py_compile.compile("test.py")
19+
20+
```
21+
22+
或者 在命令行中执行
23+
24+
```
25+
$ python -m py_compile test.py
26+
```
27+
28+
### 高级用法
29+
30+
编译整个文件夹,使用 `compileall`
31+
32+
```
33+
# coding=utf-8
34+
35+
import compileall
36+
37+
# 编译单个文件
38+
print compileall.compile_file("test.py")
39+
40+
# 编译整个文件夹
41+
print compileall.compile_dir("home")
42+
43+
```
44+
45+
1. 使用 `compileall.compile_dir` 实际上也是将文件夹中的源代码单个调用 `compileall.compile_file` 方法
46+
2. 使用 `compileall.compile_file` 实际上也是调用 `py_compile.compile` 方法
47+
3. 编译完成后的 pyc 文件在源目录下
48+
4. 调用成功后会返回 `1`
49+
50+
```
51+
$ python compileall_demo.py
52+
Compiling test.py ...
53+
1
54+
Listing home ...
55+
Compiling home/__init__.py ...
56+
Compiling home/index.py ...
57+
1
58+
```

Diff for: ‎content/uncompyle6.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
## uncompyle6
2+
3+
反编译 pyc 文件为 py 文件。
4+
5+
类似的还有 `uncompyle2` ,顾名思义,只能反编译 python 2 版本的 pyc 文件,而 `uncompyle6` 则是 python 2 和 python 3 通用的
6+
7+
```
8+
# coding=utf-8
9+
10+
import uncompyle6
11+
12+
with open("test.py", "w") as f:
13+
print uncompyle6.uncompyle_file("test.pyc", f)
14+
15+
```
16+
17+
或者使用命令行
18+
19+
```
20+
$ uncompyle2 -o test.py test.pyc
21+
# 2019.05.15 00:01:29 CST
22+
+++ okay decompyling test.pyc
23+
# decompiled 1 files: 1 okay, 0 failed, 0 verify failed
24+
# 2019.05.15 00:01:29 CST
25+
```

0 commit comments

Comments
 (0)
Please sign in to comment.