[pypy-svn] r49681 - in pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Wed Dec 12 18:45:44 CET 2007


Author: cfbolz
Date: Wed Dec 12 18:45:43 2007
New Revision: 49681

Modified:
   pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__/interp_classobj.py
   pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__/test/test_classobj.py
Log:
index


Modified: pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__/interp_classobj.py
==============================================================================
--- pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__/interp_classobj.py	(original)
+++ pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__/interp_classobj.py	Wed Dec 12 18:45:43 2007
@@ -480,6 +480,14 @@
                 space.wrap("__hash__ must return int"))
         return w_ret
 
+    def descr_index(self, space):
+        w_func = self.getattr(space, space.wrap('__index__'), False)
+        if w_func is not None:
+            return space.call_function(w_func)
+        raise OperationError(
+            space.w_TypeError,
+            space.wrap("object cannot be interpreted as an index"))
+
 
 rawdict = {}
 
@@ -552,6 +560,8 @@
                          unwrap_spec=['self', ObjSpace, W_Root]),
     __hash__ = interp2app(W_InstanceObject.descr_hash,
                           unwrap_spec=['self', ObjSpace]),
+    __index__ = interp2app(W_InstanceObject.descr_index,
+                           unwrap_spec=['self', ObjSpace]),
     **rawdict
 )
 

Modified: pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__/test/test_classobj.py
==============================================================================
--- pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__/test/test_classobj.py	(original)
+++ pypy/branch/interplevel-oldstyle-classes/pypy/module/__builtin__/test/test_classobj.py	Wed Dec 12 18:45:43 2007
@@ -511,3 +511,14 @@
                 return 1
         a = A()
         raises(TypeError, hash, a)
+
+    def test_index(self):
+        class A:
+            __metaclass__ = nclassobj
+            def __index__(self):
+                return 1
+        l = [1, 2, 3]
+        assert l[A()] == 2
+        class A:
+            __metaclass__ = nclassobj
+        raises(TypeError, "l[A()]")



More information about the Pypy-commit mailing list