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

antocuni at codespeak.net antocuni at codespeak.net
Wed Nov 29 17:49:26 CET 2006


Author: antocuni
Date: Wed Nov 29 17:49:25 2006
New Revision: 35131

Modified:
   pypy/dist/pypy/translator/cli/dotnet.py
   pypy/dist/pypy/translator/cli/test/test_dotnet.py
Log:
Make box really return real boxed objects when run on top of
pythonnet. This is necessary for e.g. calling a method on them.



Modified: pypy/dist/pypy/translator/cli/dotnet.py
==============================================================================
--- pypy/dist/pypy/translator/cli/dotnet.py	(original)
+++ pypy/dist/pypy/translator/cli/dotnet.py	Wed Nov 29 17:49:25 2006
@@ -271,17 +271,42 @@
 
 CLR = CliNamespace(None)
 
-
 BOXABLE_TYPES = [ootype.Signed, ootype.Unsigned, ootype.SignedLongLong,
                  ootype.UnsignedLongLong, ootype.Bool, ootype.Float,
                  ootype.Char, ootype.String]
 
 def box(x):
-    return x
+    t = type(x)
+    if t is int:
+        return CLR.System.Int32(x)
+    elif t is r_uint:
+        return CLR.System.UInt32(x)
+    elif t is r_longlong:
+        return CLR.System.Int64(x)
+    elif t is r_ulonglong:
+        return CLR.System.UInt64(x)
+    elif t is bool:
+        return CLR.System.Boolean(x)
+    elif t is float:
+        return CLR.System.Double(x)
+    elif t is str:
+        if len(x) == 1:
+            return CLR.System.Char(x)
+        else:
+            return CLR.System.String(x)
+    else:
+        assert False
 
 def unbox(x, TYPE):
     # TODO: check that x is really of type TYPE
-    return x
+    
+    # this is a workaround against a pythonnet limitation: you can't
+    # directly get the, e.g., python int from the System.Int32 object:
+    # a simple way to do this is to put it into an ArrayList and
+    # retrieve the value.
+    tmp = PythonNet.System.Collections.ArrayList()
+    tmp.Add(x)
+    return tmp[0]
 
 
 class Entry(ExtRegistryEntry):
@@ -319,7 +344,6 @@
             return hop.genop('cliunbox', [v_obj, v_type], hop.r_result.lowleveltype)
 
 
-
 native_exc_cache = {}
 def NativeException(cliClass):
     try:

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	Wed Nov 29 17:49:25 2006
@@ -168,6 +168,14 @@
             return unbox(x.get_Item(0), ootype.String)
         assert self.interpret(fn, []) == 'foo'
 
+    def test_box_method(self):
+        def fn():
+            x = box(42)
+            t = x.GetType()
+            return t.get_Name()
+        res = self.interpret(fn, [])
+        assert res == 'Int32'
+
     def test_exception(self):
         py.test.skip("It doesn't work so far")
         def fn():
@@ -198,7 +206,7 @@
 
     def test_init_array(self):
         def fn():
-            x = init_array([box(42), box(43)])
+            x = init_array(System.Object, box(42), box(43))
             return unbox(x[0], ootype.Signed) + unbox(x[1], ootype.Signed)
         assert self.interpret(fn, []) == 42+43
 



More information about the Pypy-commit mailing list