[Python-3000-checkins] r61193 - in python/branches/py3k/Lib: hmac.py test/test_largefile.py test/test_pep247.py test/test_pty.py

alexandre.vassalotti python-3000-checkins at python.org
Mon Mar 3 03:59:50 CET 2008


Author: alexandre.vassalotti
Date: Mon Mar  3 03:59:49 2008
New Revision: 61193

Modified:
   python/branches/py3k/Lib/hmac.py
   python/branches/py3k/Lib/test/test_largefile.py
   python/branches/py3k/Lib/test/test_pep247.py
   python/branches/py3k/Lib/test/test_pty.py
Log:
Fixed failing unit tests due to str/bytes mismatch.
Changed "assert isinstance(...)" in hmac.py to raise proper TypeError.


Modified: python/branches/py3k/Lib/hmac.py
==============================================================================
--- python/branches/py3k/Lib/hmac.py	(original)
+++ python/branches/py3k/Lib/hmac.py	Mon Mar  3 03:59:49 2008
@@ -39,7 +39,8 @@
         if key is _secret_backdoor_key: # cheap
             return
 
-        assert isinstance(key, bytes), repr(key)
+        if not isinstance(key, bytes):
+            raise TypeError("expected bytes, but got %r" % type(key).__name__)
 
         if digestmod is None:
             import hashlib
@@ -84,7 +85,8 @@
     def update(self, msg):
         """Update this hashing object with the string msg.
         """
-        assert isinstance(msg, bytes), repr(msg)
+        if not isinstance(msg, bytes):
+            raise TypeError("expected bytes, but got %r" % type(msg).__name__)
         self.inner.update(msg)
 
     def copy(self):

Modified: python/branches/py3k/Lib/test/test_largefile.py
==============================================================================
--- python/branches/py3k/Lib/test/test_largefile.py	(original)
+++ python/branches/py3k/Lib/test/test_largefile.py	Mon Mar  3 03:59:49 2008
@@ -31,10 +31,10 @@
             print('create large file via seek (may be sparse file) ...')
         f = open(TESTFN, 'wb')
         try:
-            f.write('z')
+            f.write(b'z')
             f.seek(0)
             f.seek(size)
-            f.write('a')
+            f.write(b'a')
             f.flush()
             if verbose:
                 print('check file size with os.fstat')
@@ -53,7 +53,7 @@
         f = open(TESTFN, 'rb')
         try:
             self.assertEqual(f.tell(), 0)
-            self.assertEqual(f.read(1), 'z')
+            self.assertEqual(f.read(1), b'z')
             self.assertEqual(f.tell(), 1)
             f.seek(0)
             self.assertEqual(f.tell(), 0)
@@ -76,9 +76,9 @@
             f.seek(size)
             self.assertEqual(f.tell(), size)
             # the 'a' that was written at the end of file above
-            self.assertEqual(f.read(1), 'a')
+            self.assertEqual(f.read(1), b'a')
             f.seek(-size-1, 1)
-            self.assertEqual(f.read(1), 'z')
+            self.assertEqual(f.read(1), b'z')
             self.assertEqual(f.tell(), 1)
         finally:
             f.close()
@@ -97,7 +97,7 @@
             self.assertEqual(os.lseek(f.fileno(), -size-1, 2), 0)
             self.assertEqual(os.lseek(f.fileno(), size, 0), size)
             # the 'a' that was written at the end of file above
-            self.assertEqual(f.read(1), 'a')
+            self.assertEqual(f.read(1), b'a')
         finally:
             f.close()
 
@@ -155,7 +155,7 @@
             f.seek(2147483649)
             # Seeking is not enough of a test: you must write and
             # flush, too!
-            f.write("x")
+            f.write(b'x')
             f.flush()
         except (IOError, OverflowError):
             f.close()

Modified: python/branches/py3k/Lib/test/test_pep247.py
==============================================================================
--- python/branches/py3k/Lib/test/test_pep247.py	(original)
+++ python/branches/py3k/Lib/test/test_pep247.py	Mon Mar  3 03:59:49 2008
@@ -12,25 +12,25 @@
 
     if key is not None:
         obj1 = module.new(key)
-        obj2 = module.new(key, "string")
+        obj2 = module.new(key, b"string")
 
-        h1 = module.new(key, "string").digest()
-        obj3 = module.new(key) ; obj3.update("string") ; h2 = obj3.digest()
+        h1 = module.new(key, b"string").digest()
+        obj3 = module.new(key) ; obj3.update(b"string") ; h2 = obj3.digest()
         assert h1 == h2, "Hashes must match"
 
     else:
         obj1 = module.new()
-        obj2 = module.new("string")
+        obj2 = module.new(b"string")
 
-        h1 = module.new("string").digest()
-        obj3 = module.new() ; obj3.update("string") ; h2 = obj3.digest()
+        h1 = module.new(b"string").digest()
+        obj3 = module.new() ; obj3.update(b"string") ; h2 = obj3.digest()
         assert h1 == h2, "Hashes must match"
 
     assert hasattr(obj1, 'digest_size'), "Objects must have digest_size attr"
     if module.digest_size is not None:
         assert obj1.digest_size == module.digest_size, "digest_size must match"
     assert obj1.digest_size == len(h1), "digest_size must match actual size"
-    obj1.update("string")
+    obj1.update(b"string")
     obj_copy = obj1.copy()
     assert obj1.digest() == obj_copy.digest(), "Copied objects must match"
     assert obj1.hexdigest() == obj_copy.hexdigest(), \
@@ -38,11 +38,11 @@
     digest, hexdigest = obj1.digest(), obj1.hexdigest()
     hd2 = ""
     for byte in digest:
-        hd2 += "%02x" % ord(byte)
+        hd2 += "%02x" % byte
     assert hd2 == hexdigest, "hexdigest doesn't appear correct"
 
     print('Module', module.__name__, 'seems to comply with PEP 247')
 
 
 if __name__ == '__main__':
-    check_hash_module(hmac, key='abc')
+    check_hash_module(hmac, key=b'abc')

Modified: python/branches/py3k/Lib/test/test_pty.py
==============================================================================
--- python/branches/py3k/Lib/test/test_pty.py	(original)
+++ python/branches/py3k/Lib/test/test_pty.py	Mon Mar  3 03:59:49 2008
@@ -157,7 +157,8 @@
                     break
                 if not data:
                     break
-                sys.stdout.write(data.replace('\r\n', '\n').decode('ascii'))
+                sys.stdout.write(str(data.replace(b'\r\n', b'\n'),
+                                     encoding='ascii'))
 
             ##line = os.read(master_fd, 80)
             ##lines = line.replace('\r\n', '\n').split('\n')


More information about the Python-3000-checkins mailing list