[pypy-svn] r22741 - in pypy/dist/pypy: annotation rpython/rctypes/test

gromit at codespeak.net gromit at codespeak.net
Fri Jan 27 15:15:48 CET 2006


Author: gromit
Date: Fri Jan 27 15:15:45 2006
New Revision: 22741

Modified:
   pypy/dist/pypy/annotation/binaryop.py
   pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py
Log:
ADD: (stephan, gromit) Annotation for slice access on ctypes's arras
ADD: checking for out bounds ctypes array access in getitem
FIX: a bug in getitem annotation
ADD: some tests for the error checking code

Modified: pypy/dist/pypy/annotation/binaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/binaryop.py	(original)
+++ pypy/dist/pypy/annotation/binaryop.py	Fri Jan 27 15:15:45 2006
@@ -18,6 +18,7 @@
 from pypy.annotation.model import add_knowntypedata, merge_knowntypedata
 from pypy.annotation.bookkeeper import getbookkeeper
 from pypy.objspace.flow.model import Variable
+from pypy.annotation.listdef import ListDef
 
 # convenience only!
 def immutablevalue(x):
@@ -739,12 +740,33 @@
         pass
 
     def getitem((s_cto, s_index)):
-        if s_index.is_constant() and isinstance(s_index.const, int):
-            index = s_index.const
+        if s_index.is_constant():
+            idx = s_index.const
+            knowntype = s_cto.knowntype
             try:
-                return s_cto.knowntype._type_.annotator_type
+                length = knowntype._length_
             except AttributeError:
-                return SomeCTypesObject(s_cto.knowntype._type_)
-        else:
-            return SomeObject()
+                pass
+            else:
+                if idx < 0:
+                    idx += length
+                if idx >= length:
+                    raise IndexError( "invalid index" )
+                elif idx < 0:
+                    raise IndexError( "invalid index" )
+        try:
+            return s_cto.knowntype._type_.annotator_type
+        except AttributeError:
+            return SomeCTypesObject(s_cto.knowntype._type_)
+
+class __extend__(pairtype(SomeCTypesObject, SomeSlice)):
+    def setitem((s_cto, s_slice), s_iterable):
+        pass
+
+    def getitem((s_cto, s_slice)):
+        try:
+            listdef = ListDef(None, s_cto.knowntype._type_.annotator_type)
+        except AttributeError:
+            listdef = ListDef(None, SomeCTypesObject(s_cto.knowntype._type_))
+        return SomeList(listdef)
 

Modified: pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py	Fri Jan 27 15:15:45 2006
@@ -129,6 +129,29 @@
 
     return my_pointer[0]
 
+def py_test_annotate_array_slice_content():
+    my_array = c_int_10()
+    #f#my_array[0:7] = c_int(1) * 7
+    my_array[0:5] = range(5)
+
+    return my_array[0:5]
+
+def py_test_annotate_array_content_variable_index():
+    my_array = c_int_10()
+    my_array[2] = 2
+    sum = 0
+    for idx in range(10):
+        sum += my_array[idx]
+
+    return sum
+
+def py_test_annotate_array_content_index_error_on_positive_index():
+    my_array = c_int_10()
+    return my_array[10]
+
+def py_test_annotate_array_content_index_error_on_negative_index():
+    my_array = c_int_10()
+    return my_array[-10]
 
 class Test_rctypes:
 
@@ -276,4 +299,31 @@
         assert s.knowntype == int
         #t#t.view()
 
+    def test_annotate_array_slice_access(self):
+        t = TranslationContext()
+        a = t.buildannotator()
+        s = a.build_types(py_test_annotate_array_slice_content,[])
+        #t#t.view()
+        #d#print "v90:", s, type(s)
+        assert s.knowntype == list
+        s.listdef.listitem.s_value.knowntype == int
+
+    def test_annotate_array_access_variable(self):
+        t = TranslationContext()
+        a = t.buildannotator()
+        s = a.build_types(py_test_annotate_array_content_variable_index,[])
+        assert s.knowntype == int
+        #t#t.view()
+
+    def test_annotate_array_access_index_error_on_positive_index(self):
+        t = TranslationContext()
+        a = t.buildannotator()
+        
+        py.test.raises(IndexError, "s = a.build_types(py_test_annotate_array_content_index_error_on_positive_index,[])")
+
+    def test_annotate_array_access_index_error_on_negative_index(self):
+        t = TranslationContext()
+        a = t.buildannotator()
+        
+        py.test.raises(IndexError, "s = a.build_types(py_test_annotate_array_content_index_error_on_negative_index,[])")
 



More information about the Pypy-commit mailing list