简体   繁体   English

编译后的独立Cython可执行文件是否仍包含所有原始源代码?

[英]Does compiled standalone Cython executable still contain all original source code?

I'm experimenting with Cython and possibilities of code obfuscation ( article ). 我正在试验Cython和代码混淆的可能性( 文章 )。 In that article especially noted: 在该文章中特别指出:

When the compilation is done there's no way to reverse compiled libraries back to readable Python source code! 编译完成后,无法将已编译的库反向还原为可读的Python源代码!

I use this question info to compile my code in standalone executable. 我使用此问题信息在独立的可执行文件中编译我的代码。 In my understanding and as mentioned in article 1 , Cython translates Python code into C code, with correspond calls of Python library (is this correct?). 根据我的理解,并且如第1条所述 ,Cython通过Python库的对应调用将Python代码转换为C代码(这是正确的吗?)。 In other wolds, we have only C file as output, and it can't be de-compiled back like .pyc files. 在其他软件包中,我们只有C文件作为输出,并且无法像.pyc文件一样反编译。

My test code is very simple: 我的测试代码非常简单:

def my_crashing_function():
    x = "1"
    y = 2
    test = x + y  # we will crash here

if __name__ == "__main__":
    my_crashing_function()

But when I run this executable (after cython --embed -o test.c main.py and gcc -Os -I /usr/include/python3.5m -o test test.c -lpython3.5m -lpthread -lm -lutil -ldl -s ) I get error like this: 但是当我运行此可执行文件时(在cython --embed -o test.c main.pygcc -Os -I /usr/include/python3.5m -o test test.c -lpython3.5m -lpthread -lm -lutil -ldl -s )我得到这样的错误:

user@debian:~# ./hello 
Traceback (most recent call last):
  File "main.py", line 7, in init main
   my_crashing_function()
  File "main.py", line 4, in main.my_crashing_function
   test = x + y  # we will crash here
TypeError: Can't convert 'int' object to str implicitly

As you see, we have traceback with all method names, correct variable names and lines, even with original source code comments. 如您所见,我们具有所有方法名称,正确的变量名称和行的追溯,甚至包含原始源代码注释。 If we rename variable or change comment - it will be also changed in traceback. 如果我们重命名变量或更改注释-在回溯中也将对其进行更改。 I don't understand how traceback can display all this info. 我不明白回溯如何显示所有这些信息。 It can work that way only if the full source code is also stored in the executable file. 仅当完整的源代码也存储在可执行文件中时,它才能以这种方式工作。 Please explain me, where I'm wrong? 请向我解释,我哪里错了?

Update. 更新。 Traceback in my situation was generated from original .py file. 在我的情况下,追溯是从原始.py文件生成的。 This file was in the same folder as compiled executable, and only because of this I got all source code and comments in traceback. 该文件与已编译的可执行文件位于同一文件夹中,仅由于这个原因,我在追溯中获得了所有源代码和注释。 After deletion of original .py file traceback will contain only exception type and line numbers, without other info. 删除原始.py文件后,回溯将仅包含异常类型和行号,而没有其他信息。

No, it does not embed the code. 不,它不嵌入代码。 It relies on being able to find the .pyx file - if you move that file then you will get a traceback but without the original code. 它依赖于能够找到.pyx文件-如果您移动该文件,则将获得回溯但没有原始代码。

If you inspect the generated C source you'll find that the error handling routine goes through __Pyx_AddTraceback , then __Pyx_CreateCodeObjectForTraceback , which creates a PyCodeObject linked to your .pyx source file. 如果您检查生成的C源代码,你会发现,错误处理程序通过云__Pyx_AddTraceback ,然后__Pyx_CreateCodeObjectForTraceback ,它创建了一个PyCodeObject链接到您的.pyx源文件。

Under some circumstances (I'm not sure what though) it links to your .c file instead. 在某些情况下(我不确定),它会链接到您的.c文件。 The same rules will apply though - if it can't find the source it won't show that line. 但是,将应用相同的规则-如果找不到源,它将不会显示该行。


Even without the .pyx file you will still get a traceback with useful method names - these are preserved in the compiled executable. 即使没有.pyx文件,您仍将获得有用的方法名称回溯-这些名称保留在编译的可执行文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 cython 可以按原样编译所有 python 代码吗? - Can all python code be compiled by cython as is? Cython:编译一个独立的静态可执行文件 - Cython: Compile a Standalone Static Executable 独立的 Python 可执行文件(使用 PyInstaller 编译)无法通过代理,但是当执行未编译的源代码时,一切正常 - Standalone Python executable (compiled with PyInstaller) cannot pass through proxy, but when uncompiled source is executed everything works properly 如果执行未编译的 Python 代码,在使用 Cython 构建的可执行文件中可以看到什么? - What is visible in an executable built with Cython, in case non-compiled Python code is executed? 使用 PyInstaller 将 Cython 编译的模块和 python 代码构建为可执行二进制文件 - Build Cython-compiled modules and python code into executable binary using PyInstaller 从Cython代码创建可执行文件 - Creating an executable from Cython Code 包含函数exec的python代码是否可以编译为可执行文件? - Does a python code with the function exec in it get compiled to an executable? 将预编译的Cython代码分发给Windows - Distribute pre-compiled Cython code to Windows 使用 PyInstaller 构建 Cython 编译的 Python 代码 - Building Cython-compiled python code with PyInstaller 使用 Cython 将 Python 代码编译为静态链接的可执行文件 - Compile Python code to statically linked executable with Cython
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM