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

arigo at codespeak.net arigo at codespeak.net
Fri Apr 21 14:28:37 CEST 2006


Author: arigo
Date: Fri Apr 21 14:28:36 2006
New Revision: 26097

Modified:
   pypy/dist/pypy/rpython/rctypes/aarray.py
   pypy/dist/pypy/rpython/rctypes/rarray.py
   pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py
   pypy/dist/pypy/rpython/rctypes/test/test_rarray.py
Log:
rctypes: support for array(..initial values..).  Note that it's of
limited usefulness because the annotator doesn't support *args where
'args' is a list.



Modified: pypy/dist/pypy/rpython/rctypes/aarray.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/aarray.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/aarray.py	Fri Apr 21 14:28:36 2006
@@ -6,8 +6,18 @@
 ArrayType = type(ARRAY(c_int, 10))
 
 def arraytype_specialize_call(hop):
+    from pypy.rpython.error import TyperError
+    from pypy.rpython.rmodel import inputconst
     r_array = hop.r_result
-    return r_array.allocate_instance(hop.llops)
+    v_result = r_array.allocate_instance(hop.llops)
+    if hop.nb_args > r_array.length:
+        raise TyperError("too many arguments for an array of length %d" % (
+            r_array.length,))
+    for i in range(hop.nb_args):
+        v_item = hop.inputarg(r_array.r_item, arg=i)
+        c_index = inputconst(lltype.Signed, i)
+        r_array.setitem(hop.llops, v_result, c_index, v_item)
+    return v_result
 
 def arraytype_compute_annotation(metatype, type):
     def compute_result_annotation(*arg_s):

Modified: pypy/dist/pypy/rpython/rctypes/rarray.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rarray.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rarray.py	Fri Apr 21 14:28:36 2006
@@ -87,6 +87,18 @@
 ##        v_c_array = self.get_c_data(llops, v_array)
 ##        llops.genop('setarrayitem', [v_c_array, v_index, v_newvalue])
 
+    def setitem(self, llops, v_array, v_index, v_item):
+        v_newvalue = self.r_item.get_c_data_or_value(llops, v_item)
+        # copy the new value (which might be a whole structure)
+        v_c_array = self.get_c_data(llops, v_array)
+        genreccopy_arrayitem(llops, v_newvalue, v_c_array, v_index)
+        # copy the keepalive information too
+        v_keepalive_array = self.getkeepalive(llops, v_array)
+        if v_keepalive_array is not None:
+            v_newkeepalive = self.r_item.getkeepalive(llops, v_item)
+            genreccopy_arrayitem(llops, v_newkeepalive,
+                                 v_keepalive_array, v_index)
+
 
 class __extend__(pairtype(ArrayRepr, IntegerRepr)):
     def rtype_getitem((r_array, r_int), hop):
@@ -106,16 +118,7 @@
     def rtype_setitem((r_array, r_int), hop):
         v_array, v_index, v_item = hop.inputargs(r_array, lltype.Signed,
                                                  r_array.r_item)
-        v_newvalue = r_array.r_item.get_c_data_or_value(hop.llops, v_item)
-        # copy the new value (which might be a whole structure)
-        v_c_array = r_array.get_c_data(hop.llops, v_array)
-        genreccopy_arrayitem(hop.llops, v_newvalue, v_c_array, v_index)
-        # copy the keepalive information too
-        v_keepalive_array = r_array.getkeepalive(hop.llops, v_array)
-        if v_keepalive_array is not None:
-            v_newkeepalive = r_array.r_item.getkeepalive(hop.llops, v_item)
-            genreccopy_arrayitem(hop.llops, v_newkeepalive,
-                                 v_keepalive_array, v_index)
+        r_array.setitem(hop.llops, v_array, v_index, v_item)
 
 
 class __extend__(pairtype(ArrayRepr, PointerRepr)):

Modified: pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_ctypes.py	Fri Apr 21 14:28:36 2006
@@ -139,6 +139,9 @@
     s.p.contents.value = 42
     assert a[0] == 42
 
+    a = (c_int * 5)(5, 6, 7)
+    assert list(a) == [5, 6, 7, 0, 0]
+
 def test_truth_value():
     p = POINTER(c_int)()
     assert not p

Modified: pypy/dist/pypy/rpython/rctypes/test/test_rarray.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rarray.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rarray.py	Fri Apr 21 14:28:36 2006
@@ -257,6 +257,17 @@
         func()
         interpret(func, [])
 
+    def test_specialize_constructor_args(self):
+        A = c_int * 5
+        def func(x, y):
+            return A(x, y)
+        res = interpret(func, [123, 456])
+        assert res.c_data[0] == 123
+        assert res.c_data[1] == 456
+        assert res.c_data[2] == 0
+        assert res.c_data[3] == 0
+        assert res.c_data[4] == 0
+
 class Test_compilation:
     def test_compile_array_access(self):
         def access_array():



More information about the Pypy-commit mailing list