[pypy-svn] r58674 - in pypy/branch/oo-jit/pypy/translator/cli: . src test

antocuni at codespeak.net antocuni at codespeak.net
Mon Oct 6 17:55:14 CEST 2008


Author: antocuni
Date: Mon Oct  6 17:55:14 2008
New Revision: 58674

Modified:
   pypy/branch/oo-jit/pypy/translator/cli/dotnet.py
   pypy/branch/oo-jit/pypy/translator/cli/src/pypylib.cs
   pypy/branch/oo-jit/pypy/translator/cli/test/test_dotnet.py
Log:
implement two helpers to convert between ootype.Class and System.Type and viceversa



Modified: pypy/branch/oo-jit/pypy/translator/cli/dotnet.py
==============================================================================
--- pypy/branch/oo-jit/pypy/translator/cli/dotnet.py	(original)
+++ pypy/branch/oo-jit/pypy/translator/cli/dotnet.py	Mon Oct  6 17:55:14 2008
@@ -723,3 +723,16 @@
     }
 
 known_delegates_class = {}
+
+cVoid = classof(CLR.System.Void)
+def class2type(cls):
+    'Cast a PBC of type ootype.Class into a System.Type instance'
+    if cls is cVoid:
+        return None
+    return clidowncast(box(cls), CLR.System.Type)
+
+def type2class(clitype):
+    'Cast a System.Type instance to a PBC of type ootype.Class'
+##     if clitype is None:
+##         return cVoid
+    return unbox(clitype, ootype.Class)

Modified: pypy/branch/oo-jit/pypy/translator/cli/src/pypylib.cs
==============================================================================
--- pypy/branch/oo-jit/pypy/translator/cli/src/pypylib.cs	(original)
+++ pypy/branch/oo-jit/pypy/translator/cli/src/pypylib.cs	Mon Oct  6 17:55:14 2008
@@ -283,6 +283,9 @@
 
         public static string OOString(object obj, int base_)
         {
+          if (obj == null)
+            return "<null object>";
+          else
             return string.Format("<{0} object>", obj.GetType().FullName);
         }
 

Modified: pypy/branch/oo-jit/pypy/translator/cli/test/test_dotnet.py
==============================================================================
--- pypy/branch/oo-jit/pypy/translator/cli/test/test_dotnet.py	(original)
+++ pypy/branch/oo-jit/pypy/translator/cli/test/test_dotnet.py	Mon Oct  6 17:55:14 2008
@@ -8,7 +8,8 @@
 from pypy.translator.cli.dotnet import SomeCliClass, SomeCliStaticMethod,\
      NativeInstance, CLR, box, unbox, OverloadingResolver, NativeException,\
      native_exc, new_array, init_array, typeof, eventhandler, clidowncast,\
-     cliupcast, classof, cast_to_native_object, cast_from_native_object
+     cliupcast, classof, cast_to_native_object, cast_from_native_object,\
+     class2type, type2class
 
 System = CLR.System
 ArrayList = CLR.System.Collections.ArrayList
@@ -483,6 +484,31 @@
         res = self.interpret(fn, [True])
         assert res == 42
 
+    def test_class2type(self):
+        cInt32 = classof(System.Int32)
+        cString = classof(System.String)
+        def fn(flag):
+            if flag:
+                cls = cInt32
+            else:
+                cls = cString
+            clitype = class2type(cls)
+            return clitype.get_FullName()
+        res = self.interpret(fn, [True])
+        assert res == 'System.Int32'
+
+    def test_type2class(self):
+        cInt32 = classof(System.Int32)
+        def fn(flag):
+            if flag:
+                clitype = typeof(System.Int32)
+            else:
+                clitype = typeof(System.String)
+            cls = type2class(clitype)
+            return cls is cInt32
+        res = self.interpret(fn, [True])
+        assert res
+
     def test_instance_wrapping(self):
         class Foo:
             pass
@@ -695,3 +721,6 @@
 
     def test_cast_native_object(self):
         pass # it works only when translated
+
+    def test_type2class(self):
+        pass # it works only when translated



More information about the Pypy-commit mailing list