From b64bf1bac7e136c28a3608193e85e8c86046f856 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Sun, 24 Nov 2013 16:20:39 +0100 Subject: [PATCH] fix compiler crash on unitialised result_code and fix coercion of float/bool back to C values --- Cython/Compiler/ExprNodes.py | 18 ++++++++++++++---- Cython/Compiler/Optimize.py | 3 ++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 438dbd0..fca38fb 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -958,6 +958,11 @@ class BoolNode(ConstNode): self.pos, value=self.value, constant_result=self.constant_result, type=Builtin.bool_type) + if dst_type.is_int and self.type.is_pyobject: + return BoolNode( + self.pos, value=self.value, + constant_result=self.constant_result, + type=PyrexTypes.c_bint_type) return ConstNode.coerce_to(self, dst_type, env) @@ -1111,12 +1116,17 @@ class FloatNode(ConstNode): self.pos, value=self.value, constant_result=self.constant_result, type=Builtin.float_type) + if dst_type.is_float and self.type.is_pyobject: + return FloatNode( + self.pos, value=self.value, + constant_result=self.constant_result, + type=dst_type) return ConstNode.coerce_to(self, dst_type, env) def calculate_result_code(self): return self.result_code - def as_c_constant(self): + def get_constant_c_result_code(self): strval = self.value assert isinstance(strval, (str, unicode)) cmpval = repr(float(strval)) @@ -1130,11 +1140,11 @@ class FloatNode(ConstNode): return strval def generate_evaluation_code(self, code): + c_value = self.get_constant_c_result_code() if self.type.is_pyobject: - self.result_code = code.get_py_float( - self.value, self.as_c_constant()) + self.result_code = code.get_py_float(self.value, c_value) else: - self.result_code = self.as_c_constant() + self.result_code = c_value class BytesNode(ConstNode): diff --git a/Cython/Compiler/Optimize.py b/Cython/Compiler/Optimize.py index 3187a98..eb95ef3 100644 --- a/Cython/Compiler/Optimize.py +++ b/Cython/Compiler/Optimize.py @@ -1792,7 +1792,8 @@ class OptimizeBuiltinCalls(Visitor.MethodDispatcherTransform): arg = arg.arg if arg.is_literal: if (node.type.is_int and isinstance(arg, ExprNodes.IntNode) or - node.type.is_float and isinstance(arg, ExprNodes.FloatNode)): + node.type.is_float and isinstance(arg, ExprNodes.FloatNode) or + node.type.is_int and isinstance(arg, ExprNodes.BoolNode)): return arg.coerce_to(node.type, self.current_env()) elif isinstance(arg, ExprNodes.CoerceToPyTypeNode): if arg.type is PyrexTypes.py_object_type: -- 2.7.4