Author: David Schneider <david.schnei...@picle.org>
Branch: 
Changeset: r43:5d3d11f87944
Date: 2011-09-22 13:20 +0200
http://bitbucket.org/pypy/lang-io/changeset/5d3d11f87944/

Log:    extend if method to support any object as condition

diff --git a/io/object.py b/io/object.py
--- a/io/object.py
+++ b/io/object.py
@@ -151,16 +151,15 @@
 
 @register_method('Object', 'if')
 def object_if(space, w_target, w_message, w_context):
-    w_condition = w_message.arguments[0].eval(space, w_context, w_context)
-
-    if w_condition is space.w_true:
+    w_condition = w_message.arguments[0].eval(space, w_target, w_context)
+    if space.istrue(w_condition):
         index = 1
     else:
         index = 2
 
     if index < len(w_message.arguments):
         return w_message.arguments[index].eval(space, w_context, w_context)
-    return w_condition
+    return space.newbool(index == 1)
 
 @register_method('Object', 'stopStatus')
 def object_stopstatus(space, w_target, w_message, w_context):
diff --git a/io/objspace.py b/io/objspace.py
--- a/io/objspace.py
+++ b/io/objspace.py
@@ -204,8 +204,14 @@
             return self.w_true
         return self.w_false
 
+    def istrue(self, value):
+        if value is not self.w_false and value is not self.w_nil:
+            return True
+        return False
+
     def newsequence(self, value):
         return self.w_immutable_sequence.clone_and_init(value)
+
     def isnil(self, w_object):
         return w_object is self.w_nil
 
diff --git a/io/test/test_object.py b/io/test/test_object.py
--- a/io/test/test_object.py
+++ b/io/test/test_object.py
@@ -197,6 +197,15 @@
     res, space = interpret(inp)
     assert res is space.w_false
 
+def test_if_object():
+    inp = 'if(Object)'
+    res, space = interpret(inp)
+    assert res is space.w_true
+
+    inp = 'if(nil)'
+    res, space = interpret(inp)
+    assert res is space.w_false
+
 def test_object_stopStatus():
     inp = 'stopStatus'
     res, space = interpret(inp)
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to