type inference for SliceIndexNode
authorStefan Behnel <scoder@users.berlios.de>
Fri, 4 Dec 2009 06:24:49 +0000 (07:24 +0100)
committerStefan Behnel <scoder@users.berlios.de>
Fri, 4 Dec 2009 06:24:49 +0000 (07:24 +0100)
Cython/Compiler/ExprNodes.py
tests/run/type_inference.pyx

index ec7507df3789271ba858aeb7b2a0968e4668c549..f765e5279e7b4e3a64a4a66cca27ec14ca29c7c8 100644 (file)
@@ -2073,6 +2073,15 @@ class SliceIndexNode(ExprNode):
     
     subexprs = ['base', 'start', 'stop']
 
+    def infer_type(self, env):
+        base_type = self.base.infer_type(env)
+        if base_type.is_string:
+            return bytes_type
+        elif base_type in (bytes_type, str_type, unicode_type,
+                           list_type, tuple_type):
+            return base_type
+        return py_object_type
+
     def calculate_constant_result(self):
         self.constant_result = self.base.constant_result[
             self.start.constant_result : self.stop.constant_result]
index 3c0a6784328d436ef037c339454fc231779e5bd6..aa188f5b529196259821dc273c44a521ffda203d 100644 (file)
@@ -46,6 +46,27 @@ def builtin_types():
     B = bool()
     assert typeof(B) == "bool object", typeof(B)
 
+def slicing():
+    """
+    >>> slicing()
+    """
+    b = b"abc"
+    assert typeof(b) == "char *", typeof(b)
+    b1 = b[1:2]
+    assert typeof(b1) == "bytes object", typeof(b1)
+    u = u"xyz"
+    assert typeof(u) == "unicode object", typeof(u)
+    u1 = u[1:2]
+    assert typeof(u1) == "unicode object", typeof(u1)
+    L = [1,2,3]
+    assert typeof(L) == "list object", typeof(L)
+    L1 = L[1:2]
+    assert typeof(L1) == "list object", typeof(L1)
+    t = (4,5,6)
+    assert typeof(t) == "tuple object", typeof(t)
+    t1 = t[1:2]
+    assert typeof(t1) == "tuple object", typeof(t1)
+
 def multiple_assignments():
     """
     >>> multiple_assignments()