summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDwayne Litzenberger <dlitz@dlitz.net>2014-06-22 03:32:46 -0700
committerDwayne Litzenberger <dlitz@dlitz.net>2014-06-22 04:07:24 -0700
commit54f2bc5b81124bfff8fa7f1b7cd6287a1fee1152 (patch)
tree177d23b85d97835c8a7ba90598b8b7d5557e100d
parent0ac94701bb52fef566f96ce43eb8db6befee9b60 (diff)
downloadpycrypto-54f2bc5b81124bfff8fa7f1b7cd6287a1fee1152.tar.gz
Fix tests when running under "python -OO" (PYTHONOPTIMIZE set to 1 or 2)
-rw-r--r--lib/Crypto/SelfTest/Hash/common.py4
-rw-r--r--lib/Crypto/SelfTest/Random/Fortuna/test_FortunaAccumulator.py6
-rw-r--r--lib/Crypto/SelfTest/Util/test_number.py6
-rw-r--r--lib/Crypto/SelfTest/st_common.py13
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: