[pypy-commit] pypy py3k: unicode_encode should return bytes

amauryfa noreply at buildbot.pypy.org
Thu Oct 13 22:40:18 CEST 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3k
Changeset: r48025:f6ede46962ce
Date: 2011-10-13 22:38 +0200
http://bitbucket.org/pypy/pypy/changeset/f6ede46962ce/

Log:	unicode_encode should return bytes

diff --git a/lib_pypy/_md5.py b/lib_pypy/_md5.py
--- a/lib_pypy/_md5.py
+++ b/lib_pypy/_md5.py
@@ -112,7 +112,7 @@
     return res & 0xffffffffL
 
 
-class MD5Type:
+class md5:
     "An implementation of the MD5 hash function in pure Python."
 
     digest_size = digestsize = 16
@@ -366,23 +366,3 @@
         clone.C = self.C
         clone.D = self.D
         return clone
-
-
-# ======================================================================
-# Mimic Python top-level functions from standard library API
-# for consistency with the _md5 module of the standard library.
-# ======================================================================
-
-digest_size = 16
-
-def new(arg=None):
-    """Return a new md5 crypto object.
-    If arg is present, the method call update(arg) is made.
-    """
-
-    crypto = MD5Type()
-    if arg:
-        crypto.update(arg)
-
-    return crypto
-
diff --git a/pypy/interpreter/gateway.py b/pypy/interpreter/gateway.py
--- a/pypy/interpreter/gateway.py
+++ b/pypy/interpreter/gateway.py
@@ -825,11 +825,17 @@
     def _getdefaults(self, space):
         "NOT_RPYTHON"
         defs_w = []
-        for val in self._staticdefs:
+        
+        unwrap_spec = self._code._unwrap_spec[-len(self._staticdefs):]
+        for i, val in enumerate(self._staticdefs):
             if val is NoneNotWrapped:
                 defs_w.append(None)
             else:
-                defs_w.append(space.wrap(val))
+                spec = unwrap_spec[i]
+                if spec in ['bufferstr']:
+                    defs_w.append(space.wrapbytes(val))
+                else:
+                    defs_w.append(space.wrap(val))
         return defs_w
 
     # lazy binding to space
diff --git a/pypy/module/_md5/__init__.py b/pypy/module/_md5/__init__.py
--- a/pypy/module/_md5/__init__.py
+++ b/pypy/module/_md5/__init__.py
@@ -19,9 +19,7 @@
 far using the digest() method."""
 
     interpleveldefs = {
-        'new': 'interp_md5.W_MD5',
-        'MD5Type': 'interp_md5.W_MD5',
-        'digest_size': 'space.wrap(16)',
+        'md5': 'interp_md5.W_MD5',
         }
 
     appleveldefs = {
diff --git a/pypy/module/_md5/interp_md5.py b/pypy/module/_md5/interp_md5.py
--- a/pypy/module/_md5/interp_md5.py
+++ b/pypy/module/_md5/interp_md5.py
@@ -18,7 +18,7 @@
         self.update(string)
 
     def digest_w(self):
-        return self.space.wrap(self.digest())
+        return self.space.wrapbytes(self.digest())
 
     def hexdigest_w(self):
         return self.space.wrap(self.hexdigest())
diff --git a/pypy/module/_md5/test/test_md5.py b/pypy/module/_md5/test/test_md5.py
--- a/pypy/module/_md5/test/test_md5.py
+++ b/pypy/module/_md5/test/test_md5.py
@@ -1,5 +1,5 @@
 """
-Tests for the md5 module implemented at interp-level in pypy/module/md5.
+Tests for the md5 module implemented at interp-level in pypy/module/_md5.
 """
 
 import py, sys
@@ -15,8 +15,8 @@
         """
         cls.space = gettestobjspace(usemodules=['_md5'])
         cls.w_md5 = cls.space.appexec([], """():
-            import md5
-            return md5
+            import _md5
+            return _md5
         """)
 
 
@@ -25,8 +25,6 @@
         md5.digest_size should be 16.
         """
         import sys
-        assert self.md5.digest_size == 16
-        #assert self.md5.digestsize == 16        -- not on CPython
         assert self.md5.md5().digest_size == 16
         if sys.version >= (2, 5):
             assert self.md5.blocksize == 1
@@ -35,15 +33,10 @@
 
     def test_MD5Type(self):
         """
-        Test the two ways to construct an md5 object.
+        Test the construction of an md5 object.
         """
         md5 = self.md5
         d = md5.md5()
-        if not hasattr(md5, 'MD5Type'):
-            skip("no md5.MD5Type on CPython")
-        assert isinstance(d, md5.MD5Type)
-        d = md5.new()
-        assert isinstance(d, md5.MD5Type)
 
 
     def test_md5object(self):
@@ -52,26 +45,27 @@
         hexdigest.
         """
         md5 = self.md5
+        import binascii
         cases = (
-          ("",
+          (b"",
            "d41d8cd98f00b204e9800998ecf8427e"),
-          ("a",
+          (b"a",
            "0cc175b9c0f1b6a831c399e269772661"),
-          ("abc",
+          (b"abc",
            "900150983cd24fb0d6963f7d28e17f72"),
-          ("message digest",
+          (b"message digest",
            "f96b697d7cb7938d525a2f31aaf161d0"),
-          ("abcdefghijklmnopqrstuvwxyz",
+          (b"abcdefghijklmnopqrstuvwxyz",
            "c3fcd3d76192e4007dfb496cca67e13b"),
-          ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
+          (b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
            "d174ab98d277d9f5a5611c2c9f419d9f"),
-          ("1234567890"*8,
+          (b"1234567890"*8,
            "57edf4a22be3c955ac49da2e2107b67a"),
         )
         for input, expected in cases:
-            d = md5.new(input)
+            d = md5.md5(input)
             assert d.hexdigest() == expected
-            assert d.digest() == expected.decode('hex')
+            assert d.digest() == binascii.hexlify(expected.encode('ascii'))
 
 
     def test_copy(self):
@@ -80,10 +74,10 @@
         """
         md5 = self.md5
         d1 = md5.md5()
-        d1.update("abcde")
+        d1.update(b"abcde")
         d2 = d1.copy()
-        d2.update("fgh")
-        d1.update("jkl")
+        d2.update(b"fgh")
+        d1.update(b"jkl")
         assert d1.hexdigest() == 'e570e7110ecef72fcb772a9c05d03373'
         assert d2.hexdigest() == 'e8dc4081b13434b45189a720b77b6818'
 
diff --git a/pypy/objspace/std/unicodetype.py b/pypy/objspace/std/unicodetype.py
--- a/pypy/objspace/std/unicodetype.py
+++ b/pypy/objspace/std/unicodetype.py
@@ -221,13 +221,13 @@
             if encoding == 'ascii':
                 u = space.unicode_w(w_object)
                 eh = encode_error_handler(space)
-                return space.wrap(unicode_encode_ascii(u, len(u), None,
-                                                       errorhandler=eh))
+                return space.wrapbytes(unicode_encode_ascii(
+                        u, len(u), None, errorhandler=eh))
             if encoding == 'utf-8':
                 u = space.unicode_w(w_object)
                 eh = encode_error_handler(space)
-                return space.wrap(unicode_encode_utf_8(u, len(u), None,
-                                                       errorhandler=eh))
+                return space.wrapbytes(unicode_encode_utf_8(
+                        u, len(u), None, errorhandler=eh))
         from pypy.module._codecs.interp_codecs import lookup_codec
         w_encoder = space.getitem(lookup_codec(space, encoding), space.wrap(0))
     if errors is None:
@@ -236,9 +236,9 @@
         w_errors = space.wrap(errors)
     w_restuple = space.call_function(w_encoder, w_object, w_errors)
     w_retval = space.getitem(w_restuple, space.wrap(0))
-    if not space.isinstance_w(w_retval, space.w_str):
+    if not space.isinstance_w(w_retval, space.w_bytes):
         raise operationerrfmt(space.w_TypeError,
-            "encoder did not return an string object (type '%s')",
+            "encoder did not return a bytes string (type '%s')",
             space.type(w_retval).getname(space))
     return w_retval
 


More information about the pypy-commit mailing list