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

arigo at codespeak.net arigo at codespeak.net
Mon Jul 11 14:17:55 CEST 2005


Author: arigo
Date: Mon Jul 11 14:17:52 2005
New Revision: 14500

Modified:
   pypy/dist/pypy/rpython/rlist.py
   pypy/dist/pypy/rpython/test/test_rlist.py
Log:
Allow lists to be None (null) in the rtyper.


Modified: pypy/dist/pypy/rpython/rlist.py
==============================================================================
--- pypy/dist/pypy/rpython/rlist.py	(original)
+++ pypy/dist/pypy/rpython/rlist.py	Mon Jul 11 14:17:52 2005
@@ -8,7 +8,7 @@
 from pypy.rpython.rclass import InstanceRepr
 from pypy.rpython.lltype import GcForwardReference, Ptr, GcArray, GcStruct
 from pypy.rpython.lltype import Void, Signed, malloc, typeOf, Primitive
-from pypy.rpython.lltype import Bool
+from pypy.rpython.lltype import Bool, nullptr
 from pypy.rpython import rstr
 
 # ____________________________________________________________
@@ -62,6 +62,8 @@
     def convert_const(self, listobj):
         # get object from bound list method
         #listobj = getattr(listobj, '__self__', listobj)
+        if listobj is None:
+            return nullptr(self.LIST)
         if not isinstance(listobj, list):
             raise TyperError("expected a list: %r" % (listobj,))
         try:
@@ -89,6 +91,10 @@
         v_lst, = hop.inputargs(self)
         return hop.gendirectcall(ll_len, v_lst)
 
+    def rtype_is_true(self, hop):
+        v_lst, = hop.inputargs(self)
+        return hop.gendirectcall(ll_list_is_true, v_lst)
+
     def rtype_method_append(self, hop):
         v_lst, v_value = hop.inputargs(self, self.item_repr)
         hop.gendirectcall(ll_append, v_lst, v_value)
@@ -259,6 +265,10 @@
 def ll_len(l):
     return len(l.items)
 
+def ll_list_is_true(l):
+    # check if a list is True, allowing for None
+    return bool(l) and len(l.items) != 0
+
 def ll_append(l, newitem):
     length = len(l.items)
     newitems = malloc(typeOf(l).TO.items.TO, length+1)

Modified: pypy/dist/pypy/rpython/test/test_rlist.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rlist.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rlist.py	Mon Jul 11 14:17:52 2005
@@ -315,3 +315,20 @@
     
     res = interpret(fn, [])
     assert ''.join(res.chars) == fn()
+
+def test_list_or_None():
+    empty_list = []
+    nonempty_list = [1, 2]
+    def fn(i):
+        test = [None, empty_list, nonempty_list][i]
+        if test:
+            return 1
+        else:
+            return 0
+
+    res = interpret(fn, [0])
+    assert res == 0
+    res = interpret(fn, [1])
+    assert res == 0
+    res = interpret(fn, [2])
+    assert res == 1



More information about the Pypy-commit mailing list