是否可以反编译 .dll/.pyd 文件以提取 Python 源代码?

Is it possible to decompile a .dll/.pyd file to extract Python Source Code?

是否有任何方法可以反编译 dll and/or .pyd 文件以提取用 Python 编写的源代码?

提前致谢

我假设 .pyd/.dll 文件是在 Cython 中创建的,而不是 Python?

无论如何,通常这是不可能的,除非有一个专门为文件最初编译的语言设计的反编译器。虽然我知道 C、C++、Delphi、.NET 和其他一些反编译器,但我还没有听说过 Cython 反编译器。

当然,Cython 所做的是先将您的 Python[esque] 代码转换为 C 代码,这意味着您可能更幸运地找到 C 反编译器,然后占卜原始 Python 代码基于反编译的C代码。至少,通过这种方式,您将处理从一种(相对)高级语言到另一种语言的翻译。

最坏的情况,您将不得不使用反汇编程序。然而,从反汇编器的输出中重新创建 Python 代码并不容易(非常类似于根据构成大脑细胞的蛋白质的化学公式来推断大脑的生物学功能)。

您可能会查看 this question 有关各种反编译器和反汇编器的想法和建议,并从那里进行调查。

我不同意接受的答案,似乎是的,即使在 .pyd 中也可以访问源代码的内容。

让我们看看如果出现错误会发生什么:

1) 创建此文件:

whathappenswhenerror.pyx

A = 6 
print 'hello'
print A
print 1/0 # this will generate an error

2) 用python setup.py build编译它:

setup.py

from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize("whathappenswhenerror.pyx"), include_dirs=[])

3) 现在在标准 python 文件中导入 .pyd 文件:

testwhathappenswhenerror.py

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之二)如果我删除(或移动到其他地方)第3步之前的原始.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

但这是否意味着它不包含在 .pyd 中?我不确定。