From 956316e03a73b43cd5da0e4f8892cf5bda816197 Mon Sep 17 00:00:00 2001 From: Nikita Nemkin Date: Fri, 5 Apr 2013 14:02:36 +0600 Subject: [PATCH] Fix C++ compilation error when if/else expression has extension class type. --- Cython/Compiler/ExprNodes.py | 2 ++ tests/run/if_else_expr.pyx | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 08150c4..29cd825 100755 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -9234,6 +9234,8 @@ class CondExprNode(ExprNode): self.true_val = self.true_val.analyse_types(env) self.false_val = self.false_val.analyse_types(env) self.type = PyrexTypes.independent_spanning_type(self.true_val.type, self.false_val.type) + if self.type.is_pyobject: + self.result_ctype = py_object_type if self.true_val.type.is_pyobject or self.false_val.type.is_pyobject: self.true_val = self.true_val.coerce_to(self.type, env) self.false_val = self.false_val.coerce_to(self.type, env) diff --git a/tests/run/if_else_expr.pyx b/tests/run/if_else_expr.pyx index 36df14a..993b422 100644 --- a/tests/run/if_else_expr.pyx +++ b/tests/run/if_else_expr.pyx @@ -1,7 +1,11 @@ # mode: run -# tag: if_else_expr +# tag: condexpr + +cimport cython cdef class Foo: + cdef dict data + def __repr__(self): return '' @@ -14,3 +18,16 @@ cpdef test_type_cast(Foo obj, cond): """ return [obj] if cond else obj + + +cdef func(Foo foo, dict data): + return foo, data + + +@cython.test_fail_if_path_exists('//PyTypeTestNode') +def test_cpp_pyobject_cast(Foo obj1, Foo obj2, cond): + """ + >>> test_cpp_pyobject_cast(Foo(), Foo(), True) + (, None) + """ + return func(obj1 if cond else obj2, obj1.data if cond else obj2.data) -- 2.7.4