Commits

Amaury Forgeot d'Arc  committed f6ede46

unicode_encode should return bytes

  • Participants
  • Parent commits 8c6f39b
  • Branches py3k

Comments (0)

Files changed (6)

File lib_pypy/_md5.py

     return res & 0xffffffffL
 
 
-class MD5Type:
+class md5:
     "An implementation of the MD5 hash function in pure Python."
 
     digest_size = digestsize = 16
         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
-

File pypy/interpreter/gateway.py

     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

File pypy/module/_md5/__init__.py

 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 = {

File pypy/module/_md5/interp_md5.py

         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())

File pypy/module/_md5/test/test_md5.py

 """
-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
         """
         cls.space = gettestobjspace(usemodules=['_md5'])
         cls.w_md5 = cls.space.appexec([], """():
-            import md5
-            return md5
+            import _md5
+            return _md5
         """)
 
 
         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
 
     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):
         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):
         """
         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'
 

File pypy/objspace/std/unicodetype.py

             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:
         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