This PEP aims at offering a runtime assert functionnality, extending the compiletime assert already available.
There is no runtime assert relying on python grammar available. For diagnostics and/or debugging reasons, it would be valuable to add such a feature.
A runtime assert makes sense when extra checks would be needed in a production environment (where non-debug builds are used).
By extending the current python grammar, it would be possible to limit the overhead induces by those runtime asserts when running in a non "assertive" mode (-ed). The idea here is to avoid evaluating the expression when runtime
assertion is not active.
A brief example why avoiding evaluating the expression is needed to avoid any overhead in case the runtime assert should be ignored.
::
runtime_assert( 999 in { i:None for i in range( 10000000 ) } )
::
runtime_assert( expr )
#would result in if expr and runtime_assert_active:
print RuntimeAssertionError()
There is already an implementation available, robust enough for production. The implementation is mainly grammar based, and thus the parser and the grammar needs to be updated:
|
[1] |
PEP 306, How to Change Python's Grammar (http://www.python.org/dev/peps/pep-0306) |
This document has been placed in the public domain.