[Python-3000-checkins] r57577 - in python/branches/py3k/Lib: base64.py test/test_xmlrpc.py xmlrpclib.py

guido.van.rossum python-3000-checkins at python.org
Tue Aug 28 00:27:41 CEST 2007


Author: guido.van.rossum
Date: Tue Aug 28 00:27:41 2007
New Revision: 57577

Modified:
   python/branches/py3k/Lib/base64.py
   python/branches/py3k/Lib/test/test_xmlrpc.py
   python/branches/py3k/Lib/xmlrpclib.py
Log:
Force test_xmlrpc to pass.  I'm not happy with how I did this, but I don't
see a better way; the 'Binary' class is poorly specified so it's unclear
what behavior is relied upon.


Modified: python/branches/py3k/Lib/base64.py
==============================================================================
--- python/branches/py3k/Lib/base64.py	(original)
+++ python/branches/py3k/Lib/base64.py	Tue Aug 28 00:27:41 2007
@@ -298,7 +298,7 @@
 MAXBINSIZE = (MAXLINESIZE//4)*3
 
 def encode(input, output):
-    """Encode a file."""
+    """Encode a file; input and output are binary files."""
     while True:
         s = input.read(MAXBINSIZE)
         if not s:
@@ -313,7 +313,7 @@
 
 
 def decode(input, output):
-    """Decode a file."""
+    """Decode a file; input and output are binary files."""
     while True:
         line = input.readline()
         if not line:
@@ -323,7 +323,10 @@
 
 
 def encodestring(s):
-    """Encode a string into multiple lines of base-64 data."""
+    """Encode a string into multiple lines of base-64 data.
+
+    Argument and return value are bytes.
+    """
     if not isinstance(s, bytes):
         raise TypeError("expected bytes, not %s" % s.__class__.__name__)
     pieces = []
@@ -334,7 +337,10 @@
 
 
 def decodestring(s):
-    """Decode a string."""
+    """Decode a string.
+
+    Argument and return value are bytes.
+    """
     if not isinstance(s, bytes):
         raise TypeError("expected bytes, not %s" % s.__class__.__name__)
     return binascii.a2b_base64(s)

Modified: python/branches/py3k/Lib/test/test_xmlrpc.py
==============================================================================
--- python/branches/py3k/Lib/test/test_xmlrpc.py	(original)
+++ python/branches/py3k/Lib/test/test_xmlrpc.py	Tue Aug 28 00:27:41 2007
@@ -14,7 +14,7 @@
           'anint': 2**20,
           'ashortlong': 2,
           'anotherlist': ['.zyx.41'],
-          'abase64': xmlrpclib.Binary("my dog has fleas"),
+          'abase64': xmlrpclib.Binary(b"my dog has fleas"),
           'boolean': False,
           'unicode': '\u4000\u6000\u8000',
           'ukey\u4000': 'regular value',
@@ -32,8 +32,9 @@
 class XMLRPCTestCase(unittest.TestCase):
 
     def test_dump_load(self):
-        self.assertEquals(alist,
-                          xmlrpclib.loads(xmlrpclib.dumps((alist,)))[0][0])
+        dump = xmlrpclib.dumps((alist,))
+        load = xmlrpclib.loads(dump)
+        self.assertEquals(alist, load[0][0])
 
     def test_dump_bare_datetime(self):
         # This checks that an unwrapped datetime.date object can be handled
@@ -222,24 +223,30 @@
         self.assertEqual(t1, tref)
 
 class BinaryTestCase(unittest.TestCase):
+
+    # XXX What should str(Binary(b"\xff")) return?  I'm chosing "\xff"
+    # for now (i.e. interpreting the binary data as Latin-1-encoded
+    # text).  But this feels very unsatisfactory.  Perhaps we should
+    # only define repr(), and return r"Binary(b'\xff')" instead?
+
     def test_default(self):
         t = xmlrpclib.Binary()
         self.assertEqual(str(t), '')
 
     def test_string(self):
-        d = '\x01\x02\x03abc123\xff\xfe'
+        d = b'\x01\x02\x03abc123\xff\xfe'
         t = xmlrpclib.Binary(d)
-        self.assertEqual(str(t), d)
+        self.assertEqual(str(t), str(d, "latin-1"))
 
     def test_decode(self):
-        d = '\x01\x02\x03abc123\xff\xfe'
+        d = b'\x01\x02\x03abc123\xff\xfe'
         de = base64.encodestring(d)
         t1 = xmlrpclib.Binary()
         t1.decode(de)
-        self.assertEqual(str(t1), d)
+        self.assertEqual(str(t1), str(d, "latin-1"))
 
         t2 = xmlrpclib._binary(de)
-        self.assertEqual(str(t2), d)
+        self.assertEqual(str(t2), str(d, "latin-1"))
 
 
 PORT = None

Modified: python/branches/py3k/Lib/xmlrpclib.py
==============================================================================
--- python/branches/py3k/Lib/xmlrpclib.py	(original)
+++ python/branches/py3k/Lib/xmlrpclib.py	Tue Aug 28 00:27:41 2007
@@ -364,6 +364,13 @@
     """Wrapper for binary data."""
 
     def __init__(self, data=None):
+        if data is None:
+            data = b""
+        else:
+            if not isinstance(data, bytes):
+                raise TypeError("expected bytes, not %s" %
+                                data.__class__.__name__)
+            data = bytes(data)  # Make a copy of the bytes!
         self.data = data
 
     ##
@@ -372,7 +379,7 @@
     # @return Buffer contents, as an 8-bit string.
 
     def __str__(self):
-        return self.data or ""
+        return str(self.data, "latin-1")  # XXX encoding?!
 
     def __eq__(self, other):
         if isinstance(other, Binary):
@@ -385,7 +392,7 @@
         return self.data != other
 
     def decode(self, data):
-        self.data = str8(base64.decodestring(data))
+        self.data = base64.decodestring(data)
 
     def encode(self, out):
         out.write("<value><base64>\n")
@@ -827,7 +834,7 @@
 
     def end_base64(self, data):
         value = Binary()
-        value.decode(data)
+        value.decode(data.encode("ascii"))
         self.append(value)
         self._value = 0
     dispatch["base64"] = end_base64


More information about the Python-3000-checkins mailing list