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

antocuni at codespeak.net antocuni at codespeak.net
Fri Nov 17 13:48:11 CET 2006


Author: antocuni
Date: Fri Nov 17 13:48:04 2006
New Revision: 34706

Modified:
   pypy/dist/pypy/translator/cli/dotnet.py
   pypy/dist/pypy/translator/cli/test/test_dotnet.py
Log:
- Support for creating native arrays of a known length.

- Support for setting items on arrays.



Modified: pypy/dist/pypy/translator/cli/dotnet.py
==============================================================================
--- pypy/dist/pypy/translator/cli/dotnet.py	(original)
+++ pypy/dist/pypy/translator/cli/dotnet.py	Fri Nov 17 13:48:04 2006
@@ -50,7 +50,15 @@
         if ooinst.ootype._isArray:
             return SomeOOInstance(ooinst.ootype._ELEMENT)
         return s_ImpossibleValue
-    
+
+    def setitem((ooinst, index), s_value):
+        if ooinst.ootype._isArray:
+            ELEMENT = ooinst.ootype._ELEMENT
+            VALUE = s_value.ootype
+            assert ootype.isSubclass(VALUE, ELEMENT)
+            return s_None
+        return s_ImpossibleValue
+
 
 ## Rtyper model
 
@@ -100,6 +108,13 @@
         hop.exception_is_here()
         return hop.genop('cli_getelem', [v_array, v_index], hop.r_result.lowleveltype)
 
+    def rtype_setitem((r_inst, r_int), hop):
+        if not r_inst.lowleveltype._isArray:
+            raise TyperError("setitem() on a non-array instance")        
+        vlist = hop.inputargs(*hop.args_r)
+        hop.exception_is_here()
+        return hop.genop('cli_setelem', vlist, hop.r_result.lowleveltype)
+
 
 ## OOType model
 
@@ -344,12 +359,32 @@
         v_obj, = hop.inputargs(*hop.args_r)
         return hop.genop('same_as', [v_obj], hop.r_result.lowleveltype)
 
+def new_array(type, length):
+    return [None] * length
 
 def init_array(type, *args):
     # PythonNet doesn't provide a straightforward way to create arrays... fake it with a list
     return args
 
 class Entry(ExtRegistryEntry):
+    _about_ = new_array
+
+    def compute_result_annotation(self, type_s, length_s):
+        from pypy.translator.cli.query import load_class_maybe
+        assert type_s.is_constant()
+        assert isinstance(length_s, SomeInteger)
+        TYPE = type_s.const._INSTANCE
+        fullname = '%s.%s[]' % (TYPE._namespace, TYPE._classname)
+        cliArray = load_class_maybe(fullname)
+        return SomeOOInstance(cliArray._INSTANCE)
+
+    def specialize_call(self, hop):
+        c_type, v_length = hop.inputargs(*hop.args_r)
+        hop.exception_cannot_occur()
+        return hop.genop('cli_newarray', [c_type, v_length], hop.r_result.lowleveltype)
+
+
+class Entry(ExtRegistryEntry):
     _about_ = init_array
 
     def compute_result_annotation(self, type_s, *args_s):

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	Fri Nov 17 13:48:04 2006
@@ -7,7 +7,7 @@
 from pypy.translator.cli.test.runtest import CliTest
 from pypy.translator.cli.dotnet import SomeCliClass, SomeCliStaticMethod,\
      NativeInstance, CLR, box, unbox, OverloadingResolver, NativeException,\
-     native_exc, init_array
+     native_exc, new_array, init_array
 
 System = CLR.System
 Math = CLR.System.Math
@@ -111,7 +111,7 @@
         a = RPythonAnnotator()
         s = a.build_types(fn, [])
         assert isinstance(s, annmodel.SomeOOInstance)
-        assert s.ootype._name == '[mscorlib]System.Object'
+        assert s.ootype._name == '[mscorlib]System.Object'            
 
 class TestDotnetRtyping(CliTest):
     def _skip_pythonnet(self, msg):
@@ -188,10 +188,18 @@
             return unbox(array[0], ootype.Signed)
         assert self.interpret(fn, []) == 42
 
+    def test_new_array(self):
+        def fn():
+            x = new_array(System.Object, 2)
+            x[0] = box(42)
+            x[1] = box(43)
+            return unbox(x[0], ootype.Signed) + unbox(x[1], ootype.Signed)
+        assert self.interpret(fn, []) == 42+43
+
     def test_init_array(self):
         def fn():
-            array = init_array(System.Object, box(42), box(43))
-            return unbox(array[0], ootype.Signed) + unbox(array[1], ootype.Signed)
+            x = init_array([box(42), box(43)])
+            return unbox(x[0], ootype.Signed) + unbox(x[1], ootype.Signed)
         assert self.interpret(fn, []) == 42+43
 
     def test_null(self):
@@ -241,7 +249,6 @@
         res = self.ll_to_string(self.interpret(fn, []))
         assert res.startswith("Index is less than 0")
 
-
 class TestPythonnet(TestDotnetRtyping):
     # don't interpreter functions but execute them directly through pythonnet
     def interpret(self, f, args):



More information about the Pypy-commit mailing list