[pypy-svn] r77917 - in pypy/branch/fast-forward: lib_pypy/_ctypes pypy/module/test_lib_pypy/ctypes_tests

afa at codespeak.net afa at codespeak.net
Thu Oct 14 13:43:03 CEST 2010


Author: afa
Date: Thu Oct 14 13:43:01 2010
New Revision: 77917

Modified:
   pypy/branch/fast-forward/lib_pypy/_ctypes/array.py
   pypy/branch/fast-forward/pypy/module/test_lib_pypy/ctypes_tests/test_array.py
Log:
Error in slicing ctypes arrays: a[:9999] would fail.
Test and fix.


Modified: pypy/branch/fast-forward/lib_pypy/_ctypes/array.py
==============================================================================
--- pypy/branch/fast-forward/lib_pypy/_ctypes/array.py	(original)
+++ pypy/branch/fast-forward/lib_pypy/_ctypes/array.py	Thu Oct 14 13:43:01 2010
@@ -109,8 +109,18 @@
 def array_get_slice_params(self, index):
     if index.step is not None:
         raise TypeError("3 arg slices not supported (for no reason)")
-    start = index.start or 0
-    stop = index.stop or self._length_
+    if index.start is not None:
+        start = index.start
+        if start < 0:
+            start = 0
+    else:
+        start = 0
+    if index.stop is not None:
+        stop = index.stop
+        if stop > self._length_:
+            stop = self._length_
+    else:
+        stop = self._length_
     return start, stop
 
 def array_slice_setitem(self, index, value):

Modified: pypy/branch/fast-forward/pypy/module/test_lib_pypy/ctypes_tests/test_array.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/test_lib_pypy/ctypes_tests/test_array.py	(original)
+++ pypy/branch/fast-forward/pypy/module/test_lib_pypy/ctypes_tests/test_array.py	Thu Oct 14 13:43:01 2010
@@ -88,6 +88,16 @@
         values = [i for i in na]
         assert values == [1, 2, 3, 4, 5]
 
+    def test_slice(self):
+        values = range(5)
+        numarray = c_int * 5
+
+        na = numarray(*(c_int(x) for x in values))
+
+        assert list(na[0:0]) == []
+        assert list(na[:])   == values
+        assert list(na[:10]) == values
+
     def test_classcache(self):
         assert not ARRAY(c_int, 3) is ARRAY(c_int, 4)
         assert ARRAY(c_int, 3) is ARRAY(c_int, 3)



More information about the Pypy-commit mailing list