有没有办法反编译 DLL 和/或 .pyd 文件以提取用 Python 编写的源代码?
提前致谢。
有没有办法反编译 DLL 和/或 .pyd 文件以提取用 Python 编写的源代码?
提前致谢。
我不同意被接受的答案,因为看起来即使在.pyd
中,源代码的内容也是可以访问的。
举个例子:如果出现错误,我们来看下会发生什么:
1)创建这个文件:
A = 6
print 'hello'
print A
print 1/0 # this will generate an error
2)使用命令 python setup.py build
进行编译:
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize("whathappenswhenerror.pyx"), include_dirs=[])
3) 现在在标准的Python文件中导入.pyd文件:
import whathappenswhenerror
4) 让我们使用python testwhathappenswhenerror.py
来运行它。以下是输出结果:
hello
6
Traceback (most recent call last):
File "D:\testwhathappenswhenerror.py", line 1, in <module>
import whathappenswhenerror
File "whathappenswhenerror.pyx", line 4, in init whathappenswhenerror (whathappenswhenerror.c:824)
print 1/0 # this will generate an error
ZeroDivisionError: integer division or modulo by zero
正如您所看到的,位于.pyx
源代码中的代码行print 1/0 # this will generate an error
被显示出来了!甚至连注释也被显示了!
4 bis) 如果我在第三步之前删除(或将其移动到其他位置)原始的.pyx
文件,则原始代码print 1/0 # this will generate an error
不再显示:
hello
6
Traceback (most recent call last):
File "D:\testwhathappenswhenerror.py", line 1, in <module>
import whathappenswhenerror
File "whathappenswhenerror.pyx", line 4, in init whathappenswhenerror (whathappenswhenerror.c:824)
ZeroDivisionError: integer division or modulo by zero
pyx
文件会显示Traceback使用了某种链接从pyd
(在Linux中是so
)到pyx
中的行。代码不在pyd
中。 - hpaulj.pyx
文件和import whathappenswhenerror
在同一个位置,那么如何知道它导入的是.pyd
而不是.pyx
?很可能它正在导入.pyx
,因此会显示在错误上的代码。 - krsoniFile "whathappenswhenerror.pyx", line 4
)来判断它是否使用了 pyx
文件。 - Minion Jim