Reputation: 1481
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 :(
Upvotes: 13
Views: 5495
Reputation: 8575
You may be able to do this with an environment variable, as described in this other answer. Setting PYTHONOPTIMIZE=1
is equivalent to starting Python with the -O
option. As an example, this works in Blender 2.78, which embeds Python 3.5:
blender --python-expr 'assert False; print("foo")'
PYTHONOPTIMIZE=1 blender --python-expr 'assert False; print("foo")'
The first command prints a traceback, while the second just prints "foo".
Upvotes: 1
Reputation: 879093
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:
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.
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.
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
python -O temp.py
. This creates script.pyo
.script.py
and script.pyc
(if it exists) out of your PYTHONPATH
or whatever directory the other software is reading to find your
script.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.
Upvotes: 10
Reputation: 7181
As @unutbu describes, there is no official way of doing this. However, a simple strategy is to define a flag like _test
somewhere (for example, as keyword argument to a function, or as a global variable in a module), then include this in your assert statements as follows:
def f(x, _test=True):
assert not _test or x > 0
...
Then you can disable asserts in that function if needed.
f(x, _test=False)
Upvotes: 0