From 681dcbf47e891dca5ee31e5f0dee2832d4c1beec Mon Sep 17 00:00:00 2001 From: Craig Citro Date: Tue, 6 Jul 2010 22:34:43 -0700 Subject: [PATCH] Make str unsafe for type inference. See trac #553. --- Cython/Compiler/TypeInference.py | 10 ++++++++-- tests/run/type_inference.pyx | 5 +++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Cython/Compiler/TypeInference.py b/Cython/Compiler/TypeInference.py index 2b865b50..a21042f2 100644 --- a/Cython/Compiler/TypeInference.py +++ b/Cython/Compiler/TypeInference.py @@ -314,8 +314,14 @@ def safe_spanning_type(types, might_overflow): if result_type.is_reference: result_type = result_type.ref_base_type if result_type.is_pyobject: - # any specific Python type is always safe to infer - return result_type + # In theory, any specific Python type is always safe to + # infer. However, inferring str can cause some existing code + # to break, since we are also now much more strict about + # coercion from str to char *. See trac #553. + if result_type.name == 'str': + return py_object_type + else: + return result_type elif result_type is PyrexTypes.c_double_type: # Python's float type is just a C double, so it's safe to use # the C type instead diff --git a/tests/run/type_inference.pyx b/tests/run/type_inference.pyx index a05848f5..f7fe8551 100644 --- a/tests/run/type_inference.pyx +++ b/tests/run/type_inference.pyx @@ -362,6 +362,11 @@ def safe_only(): res = ~d assert typeof(d) == "long", typeof(d) + # we special-case inference to type str, see + # trac # + s = "abc" + assert typeof(s) == "Python object", typeof(s) + # potentially overflowing arithmetic e = 1 e += 1 -- 2.26.2