How do I disable assertions in Python?
That is, if an assertion fails, I don't want it to throw an AssertionError
, but to keep going.
How do I do that?
We can disable assertions in a program by using NDEBUG macro. Using NDEBUG macro in a program disables all calls to assert.
Definition and Usage. The assert keyword is used when debugging code. The assert keyword lets you test if a condition in your code returns True, if not, the program will raise an AssertionError. You can write a message to be written if the code returns False, check the example below.
AssertionError is inherited from Exception class, when this exception occurs and raises AssertionError there are two ways to handle, either the user handles it or the default exception handler. In Example 1 we have seen how the default exception handler does the work. Now let's dig into handling it manually.
If the assertion fails, Python uses ArgumentExpression as the argument for the AssertionError. AssertionError exceptions can be caught and handled like any other exception using the try-except statement, but if not handled, they will terminate the program and produce a traceback.
How do I disable assertions in Python?
There are multiple approaches that affect a single process, the environment, or a single line of code.
I demonstrate each.
Using the -O
flag (capital O) disables all assert statements in a process.
For example:
$ python -Oc "assert False" $ python -c "assert False" Traceback (most recent call last): File "<string>", line 1, in <module> AssertionError
Note that by disable I mean it also does not execute the expression that follows it:
$ python -Oc "assert 1/0" $ python -c "assert 1/0" Traceback (most recent call last): File "<string>", line 1, in <module> ZeroDivisionError: integer division or modulo by zero
You can use an environment variable to set this flag as well.
This will affect every process that uses or inherits the environment.
E.g., in Windows, setting and then clearing the environment variable:
C:\>python -c "assert False" Traceback (most recent call last): File "<string>", line 1, in <module> AssertionError C:\>SET PYTHONOPTIMIZE=TRUE C:\>python -c "assert False" C:\>SET PYTHONOPTIMIZE= C:\>python -c "assert False" Traceback (most recent call last): File "<string>", line 1, in <module> AssertionError
Same in Unix (using set and unset for respective functionality)
You continue your question:
if an assertion fails, I don't want it to throw an AssertionError, but to keep going.
If you want the code that fails to be exercised, you can catch either ensure control flow does not reach the assertion, for example:
if False: assert False, "we know this fails, but we don't get here"
or you can catch the assertion error:
try: assert False, "this code runs, fails, and the exception is caught" except AssertionError as e: print(repr(e))
which prints:
AssertionError('this code runs, fails, and the exception is caught')
and you'll keep going from the point you handled the AssertionError
.
From the assert
documentation:
An assert statement like this:
assert expression #, optional_message
Is equivalent to
if __debug__: if not expression: raise AssertionError #(optional_message)
And,
the built-in variable
__debug__
isTrue
under normal circumstances,False
when optimization is requested (command line option-O
).
and further
Assignments to
__debug__
are illegal. The value for the built-in variable is determined when the interpreter starts.
From the usage docs:
-O
Turn on basic optimizations. This changes the filename extension for compiled (bytecode) files from .pyc to .pyo. See also PYTHONOPTIMIZE.
and
PYTHONOPTIMIZE
If this is set to a non-empty string it is equivalent to specifying the
-O
option. If set to an integer, it is equivalent to specifying-O
multiple times.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With