[Python-3000-checkins] r57572 - in python/branches/py3k/Lib: base64.py binhex.py test/string_tests.py test/test_bytes.py test/test_cgi.py test/test_codeccallbacks.py test/test_codecs.py test/test_complex.py wave.py

guido.van.rossum python-3000-checkins at python.org
Mon Aug 27 22:40:11 CEST 2007


Author: guido.van.rossum
Date: Mon Aug 27 22:40:10 2007
New Revision: 57572

Modified:
   python/branches/py3k/Lib/base64.py
   python/branches/py3k/Lib/binhex.py
   python/branches/py3k/Lib/test/string_tests.py
   python/branches/py3k/Lib/test/test_bytes.py
   python/branches/py3k/Lib/test/test_cgi.py
   python/branches/py3k/Lib/test/test_codeccallbacks.py
   python/branches/py3k/Lib/test/test_codecs.py
   python/branches/py3k/Lib/test/test_complex.py
   python/branches/py3k/Lib/wave.py
Log:
Changes in anticipation of stricter str vs. bytes enforcement.


Modified: python/branches/py3k/Lib/base64.py
==============================================================================
--- python/branches/py3k/Lib/base64.py	(original)
+++ python/branches/py3k/Lib/base64.py	Mon Aug 27 22:40:10 2007
@@ -54,7 +54,7 @@
     encoded = binascii.b2a_base64(s)[:-1]
     if altchars is not None:
         if not isinstance(altchars, bytes):
-            altchars = bytes(altchars)
+            altchars = bytes(altchars, "ascii")
         assert len(altchars) == 2, repr(altchars)
         return _translate(encoded, {'+': altchars[0:1], '/': altchars[1:2]})
     return encoded
@@ -75,7 +75,7 @@
         s = bytes(s)
     if altchars is not None:
         if not isinstance(altchars, bytes):
-            altchars = bytes(altchars)
+            altchars = bytes(altchars, "ascii")
         assert len(altchars) == 2, repr(altchars)
         s = _translate(s, {chr(altchars[0]): b'+', chr(altchars[1]): b'/'})
     return binascii.a2b_base64(s)
@@ -239,7 +239,7 @@
             acc = 0
             shift = 35
     # Process the last, partial quanta
-    last = binascii.unhexlify(bytes('%010x' % acc))
+    last = binascii.unhexlify(bytes('%010x' % acc, "ascii"))
     if padchars == 0:
         last = b''                      # No characters
     elif padchars == 1:
@@ -323,8 +323,7 @@
 
 def encodestring(s):
     """Encode a string into multiple lines of base-64 data."""
-    if not isinstance(s, bytes):
-        s = bytes(s)
+    assert isinstance(s, bytes), repr(s)
     pieces = []
     for i in range(0, len(s), MAXBINSIZE):
         chunk = s[i : i + MAXBINSIZE]
@@ -334,8 +333,7 @@
 
 def decodestring(s):
     """Decode a string."""
-    if not isinstance(s, bytes):
-        s = bytes(s)
+    assert isinstance(s, bytes), repr(s)
     return binascii.a2b_base64(s)
 
 

Modified: python/branches/py3k/Lib/binhex.py
==============================================================================
--- python/branches/py3k/Lib/binhex.py	(original)
+++ python/branches/py3k/Lib/binhex.py	Mon Aug 27 22:40:10 2007
@@ -191,8 +191,8 @@
         nl = len(name)
         if nl > 63:
             raise Error, 'Filename too long'
-        d = bytes([nl]) + bytes(name) + b'\0'
-        d2 = bytes(finfo.Type) + bytes(finfo.Creator)
+        d = bytes([nl]) + name.encode("latin-1") + b'\0'
+        d2 = bytes(finfo.Type, "ascii") + bytes(finfo.Creator, "ascii")
 
         # Force all structs to be packed with big-endian
         d3 = struct.pack('>h', finfo.Flags)

Modified: python/branches/py3k/Lib/test/string_tests.py
==============================================================================
--- python/branches/py3k/Lib/test/string_tests.py	(original)
+++ python/branches/py3k/Lib/test/string_tests.py	Mon Aug 27 22:40:10 2007
@@ -45,40 +45,44 @@
         else:
             return obj
 
-    # check that object.method(*args) returns result
-    def checkequal(self, result, object, methodname, *args):
+    # check that obj.method(*args) returns result
+    def checkequal(self, result, obj, methodname, *args):
         result = self.fixtype(result)
-        object = self.fixtype(object)
+        obj = self.fixtype(obj)
         args = self.fixtype(args)
-        realresult = getattr(object, methodname)(*args)
+        realresult = getattr(obj, methodname)(*args)
         self.assertEqual(
             result,
             realresult
         )
         # if the original is returned make sure that
         # this doesn't happen with subclasses
-        if object == realresult:
-            class subtype(self.__class__.type2test):
-                pass
-            object = subtype(object)
-            realresult = getattr(object, methodname)(*args)
-            self.assert_(object is not realresult)
-
-    # check that object.method(*args) raises exc
-    def checkraises(self, exc, object, methodname, *args):
-        object = self.fixtype(object)
+        if obj is realresult:
+            try:
+                class subtype(self.__class__.type2test):
+                    pass
+            except TypeError:
+                pass  # Skip this if we can't subclass
+            else:
+                obj = subtype(obj)
+                realresult = getattr(obj, methodname)(*args)
+                self.assert_(obj is not realresult)
+
+    # check that obj.method(*args) raises exc
+    def checkraises(self, exc, obj, methodname, *args):
+        obj = self.fixtype(obj)
         args = self.fixtype(args)
         self.assertRaises(
             exc,
-            getattr(object, methodname),
+            getattr(obj, methodname),
             *args
         )
 
-    # call object.method(*args) without any checks
-    def checkcall(self, object, methodname, *args):
-        object = self.fixtype(object)
+    # call obj.method(*args) without any checks
+    def checkcall(self, obj, methodname, *args):
+        obj = self.fixtype(obj)
         args = self.fixtype(args)
-        getattr(object, methodname)(*args)
+        getattr(obj, methodname)(*args)
 
     def test_count(self):
         self.checkequal(3, 'aaa', 'count', 'a')
@@ -118,14 +122,14 @@
                 i, m = divmod(i, base)
                 entry.append(charset[m])
             teststrings.add(''.join(entry))
-        teststrings = list(teststrings)
+        teststrings = [self.fixtype(ts) for ts in teststrings]
         for i in teststrings:
-            i = self.fixtype(i)
             n = len(i)
             for j in teststrings:
                 r1 = i.count(j)
                 if j:
-                    r2, rem = divmod(n - len(i.replace(j, '')), len(j))
+                    r2, rem = divmod(n - len(i.replace(j, self.fixtype(''))),
+                                     len(j))
                 else:
                     r2, rem = len(i)+1, 0
                 if rem or r1 != r2:
@@ -157,9 +161,8 @@
                 i, m = divmod(i, base)
                 entry.append(charset[m])
             teststrings.add(''.join(entry))
-        teststrings = list(teststrings)
+        teststrings = [self.fixtype(ts) for ts in teststrings]
         for i in teststrings:
-            i = self.fixtype(i)
             for j in teststrings:
                 loc = i.find(j)
                 r1 = (loc != -1)

Modified: python/branches/py3k/Lib/test/test_bytes.py
==============================================================================
--- python/branches/py3k/Lib/test/test_bytes.py	(original)
+++ python/branches/py3k/Lib/test/test_bytes.py	Mon Aug 27 22:40:10 2007
@@ -528,9 +528,9 @@
 
     def test_count(self):
         b = b'mississippi'
-        self.assertEqual(b.count('i'), 4)
-        self.assertEqual(b.count('ss'), 2)
-        self.assertEqual(b.count('w'), 0)
+        self.assertEqual(b.count(b'i'), 4)
+        self.assertEqual(b.count(b'ss'), 2)
+        self.assertEqual(b.count(b'w'), 0)
 
     def test_append(self):
         b = b'hell'
@@ -551,58 +551,58 @@
 
     def test_startswith(self):
         b = b'hello'
-        self.assertFalse(bytes().startswith("anything"))
-        self.assertTrue(b.startswith("hello"))
-        self.assertTrue(b.startswith("hel"))
-        self.assertTrue(b.startswith("h"))
-        self.assertFalse(b.startswith("hellow"))
-        self.assertFalse(b.startswith("ha"))
+        self.assertFalse(bytes().startswith(b"anything"))
+        self.assertTrue(b.startswith(b"hello"))
+        self.assertTrue(b.startswith(b"hel"))
+        self.assertTrue(b.startswith(b"h"))
+        self.assertFalse(b.startswith(b"hellow"))
+        self.assertFalse(b.startswith(b"ha"))
 
     def test_endswith(self):
         b = b'hello'
-        self.assertFalse(bytes().endswith("anything"))
-        self.assertTrue(b.endswith("hello"))
-        self.assertTrue(b.endswith("llo"))
-        self.assertTrue(b.endswith("o"))
-        self.assertFalse(b.endswith("whello"))
-        self.assertFalse(b.endswith("no"))
+        self.assertFalse(bytes().endswith(b"anything"))
+        self.assertTrue(b.endswith(b"hello"))
+        self.assertTrue(b.endswith(b"llo"))
+        self.assertTrue(b.endswith(b"o"))
+        self.assertFalse(b.endswith(b"whello"))
+        self.assertFalse(b.endswith(b"no"))
 
     def test_find(self):
         b = b'mississippi'
-        self.assertEqual(b.find('ss'), 2)
-        self.assertEqual(b.find('ss', 3), 5)
-        self.assertEqual(b.find('ss', 1, 7), 2)
-        self.assertEqual(b.find('ss', 1, 3), -1)
-        self.assertEqual(b.find('w'), -1)
-        self.assertEqual(b.find('mississippian'), -1)
+        self.assertEqual(b.find(b'ss'), 2)
+        self.assertEqual(b.find(b'ss', 3), 5)
+        self.assertEqual(b.find(b'ss', 1, 7), 2)
+        self.assertEqual(b.find(b'ss', 1, 3), -1)
+        self.assertEqual(b.find(b'w'), -1)
+        self.assertEqual(b.find(b'mississippian'), -1)
 
     def test_rfind(self):
         b = b'mississippi'
-        self.assertEqual(b.rfind('ss'), 5)
-        self.assertEqual(b.rfind('ss', 3), 5)
-        self.assertEqual(b.rfind('ss', 0, 6), 2)
-        self.assertEqual(b.rfind('w'), -1)
-        self.assertEqual(b.rfind('mississippian'), -1)
+        self.assertEqual(b.rfind(b'ss'), 5)
+        self.assertEqual(b.rfind(b'ss', 3), 5)
+        self.assertEqual(b.rfind(b'ss', 0, 6), 2)
+        self.assertEqual(b.rfind(b'w'), -1)
+        self.assertEqual(b.rfind(b'mississippian'), -1)
 
     def test_index(self):
         b = b'world'
-        self.assertEqual(b.index('w'), 0)
-        self.assertEqual(b.index('orl'), 1)
-        self.assertRaises(ValueError, lambda: b.index('worm'))
-        self.assertRaises(ValueError, lambda: b.index('ldo'))
+        self.assertEqual(b.index(b'w'), 0)
+        self.assertEqual(b.index(b'orl'), 1)
+        self.assertRaises(ValueError, b.index, b'worm')
+        self.assertRaises(ValueError, b.index, b'ldo')
 
     def test_rindex(self):
         # XXX could be more rigorous
         b = b'world'
-        self.assertEqual(b.rindex('w'), 0)
-        self.assertEqual(b.rindex('orl'), 1)
-        self.assertRaises(ValueError, lambda: b.rindex('worm'))
-        self.assertRaises(ValueError, lambda: b.rindex('ldo'))
+        self.assertEqual(b.rindex(b'w'), 0)
+        self.assertEqual(b.rindex(b'orl'), 1)
+        self.assertRaises(ValueError, b.rindex, b'worm')
+        self.assertRaises(ValueError, b.rindex, b'ldo')
 
     def test_replace(self):
         b = b'mississippi'
-        self.assertEqual(b.replace('i', 'a'), b'massassappa')
-        self.assertEqual(b.replace('ss', 'x'), b'mixixippi')
+        self.assertEqual(b.replace(b'i', b'a'), b'massassappa')
+        self.assertEqual(b.replace(b'ss', b'x'), b'mixixippi')
 
     def test_translate(self):
         b = b'hello'
@@ -614,19 +614,19 @@
 
     def test_split(self):
         b = b'mississippi'
-        self.assertEqual(b.split('i'), [b'm', b'ss', b'ss', b'pp', b''])
-        self.assertEqual(b.split('ss'), [b'mi', b'i', b'ippi'])
-        self.assertEqual(b.split('w'), [b])
+        self.assertEqual(b.split(b'i'), [b'm', b'ss', b'ss', b'pp', b''])
+        self.assertEqual(b.split(b'ss'), [b'mi', b'i', b'ippi'])
+        self.assertEqual(b.split(b'w'), [b])
         # require an arg (no magic whitespace split)
-        self.assertRaises(TypeError, lambda: b.split())
+        self.assertRaises(TypeError, b.split)
 
     def test_rsplit(self):
         b = b'mississippi'
-        self.assertEqual(b.rsplit('i'), [b'm', b'ss', b'ss', b'pp', b''])
-        self.assertEqual(b.rsplit('ss'), [b'mi', b'i', b'ippi'])
-        self.assertEqual(b.rsplit('w'), [b])
+        self.assertEqual(b.rsplit(b'i'), [b'm', b'ss', b'ss', b'pp', b''])
+        self.assertEqual(b.rsplit(b'ss'), [b'mi', b'i', b'ippi'])
+        self.assertEqual(b.rsplit(b'w'), [b])
         # require an arg (no magic whitespace split)
-        self.assertRaises(TypeError, lambda: b.rsplit())
+        self.assertRaises(TypeError, b.rsplit)
 
     def test_partition(self):
         b = b'mississippi'
@@ -695,30 +695,11 @@
             return obj.encode("utf-8")
         return super().fixtype(obj)
 
-    def checkequal(self, result, object, methodname, *args):
-        object = bytes(object, "utf-8")
-        realresult = getattr(bytes, methodname)(object, *args)
-        self.assertEqual(
-            self.fixtype(result),
-            realresult
-        )
-
-    def checkraises(self, exc, object, methodname, *args):
-        object = bytes(object, "utf-8")
-        self.assertRaises(
-            exc,
-            getattr(bytes, methodname),
-            object,
-            *args
-        )
-
     # Currently the bytes containment testing uses a single integer
     # value. This may not be the final design, but until then the
     # bytes section with in a bytes containment not valid
     def test_contains(self):
         pass
-    def test_find(self):
-        pass
     def test_expandtabs(self):
         pass
     def test_upper(self):

Modified: python/branches/py3k/Lib/test/test_cgi.py
==============================================================================
--- python/branches/py3k/Lib/test/test_cgi.py	(original)
+++ python/branches/py3k/Lib/test/test_cgi.py	Mon Aug 27 22:40:10 2007
@@ -232,7 +232,7 @@
                 return a
 
         f = TestReadlineFile(tempfile.TemporaryFile())
-        f.write('x' * 256 * 1024)
+        f.write(b'x' * 256 * 1024)
         f.seek(0)
         env = {'REQUEST_METHOD':'PUT'}
         fs = cgi.FieldStorage(fp=f, environ=env)

Modified: python/branches/py3k/Lib/test/test_codeccallbacks.py
==============================================================================
--- python/branches/py3k/Lib/test/test_codeccallbacks.py	(original)
+++ python/branches/py3k/Lib/test/test_codeccallbacks.py	Mon Aug 27 22:40:10 2007
@@ -675,22 +675,22 @@
         # enhance coverage of:
         # Objects/unicodeobject.c::unicode_decode_call_errorhandler()
         # and callers
-        self.assertRaises(LookupError, "\xff".decode, "ascii", "test.unknown")
+        self.assertRaises(LookupError, b"\xff".decode, "ascii", "test.unknown")
 
         def baddecodereturn1(exc):
             return 42
         codecs.register_error("test.baddecodereturn1", baddecodereturn1)
-        self.assertRaises(TypeError, "\xff".decode, "ascii", "test.baddecodereturn1")
-        self.assertRaises(TypeError, "\\".decode, "unicode-escape", "test.baddecodereturn1")
-        self.assertRaises(TypeError, "\\x0".decode, "unicode-escape", "test.baddecodereturn1")
-        self.assertRaises(TypeError, "\\x0y".decode, "unicode-escape", "test.baddecodereturn1")
-        self.assertRaises(TypeError, "\\Uffffeeee".decode, "unicode-escape", "test.baddecodereturn1")
-        self.assertRaises(TypeError, "\\uyyyy".decode, "raw-unicode-escape", "test.baddecodereturn1")
+        self.assertRaises(TypeError, b"\xff".decode, "ascii", "test.baddecodereturn1")
+        self.assertRaises(TypeError, b"\\".decode, "unicode-escape", "test.baddecodereturn1")
+        self.assertRaises(TypeError, b"\\x0".decode, "unicode-escape", "test.baddecodereturn1")
+        self.assertRaises(TypeError, b"\\x0y".decode, "unicode-escape", "test.baddecodereturn1")
+        self.assertRaises(TypeError, b"\\Uffffeeee".decode, "unicode-escape", "test.baddecodereturn1")
+        self.assertRaises(TypeError, b"\\uyyyy".decode, "raw-unicode-escape", "test.baddecodereturn1")
 
         def baddecodereturn2(exc):
             return ("?", None)
         codecs.register_error("test.baddecodereturn2", baddecodereturn2)
-        self.assertRaises(TypeError, "\xff".decode, "ascii", "test.baddecodereturn2")
+        self.assertRaises(TypeError, b"\xff".decode, "ascii", "test.baddecodereturn2")
 
         handler = PosReturn()
         codecs.register_error("test.posreturn", handler.handle)

Modified: python/branches/py3k/Lib/test/test_codecs.py
==============================================================================
--- python/branches/py3k/Lib/test/test_codecs.py	(original)
+++ python/branches/py3k/Lib/test/test_codecs.py	Mon Aug 27 22:40:10 2007
@@ -559,7 +559,7 @@
     def test_array(self):
         import array
         self.assertEqual(
-            codecs.readbuffer_encode(array.array("b", bytes("spam"))),
+            codecs.readbuffer_encode(array.array("b", b"spam")),
             (b"spam", 4)
         )
 
@@ -573,10 +573,10 @@
 class CharBufferTest(unittest.TestCase):
 
     def test_string(self):
-        self.assertEqual(codecs.charbuffer_encode("spam"), (b"spam", 4))
+        self.assertEqual(codecs.charbuffer_encode(b"spam"), (b"spam", 4))
 
     def test_empty(self):
-        self.assertEqual(codecs.charbuffer_encode(""), (b"", 0))
+        self.assertEqual(codecs.charbuffer_encode(b""), (b"", 0))
 
     def test_bad_args(self):
         self.assertRaises(TypeError, codecs.charbuffer_encode)
@@ -999,19 +999,19 @@
 
     def test_incremental_decode(self):
         self.assertEquals(
-            "".join(codecs.iterdecode((bytes(chr(c)) for c in b"python.org"), "idna")),
+            "".join(codecs.iterdecode((bytes([c]) for c in b"python.org"), "idna")),
             "python.org"
         )
         self.assertEquals(
-            "".join(codecs.iterdecode((bytes(chr(c)) for c in b"python.org."), "idna")),
+            "".join(codecs.iterdecode((bytes([c]) for c in b"python.org."), "idna")),
             "python.org."
         )
         self.assertEquals(
-            "".join(codecs.iterdecode((bytes(chr(c)) for c in b"xn--pythn-mua.org."), "idna")),
+            "".join(codecs.iterdecode((bytes([c]) for c in b"xn--pythn-mua.org."), "idna")),
             "pyth\xf6n.org."
         )
         self.assertEquals(
-            "".join(codecs.iterdecode((bytes(chr(c)) for c in b"xn--pythn-mua.org."), "idna")),
+            "".join(codecs.iterdecode((bytes([c]) for c in b"xn--pythn-mua.org."), "idna")),
             "pyth\xf6n.org."
         )
 

Modified: python/branches/py3k/Lib/test/test_complex.py
==============================================================================
--- python/branches/py3k/Lib/test/test_complex.py	(original)
+++ python/branches/py3k/Lib/test/test_complex.py	Mon Aug 27 22:40:10 2007
@@ -318,11 +318,11 @@
 
         fo = None
         try:
-            fo = open(test_support.TESTFN, "wb")
+            fo = open(test_support.TESTFN, "w")
             print(a, b, file=fo)
             fo.close()
-            fo = open(test_support.TESTFN, "rb")
-            self.assertEqual(fo.read(), ("%s %s\n" % (a, b)).encode("ascii"))
+            fo = open(test_support.TESTFN, "r")
+            self.assertEqual(fo.read(), ("%s %s\n" % (a, b)))
         finally:
             if (fo is not None) and (not fo.closed):
                 fo.close()

Modified: python/branches/py3k/Lib/wave.py
==============================================================================
--- python/branches/py3k/Lib/wave.py	(original)
+++ python/branches/py3k/Lib/wave.py	Mon Aug 27 22:40:10 2007
@@ -459,7 +459,7 @@
             self._write_header(datasize)
 
     def _write_header(self, initlength):
-        self._file.write('RIFF')
+        self._file.write(b'RIFF')
         if not self._nframes:
             self._nframes = initlength / (self._nchannels * self._sampwidth)
         self._datalength = self._nframes * self._nchannels * self._sampwidth


More information about the Python-3000-checkins mailing list