[pypy-svn] r51493 - in pypy/dist/pypy/translator/cli: . test

antocuni at codespeak.net antocuni at codespeak.net
Thu Feb 14 20:53:11 CET 2008


Author: antocuni
Date: Thu Feb 14 20:53:08 2008
New Revision: 51493

Modified:
   pypy/dist/pypy/translator/cli/dotnet.py
   pypy/dist/pypy/translator/cli/test/test_dotnet.py
Log:
add support to box/unbox lowlevel instances and records; mostly useful
to cast everything to System.Object and back



Modified: pypy/dist/pypy/translator/cli/dotnet.py
==============================================================================
--- pypy/dist/pypy/translator/cli/dotnet.py	(original)
+++ pypy/dist/pypy/translator/cli/dotnet.py	Thu Feb 14 20:53:08 2008
@@ -386,6 +386,12 @@
         else:
             return None
 
+    if isinstance(TYPE, ootype.OOType) and TYPE is not ootype.String:
+        try:
+            return ootype.enforce(TYPE, x)
+        except TypeError:
+            return None
+
     # TODO: do the typechecking also in the other cases
 
     # this is a workaround against a pythonnet limitation: you can't
@@ -409,7 +415,7 @@
 
         hop.exception_cannot_occur()
         TYPE = v_obj.concretetype
-        if (TYPE is ootype.String or isinstance(TYPE, (ootype.Instance, ootype.BuiltinType, NativeInstance))):
+        if (TYPE is ootype.String or isinstance(TYPE, (ootype.OOType, NativeInstance))):
             return hop.genop('ooupcast', [v_obj], hop.r_result.lowleveltype)
         else:
             if TYPE not in BOXABLE_TYPES:
@@ -430,6 +436,8 @@
             # can_be_None == True because it can always return None, if it fails
             classdef = self.bookkeeper.getuniqueclassdef(TYPE)
             return SomeInstance(classdef, can_be_None=True)
+        elif isinstance(TYPE, ootype.OOType):
+            return SomeOOInstance(TYPE)
         else:
             assert TYPE in BOXABLE_TYPES
             return OverloadingResolver.lltype_to_annotation(TYPE)
@@ -437,7 +445,7 @@
     def specialize_call(self, hop):
         TYPE = hop.args_v[1].value
         v_obj = hop.inputarg(hop.args_r[0], arg=0)
-        if TYPE is ootype.String or isinstance(TYPE, (type, types.ClassType)):
+        if TYPE is ootype.String or isinstance(TYPE, (type, types.ClassType)) or isinstance(TYPE, ootype.OOType):
             return hop.genop('oodowncast', [v_obj], hop.r_result.lowleveltype)
         else:
             c_type = hop.inputconst(ootype.Void, TYPE)

Modified: pypy/dist/pypy/translator/cli/test/test_dotnet.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/test_dotnet.py	(original)
+++ pypy/dist/pypy/translator/cli/test/test_dotnet.py	Thu Feb 14 20:53:08 2008
@@ -431,6 +431,37 @@
         res = self.interpret(fn, [])
         assert res is None
 
+    def test_box_unbox_ooinstance(self):
+        A = ootype.Instance('A', ootype.ROOT, {'xx': ootype.Signed})
+        def fn(flag):
+            a = ootype.new(A)
+            a.xx = 42
+            b_obj = box(a)
+            a2 = unbox(b_obj, A)
+            return a2.xx
+        res = self.interpret(fn, [True])
+        assert res == 42
+
+    def test_box_unbox_ooinstance_fail(self):
+        A = ootype.Instance('A', ootype.ROOT, {'xx': ootype.Signed})
+        def fn(flag):
+            b_obj = System.Object()
+            a2 = unbox(b_obj, A)
+            return a2
+        res = self.interpret(fn, [True])
+        assert res is None
+
+    def test_box_unbox_oorecord(self):
+        A = ootype.Record({'xx': ootype.Signed})
+        def fn(flag):
+            a = ootype.new(A)
+            a.xx = 42
+            b_obj = box(a)
+            a2 = unbox(b_obj, A)
+            return a2.xx
+        res = self.interpret(fn, [True])
+        assert res == 42
+
     def test_instance_wrapping(self):
         class Foo:
             pass



More information about the Pypy-commit mailing list