Remove doc strings but not asserts from compiled CPython

好久不见. 提交于 2019-12-23 10:24:22

问题


I want to remove doc strings from a Python program but leave asserts (and __debug__ sections) in. I have been using the -OO flag to generate .pyo files but according to the documentation that removes both asserts and doc strings.

I'm using CPython 2.7.

Clarification: I'm removing the docstrings as a cheap obfuscation method. Management made that decision and I think whether that is a useful thing to do is outside the scope of this question.


回答1:


You can't have half of -O. It removes docstrings and asserts, that's just the way it works. Asserts are usually written to be optional anyway, that is, you can remove them without affecting the behavior of the program.

If I had to remove docstrings and leave all else alone, I would consider writing a tool that opened .pyc files, unmarshaled the contents, modified them (by removing the docstrings), and repacked them as .pyc files. But that would likely be a delicate and fragile tool, if it can even be done that way.



来源:https://stackoverflow.com/questions/11496532/remove-doc-strings-but-not-asserts-from-compiled-cpython

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