编译后的独立Cython可执行文件是否仍包含所有原始源代码? [英] Does compiled standalone Cython executable still contain all original source code?

查看:170
本文介绍了编译后的独立Cython可执行文件是否仍包含所有原始源代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试验Cython和代码混淆的可能性(文章)。在该文章中特别指出:

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


完成编译后,无法将已编译的库反向转换为可读的Python源代码!

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

我使用此问题信息,将我的代码编译为独立的可执行文件。
以我的理解并在文章 1 ,Cython将Python代码转换为C代码,并调用Python库(这是正确的吗?)。在其他仓库中,我们只有C文件作为输出,并且无法像.pyc文件一样反编译。

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()

但是当我运行此可执行文件时(在 cython之后--embed -o test.c main.py gcc-操作系统-I /usr/include/python3.5m -o测试test.c -lpython3.5m -lpthread -lm -lutil -ldl -s
我得到这样的错误:

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?

更新。在我的情况下,追溯是从原始.py文件生成的。该文件与已编译的可执行文件位于同一文件夹中,仅由于这个原因,我在追溯中获得了所有源代码和注释。删除原始.py文件后,回溯将仅包含异常类型和行号,而没有其他信息。

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.

推荐答案

不,它确实没有嵌入代码。它依赖于能够找到 .pyx 文件-如果移动该文件,则将获得追溯,但没有原始代码。

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.

如果检查生成的C源代码,则会发现错误处理例程通过 __ Pyx_AddTraceback ,然后是 __ Pyx_CreateCodeObjectForTraceback ,它会创建一个 PyCodeObject 链接到您的 .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.

在某些情况下(I (虽然不确定),但它链接到您的 .c 文件。但是,将应用相同的规则-如果找不到源,它将不会显示该行。

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.

甚至没有.pyx文件,您仍然会获得带有有用方法名称的回溯-这些名称保留在编译的可执行文件中。

Even without the .pyx file you will still get a traceback with useful method names - these are preserved in the compiled executable.

这篇关于编译后的独立Cython可执行文件是否仍包含所有原始源代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆