From 54f2bc5b81124bfff8fa7f1b7cd6287a1fee1152 Mon Sep 17 00:00:00 2001 From: Dwayne Litzenberger Date: Sun, 22 Jun 2014 03:32:46 -0700 Subject: Fix tests when running under "python -OO" (PYTHONOPTIMIZE set to 1 or 2) --- lib/Crypto/SelfTest/Hash/common.py | 4 +++- .../SelfTest/Random/Fortuna/test_FortunaAccumulator.py | 6 ++++-- lib/Crypto/SelfTest/Util/test_number.py | 6 ++++-- lib/Crypto/SelfTest/st_common.py | 13 +++++++++++++ 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/Crypto/SelfTest/Hash/common.py b/lib/Crypto/SelfTest/Hash/common.py index 2e1a107..7bf0cdc 100644 --- a/lib/Crypto/SelfTest/Hash/common.py +++ b/lib/Crypto/SelfTest/Hash/common.py @@ -43,6 +43,7 @@ if sys.hexversion < 0x02030000: else: dict = dict +from Crypto.SelfTest.st_common import docstrings_disabled from Crypto.Util.strxor import strxor_c class HashDigestSizeSelfTest(unittest.TestCase): @@ -139,7 +140,8 @@ class HashDocStringTest(unittest.TestCase): def runTest(self): docstring = self.hashmod.__doc__ self.assert_(hasattr(self.hashmod, '__doc__')) - self.assert_(isinstance(self.hashmod.__doc__, str)) + if not docstrings_disabled(): # -OO makes docstrings disappear globally + self.assert_(isinstance(self.hashmod.__doc__, str)) class GenericHashConstructorTest(unittest.TestCase): def __init__(self, hashmod): diff --git a/lib/Crypto/SelfTest/Random/Fortuna/test_FortunaAccumulator.py b/lib/Crypto/SelfTest/Random/Fortuna/test_FortunaAccumulator.py index c4e6ccf..c5638e6 100644 --- a/lib/Crypto/SelfTest/Random/Fortuna/test_FortunaAccumulator.py +++ b/lib/Crypto/SelfTest/Random/Fortuna/test_FortunaAccumulator.py @@ -30,6 +30,7 @@ import sys if sys.version_info[0] == 2 and sys.version_info[1] == 1: from Crypto.Util.py21compat import * from Crypto.Util.py3compat import * +from Crypto.SelfTest.st_common import assert_disabled import unittest from binascii import b2a_hex @@ -67,8 +68,9 @@ class FortunaAccumulatorTests(unittest.TestCase): def test_which_pools(self): """FortunaAccumulator.which_pools""" - # which_pools(0) should fail - self.assertRaises(AssertionError, FortunaAccumulator.which_pools, 0) + # which_pools(0) should trigger an assertion failure (unless using -O or -OO) + if not assert_disabled(): + self.assertRaises(AssertionError, FortunaAccumulator.which_pools, 0) self.assertEqual(FortunaAccumulator.which_pools(1), [0]) self.assertEqual(FortunaAccumulator.which_pools(2), [0, 1]) diff --git a/lib/Crypto/SelfTest/Util/test_number.py b/lib/Crypto/SelfTest/Util/test_number.py index 709a774..ac23e91 100644 --- a/lib/Crypto/SelfTest/Util/test_number.py +++ b/lib/Crypto/SelfTest/Util/test_number.py @@ -31,6 +31,7 @@ if sys.version_info[0] == 2 and sys.version_info[1] == 1: from Crypto.Util.py21compat import * import unittest +from Crypto.SelfTest.st_common import assert_disabled class MyError(Exception): """Dummy exception used for tests""" @@ -46,8 +47,9 @@ class MiscTests(unittest.TestCase): def test_ceil_shift(self): """Util.number.ceil_shift""" - self.assertRaises(AssertionError, number.ceil_shift, -1, 1) - self.assertRaises(AssertionError, number.ceil_shift, 1, -1) + if not assert_disabled(): + self.assertRaises(AssertionError, number.ceil_shift, -1, 1) + self.assertRaises(AssertionError, number.ceil_shift, 1, -1) # b = 0 self.assertEqual(0, number.ceil_shift(0, 0)) diff --git a/lib/Crypto/SelfTest/st_common.py b/lib/Crypto/SelfTest/st_common.py index e0e206a..e76525c 100644 --- a/lib/Crypto/SelfTest/st_common.py +++ b/lib/Crypto/SelfTest/st_common.py @@ -72,4 +72,17 @@ def handle_fastmath_import_error(): "it failed. This may point to the gmp or mpir shared library " "not being in the path. _fastmath was found at %s" % (pathname,)) +def docstrings_disabled(): + """Returns True if docstrings are disabled (e.g. by using python -OO)""" + return docstrings_disabled.__doc__ is None + +def assert_disabled(): + """Returns True if 'assert' is a no-op (e.g. by using python -O)""" + try: + assert False + except AssertionError: + return False + else: + return True + # vim:set ts=4 sw=4 sts=4 expandtab: -- cgit v1.2.1