From aae9d0c4f99a290ce3561c52e4dfe7529dcee213 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Fri, 4 Dec 2009 07:24:49 +0100 Subject: [PATCH] type inference for SliceIndexNode --- Cython/Compiler/ExprNodes.py | 9 +++++++++ tests/run/type_inference.pyx | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index ec7507df..f765e527 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -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] diff --git a/tests/run/type_inference.pyx b/tests/run/type_inference.pyx index 3c0a6784..aa188f5b 100644 --- a/tests/run/type_inference.pyx +++ b/tests/run/type_inference.pyx @@ -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() -- 2.26.2