summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThorsten Behrens <sbehrens@gmx.li>2010-12-29 05:29:08 -0500
committerThorsten Behrens <sbehrens@gmx.li>2010-12-29 05:29:08 -0500
commitca701f92f538ddd61835a2ec252ebbb8a0b1e4de (patch)
tree7f328bedc0a41e5430bcb70bc535e6069d99e276
parentbaa9119a1448368d618fb2977b47a5c59a389533 (diff)
downloadpycrypto-ca701f92f538ddd61835a2ec252ebbb8a0b1e4de.tar.gz
PY3K support for _fastmath.c; removed floordiv(a,b) hack and replaced with divmod(a,b)[0]; move to assertEqual throughout the test suite to prep for assert_ and failIf being removed in 3.3/3.4
-rw-r--r--lib/Crypto/SelfTest/Protocol/test_chaffing.py4
-rw-r--r--lib/Crypto/SelfTest/Protocol/test_rfc1751.py4
-rw-r--r--lib/Crypto/SelfTest/PublicKey/test_DSA.py4
-rw-r--r--lib/Crypto/SelfTest/PublicKey/test_RSA.py4
-rw-r--r--lib/Crypto/SelfTest/PublicKey/test_importKey.py8
-rw-r--r--lib/Crypto/SelfTest/Util/test_Counter.py12
-rwxr-xr-xlib/Crypto/Util/floordiv.py33
-rw-r--r--lib/Crypto/Util/number.py9
-rwxr-xr-xlib/Crypto/Util/py21floordiv.py33
-rwxr-xr-xpython-3-changes.txt6
-rw-r--r--setup.py2
-rwxr-xr-xsrc/_fastmath.c15
12 files changed, 33 insertions, 101 deletions
diff --git a/lib/Crypto/SelfTest/Protocol/test_chaffing.py b/lib/Crypto/SelfTest/Protocol/test_chaffing.py
index 26c57f8..5fa0120 100644
--- a/lib/Crypto/SelfTest/Protocol/test_chaffing.py
+++ b/lib/Crypto/SelfTest/Protocol/test_chaffing.py
@@ -61,11 +61,11 @@ class ChaffingTest (unittest.TestCase):
c = Chaffing.Chaff(1.0, 1)
c.chaff(data)
chaff = c.chaff(data)
- self.assertEquals(len(chaff), 4)
+ self.assertEqual(len(chaff), 4)
c = Chaffing.Chaff(0.0, 1)
chaff = c.chaff(data)
- self.assertEquals(len(chaff), 2)
+ self.assertEqual(len(chaff), 2)
def get_tests(config={}):
return [ChaffingTest()]
diff --git a/lib/Crypto/SelfTest/Protocol/test_rfc1751.py b/lib/Crypto/SelfTest/Protocol/test_rfc1751.py
index 0dc8239..05ea6c3 100644
--- a/lib/Crypto/SelfTest/Protocol/test_rfc1751.py
+++ b/lib/Crypto/SelfTest/Protocol/test_rfc1751.py
@@ -43,7 +43,7 @@ class RFC1751Test_k2e (unittest.TestCase):
"Check converting keys to English"
for key, words in test_data:
key=binascii.a2b_hex(key)
- self.assertEquals(RFC1751.key_to_english(key), words)
+ self.assertEqual(RFC1751.key_to_english(key), words)
class RFC1751Test_e2k (unittest.TestCase):
@@ -51,7 +51,7 @@ class RFC1751Test_e2k (unittest.TestCase):
"Check converting English strings to keys"
for key, words in test_data:
key=binascii.a2b_hex(key)
- self.assertEquals(RFC1751.english_to_key(words), key)
+ self.assertEqual(RFC1751.english_to_key(words), key)
# class RFC1751Test
diff --git a/lib/Crypto/SelfTest/PublicKey/test_DSA.py b/lib/Crypto/SelfTest/PublicKey/test_DSA.py
index 197f0bb..a518a78 100644
--- a/lib/Crypto/SelfTest/PublicKey/test_DSA.py
+++ b/lib/Crypto/SelfTest/PublicKey/test_DSA.py
@@ -156,8 +156,8 @@ class DSATest(unittest.TestCase):
self.assertRaises(TypeError, dsaObj.sign, m_hash, k)
# Check __eq__ and __ne__
- self.assert_(dsaObj.publickey() == dsaObj.publickey())
- self.assert_(not (dsaObj.publickey() != dsaObj.publickey()))
+ self.assertEqual(dsaObj.publickey() == dsaObj.publickey(),True) # assert_
+ self.assertEqual(dsaObj.publickey() != dsaObj.publickey(),False) # failIf
def _test_signing(self, dsaObj):
k = a2b_hex(self.k)
diff --git a/lib/Crypto/SelfTest/PublicKey/test_RSA.py b/lib/Crypto/SelfTest/PublicKey/test_RSA.py
index 94f7364..e659e2a 100644
--- a/lib/Crypto/SelfTest/PublicKey/test_RSA.py
+++ b/lib/Crypto/SelfTest/PublicKey/test_RSA.py
@@ -216,8 +216,8 @@ class RSATest(unittest.TestCase):
self.assertRaises(TypeError, rsaObj.decrypt, ciphertext)
# Check __eq__ and __ne__
- self.assert_(rsaObj.publickey() == rsaObj.publickey())
- self.assert_(not (rsaObj.publickey() != rsaObj.publickey()))
+ self.assertEqual(rsaObj.publickey() == rsaObj.publickey(),True) # assert_
+ self.assertEqual(rsaObj.publickey() != rsaObj.publickey(),False) # failIf
def _exercise_primitive(self, rsaObj):
# Since we're using a randomly-generated key, we can't check the test
diff --git a/lib/Crypto/SelfTest/PublicKey/test_importKey.py b/lib/Crypto/SelfTest/PublicKey/test_importKey.py
index 35ec56c..0d26f90 100644
--- a/lib/Crypto/SelfTest/PublicKey/test_importKey.py
+++ b/lib/Crypto/SelfTest/PublicKey/test_importKey.py
@@ -77,7 +77,7 @@ Lr7UkvEtFrRhDDKMtuIIq19FrL4pUIMymPMSLBn3hJLe30Dw48GQM4UCAwEAAQ==
def testImportKey1(self):
key = RSA.importKey(self.rsaKeyDER)
- self.failUnless(key.has_private())
+ self.assertEqual(key.has_private(),True) # assert_
self.assertEqual(key.n, self.n)
self.assertEqual(key.e, self.e)
self.assertEqual(key.d, self.d)
@@ -87,13 +87,13 @@ Lr7UkvEtFrRhDDKMtuIIq19FrL4pUIMymPMSLBn3hJLe30Dw48GQM4UCAwEAAQ==
def testImportKey2(self):
key = RSA.importKey(self.rsaPublicKeyDER)
- self.failIf(key.has_private())
+ self.assertEqual(key.has_private(),False) # failIf
self.assertEqual(key.n, self.n)
self.assertEqual(key.e, self.e)
def testImportKey3(self):
key = RSA.importKey(self.rsaKeyPEM)
- self.failUnless(key.has_private())
+ self.assertEqual(key.has_private(),True) # assert_
self.assertEqual(key.n, self.n)
self.assertEqual(key.e, self.e)
self.assertEqual(key.d, self.d)
@@ -103,7 +103,7 @@ Lr7UkvEtFrRhDDKMtuIIq19FrL4pUIMymPMSLBn3hJLe30Dw48GQM4UCAwEAAQ==
def testImportKey4(self):
key = RSA.importKey(self.rsaPublicKeyPEM)
- self.failIf(key.has_private())
+ self.assertEqual(key.has_private(),False) # failIf
self.assertEqual(key.n, self.n)
self.assertEqual(key.e, self.e)
diff --git a/lib/Crypto/SelfTest/Util/test_Counter.py b/lib/Crypto/SelfTest/Util/test_Counter.py
index 135bdfb..9a4ea77 100644
--- a/lib/Crypto/SelfTest/Util/test_Counter.py
+++ b/lib/Crypto/SelfTest/Util/test_Counter.py
@@ -41,20 +41,20 @@ class CounterTests(unittest.TestCase):
def test_BE_shortcut(self):
"""Big endian, shortcut enabled"""
c = Counter.new(128)
- self.assert_(c.__PCT_CTR_SHORTCUT__)
+ self.assertEqual(c.__PCT_CTR_SHORTCUT__,True) # assert_
c = Counter.new(128, little_endian=False)
- self.assert_(c.__PCT_CTR_SHORTCUT__)
+ self.assertEqual(c.__PCT_CTR_SHORTCUT__,True) # assert_
c = Counter.new(128, disable_shortcut=False)
- self.assert_(c.__PCT_CTR_SHORTCUT__)
+ self.assertEqual(c.__PCT_CTR_SHORTCUT__,True) # assert_
c = Counter.new(128, little_endian=False, disable_shortcut=False)
- self.assert_(c.__PCT_CTR_SHORTCUT__)
+ self.assertEqual(c.__PCT_CTR_SHORTCUT__,True) # assert_
def test_LE_shortcut(self):
"""Little endian, shortcut enabled"""
c = Counter.new(128, little_endian=True)
- self.assert_(c.__PCT_CTR_SHORTCUT__)
+ self.assertEqual(c.__PCT_CTR_SHORTCUT__,True) # assert_
c = Counter.new(128, little_endian=True, disable_shortcut=False)
- self.assert_(c.__PCT_CTR_SHORTCUT__)
+ self.assertEqual(c.__PCT_CTR_SHORTCUT__,True) # assert_
def test_BE_no_shortcut(self):
"""Big endian, shortcut disabled"""
diff --git a/lib/Crypto/Util/floordiv.py b/lib/Crypto/Util/floordiv.py
deleted file mode 100755
index 1abc45c..0000000
--- a/lib/Crypto/Util/floordiv.py
+++ /dev/null
@@ -1,33 +0,0 @@
-# -*- coding: utf-8 -*-
-#
-# Util/py21floordiv.py : Compatibility code for handling // operator missing in 2.1
-#
-# Written in 2010 by Thorsten Behrens
-#
-# ===================================================================
-# The contents of this file are dedicated to the public domain. To
-# the extent that dedication to the public domain is not available,
-# everyone is granted a worldwide, perpetual, royalty-free,
-# non-exclusive license to exercise all rights associated with the
-# contents of this file for any purpose whatsoever.
-# No rights are reserved.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
-# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
-# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-# SOFTWARE.
-# ===================================================================
-
-"""Compatibility code for handling // operator missing in 2.1
-"""
-
-__revision__ = "$Id$"
-
-def floordiv(a,b):
- return a//b
-
-# vim:set ts=4 sw=4 sts=4 expandtab:
diff --git a/lib/Crypto/Util/number.py b/lib/Crypto/Util/number.py
index 651acfd..87bdc45 100644
--- a/lib/Crypto/Util/number.py
+++ b/lib/Crypto/Util/number.py
@@ -29,11 +29,6 @@ __revision__ = "$Id$"
from Crypto.pct_warnings import GetRandomNumber_DeprecationWarning
import math
import sys
-if sys.version_info[0] is 2 and sys.version_info[1] is 1:
- from Crypto.Util.py21floordiv import *
-else:
- from Crypto.Util.floordiv import *
-
from Crypto.Util.py3compat import *
bignum = long
@@ -320,7 +315,7 @@ def getStrongPrime(N, e=0, false_positive_prob=1e-6, randfunc=None):
if GCD (e, X-1) != 1:
is_possible_prime = 0
else:
- if GCD (e, floordiv((X-1),2)) != 1:
+ if GCD (e, divmod((X-1),2)[0]) != 1:
is_possible_prime = 0
# do some Rabin-Miller-Tests
@@ -343,7 +338,7 @@ def isPrime(N, false_positive_prob=1e-6, randfunc=None):
The optional false_positive_prob is the statistical probability
that true is returned even though it is not (pseudo-prime).
It defaults to 1e-6 (less than 1:1000000).
- Note that the real probability of a false-positiv is far less. This is
+ Note that the real probability of a false-positive is far less. This is
just the mathematically provable limit.
If randfunc is omitted, then Random.new().read is used.
diff --git a/lib/Crypto/Util/py21floordiv.py b/lib/Crypto/Util/py21floordiv.py
deleted file mode 100755
index cad6cef..0000000
--- a/lib/Crypto/Util/py21floordiv.py
+++ /dev/null
@@ -1,33 +0,0 @@
-# -*- coding: utf-8 -*-
-#
-# Util/floordiv.py : Compatibility code for handling // operator missing in 2.1
-#
-# Written in 2010 by Thorsten Behrens
-#
-# ===================================================================
-# The contents of this file are dedicated to the public domain. To
-# the extent that dedication to the public domain is not available,
-# everyone is granted a worldwide, perpetual, royalty-free,
-# non-exclusive license to exercise all rights associated with the
-# contents of this file for any purpose whatsoever.
-# No rights are reserved.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
-# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
-# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-# SOFTWARE.
-# ===================================================================
-
-"""Compatibility code for handling // operator missing in 2.1
-"""
-
-__revision__ = "$Id$"
-
-def floordiv(a,b):
- return a/b
-
-# vim:set ts=4 sw=4 sts=4 expandtab:
diff --git a/python-3-changes.txt b/python-3-changes.txt
index d1abc66..51f9455 100755
--- a/python-3-changes.txt
+++ b/python-3-changes.txt
@@ -31,8 +31,10 @@ Replace string.split(a) with a.split().
Replace body of white-space-stripping functions with 'return "".join(s.split())'
Integer division via the "/" operator can return a float in 3.x. This causes issues in Util.number.getStrongPrime. As 2.1 does not support
-the "//" operator, a helper function "floordiv()" is brought in via 'if sys.version_info' from floordiv.py or py21floordiv.py.
+the "//" operator, divmod(a,b)[0] is used instead, to conform with an existing practice throughout the rest of the pycrypto code base.
+Do not use assert_/failUnless or failIf. These are deprecated and are scheduled to be removed in Python 3.3 and 3.4.
+Use instead assertEqual(expr,True) for assert_ and assertEqual(expr,False) for failIf
C code:
@@ -68,6 +70,6 @@ TODO:
This is friendlier than just relying on Python's errors.
- Document the expected types for functions. The cipher, the key and the input texts are byte-strings. Plaintext decodes are byte-strings currently, this needs review.
In keeping with how Python 3.x's hash functions work, the input MODE is a text string. hexdigest() returns a text string, and digest() returns a byte-string.
-- Compile and test _fastmath.c
+- Look into LIBPATH/%LIB% and /NODEFAULTLIB:LIBCMT in setup.py for libgmp/libmpir
- Go through test cases and see which modules are not covered
- Look into exclusions in setup.py
diff --git a/setup.py b/setup.py
index 4a1db9b..10aeaee 100644
--- a/setup.py
+++ b/setup.py
@@ -385,6 +385,6 @@ core.setup(**kw)
#PY3K: Workaround for winrandom.pyd not existing during the first pass.
# It needs to be there for 2to3 to fix the import in nt.py
if sys.platform == 'win32' and sys.version_info[0] is 3 and 'build' in sys.argv[1:]:
- PrintErr("Second pass to allow 2to3 to fix nt.py. No cause for alarm.")
+ PrintErr("\nSecond pass to allow 2to3 to fix nt.py. No cause for alarm.\n")
touch("./lib/Crypto/Random/OSRNG/nt.py")
core.setup(**kw)
diff --git a/src/_fastmath.c b/src/_fastmath.c
index 4656181..f3922cf 100755
--- a/src/_fastmath.c
+++ b/src/_fastmath.c
@@ -380,7 +380,7 @@ static PyTypeObject dsaKeyType = {
0, /* call */
#ifdef IS_PY3K
0, /*tp_str*/
- dsaKey_getattro, /*tp_getattro*/
+ (getattrofunc) dsaKey_getattro, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_flags*/
@@ -402,12 +402,13 @@ static PyTypeObject rsaKeyType = {
PyObject_HEAD_INIT (NULL)
0, /*ob_size*/
#endif
- "rsaKey",
- sizeof (rsaKey),
- 0,
+ "rsaKey", /*tp_name*/
+ sizeof (rsaKey), /*tp_size*/
+ 0, /*tp_itemsize*/
+ /* methods */
(destructor) rsaKey_dealloc, /* dealloc */
0, /* print */
-#ifdef IS_PY3k
+#ifdef IS_PY3K
0, /* getattr */
#else
(getattrfunc) rsaKey_getattr, /* getattr */
@@ -422,7 +423,7 @@ static PyTypeObject rsaKeyType = {
0, /* call */
#ifdef IS_PY3K
0, /*tp_str*/
- rsaKey_getattro, /*tp_getattro*/
+ (getattrofunc) rsaKey_getattro, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_flags*/
@@ -477,7 +478,7 @@ dsaKey_dealloc (dsaKey * key)
static PyObject *
#ifdef IS_PY3K
-dsaKey_getattr (dsaKey * key, PyObject *attr)
+dsaKey_getattro (dsaKey * key, PyObject *attr)
#else
dsaKey_getattr (dsaKey * key, char *attr)
#endif