[pypy-svn] r50949 - in pypy/dist/pypy/objspace: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Wed Jan 23 23:53:22 CET 2008


Author: cfbolz
Date: Wed Jan 23 23:53:20 2008
New Revision: 50949

Modified:
   pypy/dist/pypy/objspace/reflective.py
   pypy/dist/pypy/objspace/test/test_reflective.py
Log:
allow reflective space to handle is_true.


Modified: pypy/dist/pypy/objspace/reflective.py
==============================================================================
--- pypy/dist/pypy/objspace/reflective.py	(original)
+++ pypy/dist/pypy/objspace/reflective.py	Wed Jan 23 23:53:20 2008
@@ -36,6 +36,8 @@
     for opname, _, args, _ in ObjSpace.MethodTable:
         if opname == name:
             return args
+    if opname == "is_true":
+        return 1
 
 def make_space_access_method(name, wrappedfn, parentfn):
     if name.startswith("new") or name.endswith("_w"):
@@ -50,6 +52,17 @@
             finally:
                 reset_reflective_space(space, w_old_reflectivespace)
         unwrap_spec = ['self', ObjSpace, W_Root, argument.Arguments]
+    elif name == "is_true":
+        def func(self, space, w_obj):
+            w_old_reflectivespace = get_reflective_space(space)
+            set_reflectivespace(space, self.w_reflectivespace)
+            try:
+                if parentfn(w_obj):
+                    return space.w_True
+                return space.w_False
+            finally:
+                reset_reflective_space(space, w_old_reflectivespace)
+        unwrap_spec = ['self', ObjSpace, W_Root]
     else:
         args = get_spaceop_args(name)
         if args == 1:
@@ -90,8 +103,8 @@
     'wrap',
     'interpclass_w',
     'unwrap',
-    'is_true',
     'marshal_w',
+    'nonzero', # maps to is_true anyway
     ]
 
 
@@ -175,6 +188,17 @@
                         space.wrap("space.type must return a type object!"))
                 return w_obj
             return parentfn(*args_w)
+    elif opname == "is_true":
+        def fn(w_obj):
+            w_newobj = user_hook(w_obj)
+            if w_newobj is not None:
+                if w_newobj is space.w_True:
+                    return True
+                elif w_newobj is space.w_False:
+                    return False
+                raise OperationError(space.w_TypeError,
+                                     space.wrap("is_true must return True or False"))
+            return parentfn(w_obj)
     else:
         def fn(*args_w):
             w_obj = user_hook(*args_w)

Modified: pypy/dist/pypy/objspace/test/test_reflective.py
==============================================================================
--- pypy/dist/pypy/objspace/test/test_reflective.py	(original)
+++ pypy/dist/pypy/objspace/test/test_reflective.py	Wed Jan 23 23:53:20 2008
@@ -134,6 +134,25 @@
         raises(AttributeError, "a.x = 2")
         set_reflectivespace(None)
 
+    def test_is_true(self):
+        from __pypy__ import set_reflectivespace
+        class Space:
+            def is_true(self, space, obj):
+                print "is_true", obj
+                if type(obj) == int:
+                    # confusity
+                    return bool(obj % 13)
+                return space.is_true(bool)
+        set_reflectivespace(Space())
+        bool(13)
+        if 13:
+            assert False, "should not get here"
+        if "abc":
+            pass
+        else:
+            assert False, "should not get here"
+        set_reflectivespace(None)
+        
 
     def test_autocurry(self):
         # rather simplified for now



More information about the Pypy-commit mailing list