Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 378
pbp Avatar asked Dec 28 '11 14:12

pbp


People also ask

How do I disable assert in Python?

Using the -O flag (capital O) disables all assert statements in a process.

How do I stop assertion errors in Python?

Steps to avoid Assertion Error in Python We can use the -O flag to disable all the assert statements present in the process. By disabling the assert statements in the process, the statements following the assert statements will also be not executed.

Should I use assert in Python?

Assert statements are used to debug code and handle errors. You should not use an assert statement in a production environment. Debugging is a crucial part of programming in any language. When debugging programs in Python, there may be times when you want to test for a certain condition.

What does assert () do in Python?

The assert keyword lets you test if a condition in your code returns True, if not, the program will raise an AssertionError.


1 Answers

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.

like image 76
unutbu Avatar answered Sep 18 '22 13:09

unutbu