infer type of slices for some builtin types
authorStefan Behnel <scoder@users.berlios.de>
Wed, 8 Sep 2010 07:21:19 +0000 (09:21 +0200)
committerStefan Behnel <scoder@users.berlios.de>
Wed, 8 Sep 2010 07:21:19 +0000 (09:21 +0200)
Cython/Compiler/ExprNodes.py
tests/run/type_inference.pyx

index bbabac2808390c6462206d23d73823715dad81ce..d12f48a80ba2f0eb8cc5b4170c97a0aa8ccb8f4b 100755 (executable)
@@ -1942,8 +1942,12 @@ class IndexNode(ExprNode):
         return self.base.type_dependencies(env)
     
     def infer_type(self, env):
+        is_slice = isinstance(self.index, SliceNode)
         if isinstance(self.base, BytesNode):
-            return py_object_type
+            if is_slice:
+                return bytes_type
+            else:
+                return py_object_type # Py2/3 return different types
         base_type = self.base.infer_type(env)
         if base_type.is_ptr or base_type.is_array:
             return base_type.base_type
@@ -1957,7 +1961,10 @@ class IndexNode(ExprNode):
             # on a subsequent PyObject coercion.
             return PyrexTypes.c_py_unicode_type
         elif base_type in (str_type, unicode_type):
-            # these types will always return themselves on Python indexing
+            # these types will always return their own type on Python indexing/slicing
+            return base_type
+        elif is_slice and base_type in (bytes_type, list_type, tuple_type):
+            # slicing these returns the same type
             return base_type
         else:
             # TODO: Handle buffers (hopefully without too much redundancy).
index c567c12835b90155f976d4d998cc5c2fcafcc4aa..495498711ae056ff69fa4b427546d01d756e281a 100644 (file)
@@ -68,10 +68,14 @@ def slicing():
     assert typeof(L) == "list object", typeof(L)
     L1 = L[1:2]
     assert typeof(L1) == "list object", typeof(L1)
+    L2 = L[1:2:2]
+    assert typeof(L2) == "list object", typeof(L2)
     t = (4,5,6)
     assert typeof(t) == "tuple object", typeof(t)
     t1 = t[1:2]
     assert typeof(t1) == "tuple object", typeof(t1)
+    t2 = t[1:2:2]
+    assert typeof(t2) == "tuple object", typeof(t2)
 
 def indexing():
     """