简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM