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

前端 未结 2 1820
悲哀的现实
悲哀的现实 2020-12-09 03:57

Are there any ways to decompile a dll and/or a .pyd file in order to extract source code written in Python?

Thanks in advance

2条回答
  •  -上瘾入骨i
    2020-12-09 04:47

    I don't agree with the accepted answer, it seems that yes, the content of the source code is accessible even in a .pyd.

    Let's see for example what happens if an error arrives:

    1) Create this file:

    whathappenswhenerror.pyx

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

    2) Compile it with 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) Now import the .pyd file in a standard python file:

    testwhathappenswhenerror.py

    import whathappenswhenerror
    

    4) Let's run it with python testwhathappenswhenerror.py. Here is the output:

    hello 
    6 
    Traceback (most recent call last):
      File "D:\testwhathappenswhenerror.py", line 1, in 
        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
    

    As you can see the line of code print 1/0 # this will generate an error that was in the .pyx source code is displayed! Even the comment is displayed!

    4 bis) If I delete (or move somewhere else) the original .pyx file before step 3), then the original code print 1/0 # this will generate an error is no longer displayed:

    hello
    6
    Traceback (most recent call last):
      File "D:\testwhathappenswhenerror.py", line 1, in 
        import whathappenswhenerror
      File "whathappenswhenerror.pyx", line 4, in init whathappenswhenerror (whathappenswhenerror.c:824)
    ZeroDivisionError: integer division or modulo by zero
    

    But does this mean it's not included in the .pyd? I'm not sure.

提交回复
热议问题