How can I completely disable calls to assert()?
You must #define NDEBUG
(or use the flag -DNDEBUG
with g++) this will disable assert as long as it's defined before the inclusion of the assert header file.
Does the NS_BLOCK_ASSERTIONS disable both NSAssert and assert() calls?
No, assert()
is controlled with the NDEBUG
symbol:
#ifdef NDEBUG
#define assert(e) ((void)0)
#else
...
(see /usr/include/assert.h
if you have the Xcode Command Line Tools installed, or elsewhere in the Xcode.app bundle, if not).
C++ suppress assertions in debug build
If you are talking about standard assert
macro, you can turn it off by defining NDEBUG
macro.
If NDEBUG is defined as a macro name at the point in the source code
where<cassert>
is included, thenassert
does nothing.
Disable assertions in Python
#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.
For the whole process
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
For the environment
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)
Single point in code
You continue your question:
if an assertion fails, I don't want it to throw an AssertionError, but to keep going.
You can 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 if you want the assert expression to be exercised then 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
.
References
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.
Are asserts disabled in release build?
assert()
in the <cassert>
header is only disabled if you define the macro NDEBUG
prior to including the <cassert>
header file. See also these docs
With gcc/g++, the easiest way to do so is to define the NDEBUG
macro on the command line when invoking the compiler like so:
g++ -DNDEBUG ... other args...
Arguments such as optimization flags and similar flags does not disable the assert.
How to turn off ASSERTs in debug mode in Visual Studio 2013
User _CrtSetReportMode
int iPrev = _CrtSetReportMode(_CRT_ASSERT,0);
// Start Operation with no ASSERTs
...
// Restore previous mode with ASSERTs
_CrtSetReportMode(_CRT_ASSERT,iPrev);
Instead of using 0, you can use _CRTDBG_MODE_DEBUG only.
Related Topics
How to Find Where an Exception Was Thrown in C++
How to Mix C++ and C Correctly
Is There a Working C++ Refactoring Tool
Iterative Dfs VS Recursive Dfs and Different Elements Order
Cin.Ignore(Numeric_Limits<Streamsize>::Max(), '\N')
Behavior When Dereferencing the .End() of a Vector of Strings
Passing Shared Pointers as Arguments
How to Convert Cstring and Std::String Std::Wstring to Each Other
Is Reading an Indeterminate Value Undefined Behavior
How to Pass the Windows Defender Smartscreen Protection
Compile a Dll in C/C++, Then Call It from Another Program
Default, Value and Zero Initialization Mess
How to Validate Input Using Scanf
How Boost::Function and Boost::Bind Work
Function Declaration Inside or Outside the Class
How Do Compilers Treat Variable Length Arrays
C++ - Passing References to Std::Shared_Ptr or Boost::Shared_Ptr