[pypy-svn] r7323 - in pypy/trunk/src/pypy: objspace tool

mwh at codespeak.net mwh at codespeak.net
Wed Nov 17 15:09:58 CET 2004


Author: mwh
Date: Wed Nov 17 15:09:58 2004
New Revision: 7323

Added:
   pypy/trunk/src/pypy/tool/hack.py
Modified:
   pypy/trunk/src/pypy/objspace/descroperation.py
Log:
Add a hack to copy a function with a new name.
Use this to good effect in descroperation.
Only supported for 2.3 and later because new.function is REALLY 
DUMB in 2.2 and I can't be bothered to hack around it.


Modified: pypy/trunk/src/pypy/objspace/descroperation.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/descroperation.py	(original)
+++ pypy/trunk/src/pypy/objspace/descroperation.py	Wed Nov 17 15:09:58 2004
@@ -321,6 +321,8 @@
 
 # regular methods def helpers
 
+from pypy.tool.hack import func_with_new_name
+
 def _make_binop_impl(symbol, specialnames):
     left, right = specialnames
     def binop_impl(space, w_obj1, w_obj2):
@@ -343,7 +345,7 @@
             return w_res
         raise OperationError(space.w_TypeError,
                 space.wrap("unsupported operand type(s) for %s" % symbol))
-    return binop_impl
+    return func_with_new_name(binop_impl, "binop_%s_impl"%left.strip('_'))
 
 def _make_comparison_impl(symbol, specialnames):
     left, right = specialnames
@@ -374,7 +376,8 @@
         res = space.unwrap(w_res)
         return space.wrap(op(res, 0))
 
-    return comparison_impl
+    return func_with_new_name(comparison_impl, 'comparison_%s_impl'%left.strip('_'))
+
 
 def _make_inplace_impl(symbol, specialnames):
     specialname, = specialnames
@@ -391,7 +394,7 @@
         # XXX fix the error message we get here
         return getattr(space, noninplacespacemethod)(w_lhs, w_rhs)
 
-    return inplace_impl
+    return func_with_new_name(inplace_impl, 'inplace_%s_impl'%specialname.strip('_'))
 
 def _make_unaryop_impl(symbol, specialnames):
     specialname, = specialnames
@@ -401,7 +404,7 @@
             raise OperationError(space.w_TypeError,
                    space.wrap("operand does not support unary %s" % symbol))
         return space.get_and_call_function(w_impl, w_obj)
-    return unaryop_impl
+    return func_with_new_name(unaryop_impl, 'unaryop_%s_impl'%specialname.strip('_'))
     
 
 # add regular methods

Added: pypy/trunk/src/pypy/tool/hack.py
==============================================================================
--- (empty file)
+++ pypy/trunk/src/pypy/tool/hack.py	Wed Nov 17 15:09:58 2004
@@ -0,0 +1,20 @@
+import autopath
+
+import new, sys
+
+if sys.version_info > (2, 2):
+
+    def func_with_new_name(func, newname):
+        return new.function(func.func_code, func.func_globals,
+                            newname, func.func_defaults,
+                            func.func_closure)
+
+else:
+
+    def func_with_new_name(func, newname):
+        return func
+
+if __name__ == '__main__':
+    def f(): pass
+    g = func_with_new_name(f, 'g')
+    print g.__name__



More information about the Pypy-commit mailing list