[pypy-commit] pypy default: add a helper to lookup special methods

gutworth noreply at buildbot.pypy.org
Tue May 24 00:54:25 CEST 2011


Author: Benjamin Peterson <benjamin at python.org>
Branch: 
Changeset: r44387:969ca8852422
Date: 2011-05-23 17:37 -0500
http://bitbucket.org/pypy/pypy/changeset/969ca8852422/

Log:	add a helper to lookup special methods

diff --git a/pypy/module/__pypy__/__init__.py b/pypy/module/__pypy__/__init__.py
--- a/pypy/module/__pypy__/__init__.py
+++ b/pypy/module/__pypy__/__init__.py
@@ -16,6 +16,7 @@
         'debug_stop'                : 'interp_debug.debug_stop',
         'debug_print_once'          : 'interp_debug.debug_print_once',
         'builtinify'                : 'interp_magic.builtinify',
+        'lookup_special'            : 'interp_magic.lookup_special',
     }
 
     def setup_after_space_initialization(self):
diff --git a/pypy/module/__pypy__/interp_magic.py b/pypy/module/__pypy__/interp_magic.py
--- a/pypy/module/__pypy__/interp_magic.py
+++ b/pypy/module/__pypy__/interp_magic.py
@@ -1,3 +1,4 @@
+from pypy.interpreter.baseobjspace import ObjSpace, W_Root
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.gateway import unwrap_spec
 from pypy.rlib.objectmodel import we_are_translated
@@ -59,3 +60,11 @@
     func = space.interp_w(Function, w_func)
     bltn = BuiltinFunction(func)
     return space.wrap(bltn)
+
+ at unwrap_spec(ObjSpace, W_Root, str)
+def lookup_special(space, w_obj, meth):
+    """Lookup up a special method on an object."""
+    w_descr = space.lookup(w_obj, meth)
+    if w_descr is None:
+        return space.w_None
+    return space.get(w_descr, w_obj)
diff --git a/pypy/module/__pypy__/test/test_special.py b/pypy/module/__pypy__/test/test_special.py
--- a/pypy/module/__pypy__/test/test_special.py
+++ b/pypy/module/__pypy__/test/test_special.py
@@ -36,3 +36,11 @@
         assert not hasattr(A.b, 'im_func')
         assert A.a is not A.__dict__['a']
         assert A.b is A.__dict__['b']
+
+    def test_lookup_special(self):
+        from __pypy__ import lookup_special
+        class X(object):
+            def foo(self): return 42
+        x = X()
+        x.foo = 23
+        assert lookup_special(x, "foo")() == 42


More information about the pypy-commit mailing list