[pypy-svn] pypy default: hash.update() accepts arrays and other objects with the buffer interface.
amauryfa
commits-noreply at bitbucket.org
Wed Jan 26 11:48:22 CET 2011
Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch:
Changeset: r41338:a70edbdf6dd0
Date: 2011-01-26 11:45 +0100
http://bitbucket.org/pypy/pypy/changeset/a70edbdf6dd0/
Log: hash.update() accepts arrays and other objects with the buffer
interface. CPython uses the s* code, try to emulate this in Python
code.
diff --git a/lib_pypy/_sha512.py b/lib_pypy/_sha512.py
--- a/lib_pypy/_sha512.py
+++ b/lib_pypy/_sha512.py
@@ -152,6 +152,14 @@
sha_info['digestsize'] = 48
return sha_info
+def getbuf(s):
+ if isinstance(s, str):
+ return s
+ elif isinstance(s, unicode):
+ return str(s)
+ else:
+ return buffer(s)
+
def sha_update(sha_info, buffer):
count = len(buffer)
buffer_idx = 0
@@ -241,10 +249,10 @@
def __init__(self, s=None):
self._sha = sha_init()
if s:
- sha_update(self._sha, s)
+ sha_update(self._sha, getbuf(s))
def update(self, s):
- sha_update(self._sha, s)
+ sha_update(self._sha, getbuf(s))
def digest(self):
return sha_final(self._sha.copy())[:self._sha['digestsize']]
@@ -263,7 +271,7 @@
def __init__(self, s=None):
self._sha = sha384_init()
if s:
- sha_update(self._sha, s)
+ sha_update(self._sha, getbuf(s))
def copy(self):
new = sha384.__new__(sha384)
diff --git a/lib_pypy/_sha256.py b/lib_pypy/_sha256.py
--- a/lib_pypy/_sha256.py
+++ b/lib_pypy/_sha256.py
@@ -131,6 +131,14 @@
sha_info['digestsize'] = 28
return sha_info
+def getbuf(s):
+ if isinstance(s, str):
+ return s
+ elif isinstance(s, unicode):
+ return str(s)
+ else:
+ return buffer(s)
+
def sha_update(sha_info, buffer):
count = len(buffer)
buffer_idx = 0
@@ -211,10 +219,10 @@
def __init__(self, s=None):
self._sha = sha_init()
if s:
- sha_update(self._sha, s)
+ sha_update(self._sha, getbuf(s))
def update(self, s):
- sha_update(self._sha, s)
+ sha_update(self._sha, getbuf(s))
def digest(self):
return sha_final(self._sha.copy())[:self._sha['digestsize']]
@@ -233,7 +241,7 @@
def __init__(self, s=None):
self._sha = sha224_init()
if s:
- sha_update(self._sha, s)
+ sha_update(self._sha, getbuf(s))
def copy(self):
new = sha224.__new__(sha224)
More information about the Pypy-commit
mailing list