Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

debugging - Disabling python's assert() without -0 flag

I'm running a python script from inside a different software (it provides a python interface to manipulate its data structures).

I'm optimizing my code for speed and would like to see what impact on performance my asserts have.

I'm unable to use python -O. What other options do I have, to programatically disable all asserts in python code? The variable __debug__ (which is cleared by -O flag) cannot be assigned to :(

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The docs say,

The value for the built-in variable [__debug__] is determined when the interpreter starts.

So, if you can not control how the python interpreter is started, then it looks like you can not disable assert.

Here then are some other options:

  1. The safest way is to manually remove all the assert statements.
  2. If all your assert statements occur on lines by themselves, then perhaps you could remove them with

    sed -i 's/assert /pass #assert /g' script.py
    

    Note that this will mangle your code if other code comes after the assert. For example, the sed command above would comment-out the return in a line like this:

    assert x; return True
    

    which would change the logic of your program.

    If you have code like this, it would probably be best to manually remove the asserts.

  3. There might be a way to remove them programmatically by parsing your script with the tokenize module, but writing such a program to remove asserts may take more time than it would take to manually remove the asserts, especially if this is a one-time job.

  4. If the other piece of software accepts .pyc files, then there is a dirty trick which seems to work on my machine, though note a Python core developer warns against this (See éric Araujo's comment on 2011-09-17). Suppose your script is called script.py.

    • Make a temporary script called, say, temp.py:

      import script
      
    • Run python -O temp.py. This creates script.pyo.
    • Move script.py and script.pyc (if it exists) out of your PYTHONPATH or whatever directory the other software is reading to find your script.
    • Rename script.pyo --> script.pyc.

    Now when the other software tries to import your script, it will only find the pyc file, which has the asserts removed.

    For example, if script.py looks like this:

    assert False
    print('Got here')
    

    then running python temp.py will now print Got here instead of raising an AssertionError.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...