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

风格不统一 提交于 2020-06-17 03:43:41

问题


I'm experimenting with Cython and possibilities of code obfuscation (article). In that article especially noted:

When the compilation is done there’s no way to reverse compiled libraries back to readable Python source code!

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?). In other wolds, we have only C file as output, and it can't be de-compiled back like .pyc files.

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:

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. 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.


回答1:


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.

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.

Under some circumstances (I'm not sure what though) it links to your .c file instead. 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.



来源:https://stackoverflow.com/questions/53359201/does-compiled-standalone-cython-executable-still-contain-all-original-source-cod

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!