[pypy-svn] r78655 - in pypy/branch/fast-forward/pypy/module/_hashlib: . test

afa at codespeak.net afa at codespeak.net
Tue Nov 2 22:55:02 CET 2010


Author: afa
Date: Tue Nov  2 22:54:57 2010
New Revision: 78655

Modified:
   pypy/branch/fast-forward/pypy/module/_hashlib/interp_hashlib.py
   pypy/branch/fast-forward/pypy/module/_hashlib/test/test_hashlib.py
Log:
_haslib module now accept any buffer object


Modified: pypy/branch/fast-forward/pypy/module/_hashlib/interp_hashlib.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/_hashlib/interp_hashlib.py	(original)
+++ pypy/branch/fast-forward/pypy/module/_hashlib/interp_hashlib.py	Tue Nov  2 22:54:57 2010
@@ -26,13 +26,10 @@
         return space.wrap("<%s HASH object at 0x%s>" % (
             self.name, addrstring))
 
-    @unwrap_spec('self', ObjSpace, str)
-    def update(self, space, buffer):
-        buf = rffi.str2charp(buffer)
-        try:
-            ropenssl.EVP_DigestUpdate(self.ctx, buf, len(buffer))
-        finally:
-            rffi.free_charp(buf)
+    @unwrap_spec('self', ObjSpace, 'bufferstr')
+    def update(self, space, string):
+        with rffi.scoped_nonmovingbuffer(string) as buf:
+            ropenssl.EVP_DigestUpdate(self.ctx, buf, len(string))
 
     @unwrap_spec('self', ObjSpace)
     def copy(self, space):
@@ -117,7 +114,7 @@
     block_size=GetSetProperty(W_Hash.get_block_size),
     )
 
- at unwrap_spec(ObjSpace, str, str)
+ at unwrap_spec(ObjSpace, str, 'bufferstr')
 def new(space, name, string=''):
     w_hash = W_Hash(space, name)
     w_hash.update(space, string)

Modified: pypy/branch/fast-forward/pypy/module/_hashlib/test/test_hashlib.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/_hashlib/test/test_hashlib.py	(original)
+++ pypy/branch/fast-forward/pypy/module/_hashlib/test/test_hashlib.py	Tue Nov  2 22:54:57 2010
@@ -71,3 +71,10 @@
         assert h.digest_size == 16
         assert len(h.hexdigest()) == 32
 
+    def test_buffer(self):
+        import _hashlib, array
+        b = array.array('b', 'x' * 10)
+        h = _hashlib.new('md5', b)
+        h.update(b)
+        assert h.digest() == _hashlib.openssl_md5('x' * 20).digest()
+



More information about the Pypy-commit mailing list