From b0dd6901d54e687571e44c4fa1d9c7db0d00e299 Mon Sep 17 00:00:00 2001 From: Julian Taylor Date: Sat, 19 Oct 2013 19:02:51 +0200 Subject: ENH: accept callable as message in assert_ Allows deferring evaluation until failure. Used for blocked minmax test which evaluate array representations for the error message many thousand times accumulating to a full second useless runtime. --- numpy/testing/utils.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'numpy/testing/utils.py') diff --git a/numpy/testing/utils.py b/numpy/testing/utils.py index 1aee5c507..2a99fe5cb 100644 --- a/numpy/testing/utils.py +++ b/numpy/testing/utils.py @@ -33,6 +33,7 @@ verbose = 0 def assert_(val, msg='') : """ Assert that works in release mode. + Accepts callable msg to allow deferring evaluation until failure. The Python built-in ``assert`` does not work when executing code in optimized mode (the ``-O`` flag) - no byte-code is generated for it. @@ -41,7 +42,11 @@ def assert_(val, msg='') : """ if not val : - raise AssertionError(msg) + try: + smsg = msg() + except TypeError: + smsg = msg + raise AssertionError(smsg) def gisnan(x): """like isnan, but always raise an error if type not supported instead of -- cgit v1.2.1