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

cfbolz at codespeak.net cfbolz at codespeak.net
Sun Dec 11 00:53:36 CET 2005


Author: cfbolz
Date: Sun Dec 11 00:53:35 2005
New Revision: 21014

Modified:
   pypy/dist/pypy/rpython/rbuiltin.py
   pypy/dist/pypy/rpython/test/test_objectmodel.py
Log:
rtyping support for these strange casts -- not clear to me yet how to support
them in the llinterpreter


Modified: pypy/dist/pypy/rpython/rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rpython/rbuiltin.py	(original)
+++ pypy/dist/pypy/rpython/rbuiltin.py	Sun Dec 11 00:53:35 2005
@@ -1,8 +1,8 @@
 from pypy.annotation.pairtype import pairtype
 from pypy.annotation import model as annmodel
 from pypy.objspace.flow.model import Constant
-from pypy.rpython.lltypesystem import lltype
-from pypy.rpython import rarithmetic, objectmodel, rstack
+from pypy.rpython.lltypesystem import lltype, rclass
+from pypy.rpython import rarithmetic, objectmodel, rstack, rint
 from pypy.rpython.error import TyperError
 from pypy.rpython.rmodel import Repr, IntegerRepr
 from pypy.rpython.rrange import rtype_builtin_range, rtype_builtin_xrange 
@@ -280,6 +280,18 @@
     return hop.genop('runtime_type_info', vlist,
                  resulttype = rptr.PtrRepr(lltype.Ptr(lltype.RuntimeTypeInfo)))
 
+def rtype_cast_object_to_int(hop):
+    assert isinstance(hop.args_r[0], rclass.InstanceRepr)
+    vlist = hop.inputargs(hop.args_r[0])
+    return hop.genop('cast_ptr_to_int', vlist,
+                 resulttype = rint.signed_repr)
+
+def rtype_cast_int_to_object(hop):
+    assert isinstance(hop.args_r[0], rint.IntegerRepr)
+    vlist = [hop.inputarg(rint.signed_repr, arg=0)]
+    return hop.genop('cast_int_to_ptr', vlist,
+                 resulttype = hop.r_result.lowleveltype)
+
 BUILTIN_TYPER[lltype.malloc] = rtype_malloc
 BUILTIN_TYPER[lltype.cast_pointer] = rtype_cast_pointer
 BUILTIN_TYPER[lltype.cast_ptr_to_int] = rtype_cast_ptr_to_int
@@ -292,6 +304,8 @@
 BUILTIN_TYPER[rarithmetic.r_longlong] = rtype_r_longlong
 BUILTIN_TYPER[objectmodel.r_dict] = rtype_r_dict
 BUILTIN_TYPER[objectmodel.we_are_translated] = rtype_we_are_translated
+BUILTIN_TYPER[objectmodel.cast_object_to_int] = rtype_cast_object_to_int
+BUILTIN_TYPER[objectmodel.cast_int_to_object] = rtype_cast_int_to_object
 BUILTIN_TYPER[rstack.yield_current_frame_to_caller] = (
     rtype_yield_current_frame_to_caller)
 

Modified: pypy/dist/pypy/rpython/test/test_objectmodel.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_objectmodel.py	(original)
+++ pypy/dist/pypy/rpython/test/test_objectmodel.py	Sun Dec 11 00:53:35 2005
@@ -11,7 +11,7 @@
     res = interpret(fn, [])
     assert res is True
 
-def test_casttoandfromint():
+def test_casttoandfromint_on_cpython():
     class A(object):
         pass
     class B(object):
@@ -27,6 +27,23 @@
     a = cast_int_to_object(i1, A)
     py.test.raises(RuntimeError, "a.b")
 
+def test_casttoandfromint():
+    class A(object):
+        pass
+    def f():
+        a = A()
+        return cast_object_to_int(a)
+    def g():
+        a = A()
+        i = cast_object_to_int(a)
+        return cast_object_to_int(cast_int_to_object(i, A)) == i
+    res = interpret(f, [])
+    # XXX humpf: there is no sane way to implement cast_ptr_to_int
+    # without going for the same hacks as in robjectmodel.cast_XXX_to_XXX
+    py.test.raises(AssertionError, interpret, g, [])
+    assert res > 0
+     
+    
 
 def strange_key_eq(key1, key2):
     return key1[0] == key2[0]   # only the 1st character is relevant



More information about the Pypy-commit mailing list