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

arigo at codespeak.net arigo at codespeak.net
Fri May 5 22:09:15 CEST 2006


Author: arigo
Date: Fri May  5 22:09:13 2006
New Revision: 26834

Modified:
   pypy/dist/pypy/rpython/lltypesystem/rstr.py
   pypy/dist/pypy/rpython/lltypesystem/rtagged.py
   pypy/dist/pypy/rpython/lltypesystem/test/test_rtagged.py
   pypy/dist/pypy/rpython/objectmodel.py
Log:
Finished rtagged.


Modified: pypy/dist/pypy/rpython/lltypesystem/rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/rstr.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/rstr.py	Fri May  5 22:09:13 2006
@@ -177,6 +177,9 @@
 instance_str_prefix = string_repr.convert_const("<")
 instance_str_suffix = string_repr.convert_const(" object>")
 
+unboxed_instance_str_prefix = string_repr.convert_const("<unboxed ")
+unboxed_instance_str_suffix = string_repr.convert_const(">")
+
 list_str_open_bracket = string_repr.convert_const("[")
 list_str_close_bracket = string_repr.convert_const("]")
 list_str_sep = string_repr.convert_const(", ")

Modified: pypy/dist/pypy/rpython/lltypesystem/rtagged.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/rtagged.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/rtagged.py	Fri May  5 22:09:13 2006
@@ -43,7 +43,11 @@
         return v_instance, False   # don't call __init__
 
     def convert_const(self, value):
-        raise NotImplementedError
+        if value is None:
+            return self.null_instance()
+        else:
+            number = value.getvalue()
+            return ll_int_to_unboxed(self.lowleveltype, number)
 
     def getfield(self, vinst, attr, llops, force_cast=False):
         if attr != '__class__':
@@ -58,13 +62,22 @@
             return cunboxedcls
 
     def rtype_type(self, hop):
-        raise NotImplementedError
+        [vinst] = hop.inputargs(self)
+        return self.getfield(vinst, '__class__', hop.llops)
 
     def rtype_setattr(self, hop):
-        raise NotImplementedError
+        raise TyperError("cannot set attributes on %r" % (self,))
 
     def ll_str(self, i):
-        raise NotImplementedError
+        if lltype.cast_ptr_to_int(i) & 1:
+            from pypy.rpython.lltypesystem import rstr
+            from pypy.rpython.rint import signed_repr
+            llstr1 = signed_repr.ll_str(ll_unboxed_to_int(i))
+            return rstr.ll_strconcat(rstr.unboxed_instance_str_prefix,
+                      rstr.ll_strconcat(llstr1,
+                                        rstr.unboxed_instance_str_suffix))
+        else:
+            return InstanceRepr.ll_str(self, i)
 
     def rtype_isinstance(self, hop):
         if not hop.args_s[1].is_constant():
@@ -84,6 +97,12 @@
                                  minid, maxid, c_answer_if_unboxed)
 
 
+def ll_int_to_unboxed(PTRTYPE, value):
+    return lltype.cast_int_to_ptr(PTRTYPE, value*2+1)
+
+def ll_unboxed_to_int(p):
+    return lltype.cast_ptr_to_int(p) >> 1
+
 def ll_unboxed_getclass(instance, class_if_unboxed):
     if lltype.cast_ptr_to_int(instance) & 1:
         return class_if_unboxed

Modified: pypy/dist/pypy/rpython/lltypesystem/test/test_rtagged.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/test/test_rtagged.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/test/test_rtagged.py	Fri May  5 22:09:13 2006
@@ -5,15 +5,24 @@
 
 
 class A(object):
-    pass
+    def meth(self, x):
+        raise NotImplementedError
+
 class B(A):
     def __init__(self, normalint):
         self.normalint = normalint
+    def meth(self, x):
+        return self.normalint + x + 2
+
 class C(A, UnboxedValue):
-    pass
+    def meth(self, x):
+        return self.getvalue() + x + 3
+
+# ____________________________________________________________
 
 def test_on_top_of_cpython():
     assert C(17).getvalue() == 17
+    assert C(20).meth(10) == 33
 
 def test_instantiate():
     def fn1(n):
@@ -51,3 +60,72 @@
     res = interpret(fn2, [sys.maxint])
     assert res.item0 == 'B'
     assert res.item1 == sys.maxint
+
+def test_prebuilt():
+    c = C(111)
+    def fn(n):
+        if n < 0:
+            x = c
+        else:
+            x = C(n)
+        return x.getvalue()
+
+    res = interpret(fn, [12])
+    assert res == 12
+    res = interpret(fn, [-1])
+    assert res == 111
+
+def test_C_or_None():
+    def g(x):
+        if x is None:
+            return sys.maxint
+        else:
+            return x.getvalue()
+    def fn(n):
+        if n < 0:
+            x = None
+        else:
+            x = C(n)
+        return g(x)
+
+    res = interpret(fn, [-1])
+    assert res == sys.maxint
+    res = interpret(fn, [56])
+    assert res == 56
+
+def test_type():
+    def fn(n):
+        if n < 0:
+            x = B(n)
+        else:
+            x = C(n)
+        return type(x) is B, type(x) is C
+
+    res = interpret(fn, [-212])
+    assert res.item0 and not res.item1
+    res = interpret(fn, [9874])
+    assert res.item1 and not res.item0
+
+def test_str():
+    def fn(n):
+        if n > 0:
+            x = B(n)
+        else:
+            x = C(n)
+        return str(x)
+    res = interpret(fn, [-832])
+    assert ''.join(res.chars) == '<unboxed -832>'
+    res = interpret(fn, [1])
+    assert ''.join(res.chars) == '<B object>'
+
+def test_method():
+    def fn(n):
+        if n > 0:
+            x = B(n)
+        else:
+            x = C(n)
+        return x.meth(100)
+    res = interpret(fn, [1000])
+    assert res == 1102
+    res = interpret(fn, [-1000])
+    assert res == -897

Modified: pypy/dist/pypy/rpython/objectmodel.py
==============================================================================
--- pypy/dist/pypy/rpython/objectmodel.py	(original)
+++ pypy/dist/pypy/rpython/objectmodel.py	Fri May  5 22:09:13 2006
@@ -128,6 +128,9 @@
     def __init__(self, value):
         pass
 
+    def __repr__(self):
+        return '<unboxed %d>' % (self.getvalue(),)
+
     def getvalue(self):
         return getvalue_from_unboxed(self)
 



More information about the Pypy-commit mailing list