5

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.

3
  • 1
    Why? o_O That seems like an odd requirement. Commented Jul 15, 2012 at 23:51
  • +1, I would like a possibility to do this too Commented Jun 28, 2013 at 12:14
  • Relevant to: stackoverflow.com/q/1273211/1959808 Commented Sep 29, 2017 at 8:28

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I've explained why I need to remove docstrings but not asserts in the question.
Surely it would be much easier and quite robust to write a script to scrub the docstrings from the source files before they are compiled.
@PeterGraham: You didn't explain why you want to keep the asserts – and this is the really odd part of your requirements. Asserts are not meant to be needed in production builds.
There are good reasons to keep assertions in production builds, especially of flight code. See: stackoverflow.com/a/29741912/1959808

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.