[pypy-svn] r18733 - in pypy/dist/pypy/rpython: . ootypesystem ootypesystem/test

arigo at codespeak.net arigo at codespeak.net
Tue Oct 18 12:14:15 CEST 2005


Author: arigo
Date: Tue Oct 18 12:14:14 2005
New Revision: 18733

Modified:
   pypy/dist/pypy/rpython/llinterp.py
   pypy/dist/pypy/rpython/ootypesystem/ootype.py
   pypy/dist/pypy/rpython/ootypesystem/rclass.py
   pypy/dist/pypy/rpython/ootypesystem/test/test_ooclean.py
Log:
ootyper: boolean checks on oo instances returns True/False
for non-null/null instances.



Modified: pypy/dist/pypy/rpython/llinterp.py
==============================================================================
--- pypy/dist/pypy/rpython/llinterp.py	(original)
+++ pypy/dist/pypy/rpython/llinterp.py	Tue Oct 18 12:14:14 2005
@@ -652,6 +652,10 @@
     def op_oodowncast(self, INST, inst):
         return ootype.oodowncast(INST, inst)
 
+    def op_oononnull(self, inst):
+        assert isinstance(inst, ootype._instance)
+        return bool(inst)
+
 # by default we route all logging messages to nothingness
 # e.g. tests can then switch on logging to get more help
 # for failing tests

Modified: pypy/dist/pypy/rpython/ootypesystem/ootype.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/ootype.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/ootype.py	Tue Oct 18 12:14:14 2005
@@ -168,6 +168,9 @@
 
         self.__dict__[name] = value
 
+    def __nonzero__(self):
+        return True    # better be explicit -- overridden in _null_instance
+
 class _null_instance(_instance):
 
     def __init__(self, INSTANCE):
@@ -186,6 +189,9 @@
 
         raise RuntimeError("Assignment to field in null object")
 
+    def __nonzero__(self):
+        return False
+
 class _callable(object):
 
    def __init__(self, TYPE, **attrs):

Modified: pypy/dist/pypy/rpython/ootypesystem/rclass.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/rclass.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/rclass.py	Tue Oct 18 12:14:14 2005
@@ -173,6 +173,10 @@
         v_attr = hop.inputconst(ootype.Void, mangled)
         return hop.genop('oosetfield', [v_inst, v_attr, v_newval])
 
+    def rtype_is_true(self, hop):
+        vinst, = hop.inputargs(self)
+        return hop.genop('oononnull', [vinst], resulttype=ootype.Bool)
+
     def convert_const(self, value):
         if value is None:
             return null(self.lowleveltype)

Modified: pypy/dist/pypy/rpython/ootypesystem/test/test_ooclean.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/test/test_ooclean.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/test/test_ooclean.py	Tue Oct 18 12:14:14 2005
@@ -201,3 +201,14 @@
     res = interpret(dummyfn, [6], type_system='ootype')
     assert res == 42
 
+def test_null_instance():
+    def dummyfn(flag):
+        if flag:
+            x = EmptyBase()
+        else:
+            x = None
+        return not x
+    res = interpret(dummyfn, [True], type_system='ootype')
+    assert res is False
+    res = interpret(dummyfn, [False], type_system='ootype')
+    assert res is True



More information about the Pypy-commit mailing list