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

afa at codespeak.net afa at codespeak.net
Sat Oct 30 20:32:02 CEST 2010


Author: afa
Date: Sat Oct 30 20:32:01 2010
New Revision: 78567

Modified:
   pypy/branch/fast-forward/pypy/module/_hashlib/interp_hashlib.py
   pypy/branch/fast-forward/pypy/module/_hashlib/test/test_hashlib.py
Log:
Note to self: a function definition in a loop is probably an error!
all functions capture the same variale, and so get the
last value of the list!


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	Sat Oct 30 20:32:01 2010
@@ -124,10 +124,13 @@
     return space.wrap(w_hash)
 
 # shortcut functions
-for name in algorithms:
-    newname = 'new_%s' % (name,)
+def make_new_hash(name):
     @func_renamer(newname)
     @unwrap_spec(ObjSpace, str)
     def new_hash(space, string=''):
         return new(space, name, string)
-    globals()[newname] = new_hash
+    return new_hash
+
+for name in algorithms:
+    newname = 'new_%s' % (name,)
+    globals()[newname] = make_new_hash(name)

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	Sat Oct 30 20:32:01 2010
@@ -57,6 +57,10 @@
             assert c_digest    == digest
             assert c_hexdigest == hexdigest
 
+    def test_shortcut(self):
+        import hashlib
+        assert repr(hashlib.md5()).startswith("<md5 HASH object")
+
     def test_unicode(self):
         import _hashlib
         assert _hashlib.new('sha1', u'xxx').__class__.__name__ == 'HASH'



More information about the Pypy-commit mailing list