[Python-3000-checkins] r56216 - in python/branches/py3k-struni: Lib/test/test_hashlib.py Lib/uuid.py Modules/_hashopenssl.c Modules/sha256module.c Modules/sha512module.c

guido.van.rossum python-3000-checkins at python.org
Mon Jul 9 16:29:41 CEST 2007


Author: guido.van.rossum
Date: Mon Jul  9 16:29:40 2007
New Revision: 56216

Modified:
   python/branches/py3k-struni/Lib/test/test_hashlib.py
   python/branches/py3k-struni/Lib/uuid.py
   python/branches/py3k-struni/Modules/_hashopenssl.c
   python/branches/py3k-struni/Modules/sha256module.c
   python/branches/py3k-struni/Modules/sha512module.c
Log:
Change hashlib to return bytes from digest() instead of str8.


Modified: python/branches/py3k-struni/Lib/test/test_hashlib.py
==============================================================================
--- python/branches/py3k-struni/Lib/test/test_hashlib.py	(original)
+++ python/branches/py3k-struni/Lib/test/test_hashlib.py	Mon Jul  9 16:29:40 2007
@@ -12,12 +12,12 @@
 
 
 def hexstr(s):
-    import string
-    h = string.hexdigits
-    r = ''
-    for c in s:
-        i = ord(c)
-        r = r + h[(i >> 4) & 0xF] + h[i & 0xF]
+    assert isinstance(s, bytes), repr(s)
+    h = b"0123456789abcdef"
+    r = b''
+    for i in s:
+        r.append(h[(i >> 4) & 0xF])
+        r.append(h[i & 0xF])
     return r
 
 
@@ -37,7 +37,8 @@
     def test_hexdigest(self):
         for name in self.supported_hash_names:
             h = hashlib.new(name)
-            self.assert_(hexstr(h.digest()) == h.hexdigest())
+            assert isinstance(h.digest(), bytes), name
+            self.assertEqual(hexstr(h.digest()), h.hexdigest())
 
 
     def test_large_update(self):
@@ -66,14 +67,15 @@
 
 
     def test_case_md5_0(self):
-        self.check('md5', '', 'd41d8cd98f00b204e9800998ecf8427e')
+        self.check('md5', b'', b'd41d8cd98f00b204e9800998ecf8427e')
 
     def test_case_md5_1(self):
-        self.check('md5', 'abc', '900150983cd24fb0d6963f7d28e17f72')
+        self.check('md5', b'abc', b'900150983cd24fb0d6963f7d28e17f72')
 
     def test_case_md5_2(self):
-        self.check('md5', 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
-                   'd174ab98d277d9f5a5611c2c9f419d9f')
+        self.check('md5',
+                   b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
+                   b'd174ab98d277d9f5a5611c2c9f419d9f')
 
 
     # use the three examples from Federal Information Processing Standards
@@ -81,20 +83,21 @@
     # http://www.itl.nist.gov/div897/pubs/fip180-1.htm
 
     def test_case_sha1_0(self):
-        self.check('sha1', "",
-                   "da39a3ee5e6b4b0d3255bfef95601890afd80709")
+        self.check('sha1', b"",
+                   b"da39a3ee5e6b4b0d3255bfef95601890afd80709")
 
     def test_case_sha1_1(self):
-        self.check('sha1', "abc",
-                   "a9993e364706816aba3e25717850c26c9cd0d89d")
+        self.check('sha1', b"abc",
+                   b"a9993e364706816aba3e25717850c26c9cd0d89d")
 
     def test_case_sha1_2(self):
-        self.check('sha1', "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
-                   "84983e441c3bd26ebaae4aa1f95129e5e54670f1")
+        self.check('sha1',
+                   b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
+                   b"84983e441c3bd26ebaae4aa1f95129e5e54670f1")
 
     def test_case_sha1_3(self):
-        self.check('sha1', "a" * 1000000,
-                   "34aa973cd4c4daa4f61eeb2bdbad27316534016f")
+        self.check('sha1', b"a" * 1000000,
+                   b"34aa973cd4c4daa4f61eeb2bdbad27316534016f")
 
 
     # use the examples from Federal Information Processing Standards

Modified: python/branches/py3k-struni/Lib/uuid.py
==============================================================================
--- python/branches/py3k-struni/Lib/uuid.py	(original)
+++ python/branches/py3k-struni/Lib/uuid.py	Mon Jul  9 16:29:40 2007
@@ -528,7 +528,7 @@
     """Generate a UUID from the MD5 hash of a namespace UUID and a name."""
     from hashlib import md5
     hash = md5(namespace.bytes + bytes(name, "utf-8")).digest()
-    return UUID(bytes=bytes_(hash[:16]), version=3)
+    return UUID(bytes=hash[:16], version=3)
 
 def uuid4():
     """Generate a random UUID."""
@@ -551,7 +551,7 @@
     """Generate a UUID from the SHA-1 hash of a namespace UUID and a name."""
     from hashlib import sha1
     hash = sha1(namespace.bytes + bytes(name, "utf-8")).digest()
-    return UUID(bytes=bytes_(hash[:16]), version=5)
+    return UUID(bytes=hash[:16], version=5)
 
 # The following standard UUIDs are for use with uuid3() or uuid5().
 

Modified: python/branches/py3k-struni/Modules/_hashopenssl.c
==============================================================================
--- python/branches/py3k-struni/Modules/_hashopenssl.c	(original)
+++ python/branches/py3k-struni/Modules/_hashopenssl.c	Mon Jul  9 16:29:40 2007
@@ -103,7 +103,7 @@
     digest_size = EVP_MD_CTX_size(&temp_ctx);
     EVP_DigestFinal(&temp_ctx, digest, NULL);
 
-    retval = PyString_FromStringAndSize((const char *)digest, digest_size);
+    retval = PyBytes_FromStringAndSize((const char *)digest, digest_size);
     EVP_MD_CTX_cleanup(&temp_ctx);
     return retval;
 }
@@ -133,7 +133,7 @@
     retval = PyString_FromStringAndSize(NULL, digest_size * 2);
     if (!retval)
 	    return NULL;
-    hex_digest = PyString_AsString(retval);
+    hex_digest = PyString_AS_STRING(retval);
     if (!hex_digest) {
 	    Py_DECREF(retval);
 	    return NULL;

Modified: python/branches/py3k-struni/Modules/sha256module.c
==============================================================================
--- python/branches/py3k-struni/Modules/sha256module.c	(original)
+++ python/branches/py3k-struni/Modules/sha256module.c	Mon Jul  9 16:29:40 2007
@@ -432,7 +432,7 @@
 
     SHAcopy(self, &temp);
     sha_final(digest, &temp);
-    return PyString_FromStringAndSize((const char *)digest, self->digestsize);
+    return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
 }
 
 PyDoc_STRVAR(SHA256_hexdigest__doc__,
@@ -510,9 +510,9 @@
 SHA256_get_name(PyObject *self, void *closure)
 {
     if (((SHAobject *)self)->digestsize == 32)
-        return PyString_FromStringAndSize("SHA256", 6);
+        return PyUnicode_FromStringAndSize("SHA256", 6);
     else
-        return PyString_FromStringAndSize("SHA224", 6);
+        return PyUnicode_FromStringAndSize("SHA224", 6);
 }
 
 static PyGetSetDef SHA_getseters[] = {

Modified: python/branches/py3k-struni/Modules/sha512module.c
==============================================================================
--- python/branches/py3k-struni/Modules/sha512module.c	(original)
+++ python/branches/py3k-struni/Modules/sha512module.c	Mon Jul  9 16:29:40 2007
@@ -498,7 +498,7 @@
 
     SHAcopy(self, &temp);
     sha512_final(digest, &temp);
-    return PyString_FromStringAndSize((const char *)digest, self->digestsize);
+    return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
 }
 
 PyDoc_STRVAR(SHA512_hexdigest__doc__,
@@ -576,9 +576,9 @@
 SHA512_get_name(PyObject *self, void *closure)
 {
     if (((SHAobject *)self)->digestsize == 64)
-        return PyString_FromStringAndSize("SHA512", 6);
+        return PyUnicode_FromStringAndSize("SHA512", 6);
     else
-        return PyString_FromStringAndSize("SHA384", 6);
+        return PyUnicode_FromStringAndSize("SHA384", 6);
 }
 
 static PyGetSetDef SHA_getseters[] = {


More information about the Python-3000-checkins mailing list