[pypy-svn] r75041 - in pypy/trunk/pypy/tool: . test

afa at codespeak.net afa at codespeak.net
Thu Jun 3 10:47:07 CEST 2010


Author: afa
Date: Thu Jun  3 10:47:06 2010
New Revision: 75041

Added:
   pypy/trunk/pypy/tool/test/test_sourcetools.py   (contents, props changed)
Modified:
   pypy/trunk/pypy/tool/sourcetools.py
Log:
A decorator to rename a function


Modified: pypy/trunk/pypy/tool/sourcetools.py
==============================================================================
--- pypy/trunk/pypy/tool/sourcetools.py	(original)
+++ pypy/trunk/pypy/tool/sourcetools.py	Thu Jun  3 10:47:06 2010
@@ -227,6 +227,12 @@
         f.func_dict.update(func.func_dict)
     return f
 
+def func_renamer(newname):
+    """A function decorator which changes the name of a function."""
+    def decorate(func):
+        return func_with_new_name(func, newname)
+    return decorate
+
 PY_IDENTIFIER = ''.join([(('0' <= chr(i) <= '9' or
                            'a' <= chr(i) <= 'z' or
                            'A' <= chr(i) <= 'Z') and chr(i) or '_')

Added: pypy/trunk/pypy/tool/test/test_sourcetools.py
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/tool/test/test_sourcetools.py	Thu Jun  3 10:47:06 2010
@@ -0,0 +1,24 @@
+from pypy.tool.sourcetools import func_with_new_name, func_renamer
+
+def test_rename():
+    def f(x, y=5):
+        return x + y
+    f.prop = int
+
+    g = func_with_new_name(f, "g")
+    assert g(4, 5) == 9
+    assert g.func_name == "g"
+    assert f.func_defaults == (5,)
+    assert g.prop is int
+
+def test_rename_decorator():
+    @func_renamer("g")
+    def f(x, y=5):
+        return x + y
+    f.prop = int
+
+    assert f(4, 5) == 9
+
+    assert f.func_name == "g"
+    assert f.func_defaults == (5,)
+    assert f.prop is int



More information about the Pypy-commit mailing list