From bd9b1d7713cf1f09c1967c389f58e2173c398bb6 Mon Sep 17 00:00:00 2001 From: serge-sans-paille Date: Thu, 17 Oct 2024 09:28:01 +0200 Subject: [PATCH] Replace bool cast by call to bool builtin Some types (e.g. std::tuple) don't support bool cast, let's use the builtin to do it. --- docs/TUTORIAL.rst | 2 +- pythran/backend.py | 2 +- pythran/tests/test_base.py | 5 +++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/docs/TUTORIAL.rst b/docs/TUTORIAL.rst index 7692547eb..4c8003e44 100644 --- a/docs/TUTORIAL.rst +++ b/docs/TUTORIAL.rst @@ -76,7 +76,7 @@ subset of Python AST) into a C++ AST:: >>> from pythran import backend >>> cxx = pm.dump(backend.Cxx, tree) >>> str(cxx) - '#include \n#include \n#include \n#include \n#include \n#include \nnamespace \n{\n namespace __pythran_tutorial_module\n {\n struct fib\n {\n typedef void callable;\n typedef void pure;\n template \n struct type\n {\n typedef typename std::remove_cv::type>::type __type0;\n typedef __type0 __type1;\n typedef typename pythonic::returnable<__type1>::type __type2;\n typedef __type2 result_type;\n } \n ;\n template \n inline\n typename type::result_type operator()(argument_type0 n) const\n ;\n } ;\n template \n inline\n typename fib::type::result_type fib::operator()(argument_type0 n) const\n {\n return (((bool)pythonic::operator_::lt(n, 2L)) ? typename __combined::type(n) : typename __combined::type(pythonic::operator_::add(pythonic::types::call(fib(), pythonic::operator_::sub(n, 1L)), pythonic::types::call(fib(), pythonic::operator_::sub(n, 2L)))));\n }\n }\n}' + '#include \n#include \n#include \n#include \n#include \n#include \nnamespace \n{\n namespace __pythran_tutorial_module\n {\n struct fib\n {\n typedef void callable;\n typedef void pure;\n template \n struct type\n {\n typedef typename std::remove_cv::type>::type __type0;\n typedef __type0 __type1;\n typedef typename pythonic::returnable<__type1>::type __type2;\n typedef __type2 result_type;\n } \n ;\n template \n inline\n typename type::result_type operator()(argument_type0 n) const\n ;\n } ;\n template \n inline\n typename fib::type::result_type fib::operator()(argument_type0 n) const\n {\n return (pythonic::builtins::functor::bool_{}(pythonic::operator_::lt(n, 2L)) ? typename __combined::type(n) : typename __combined::type(pythonic::operator_::add(pythonic::types::call(fib(), pythonic::operator_::sub(n, 1L)), pythonic::types::call(fib(), pythonic::operator_::sub(n, 2L)))));\n }\n }\n}' The above string is understandable by a C++11 compiler, but it quickly reaches the limit of our developer brain, so most of the time, we are more comfortable with the Python backend:: diff --git a/pythran/backend.py b/pythran/backend.py index afedfe62f..cb8ed8fd3 100644 --- a/pythran/backend.py +++ b/pythran/backend.py @@ -907,7 +907,7 @@ def visit_IfExp(self, node): body = self.visit(node.body) orelse = self.visit(node.orelse) return ( - "(((bool){0}) " + "(pythonic::builtins::functor::bool_{{}}({0}) " "? typename __combined::type({1}) " ": typename __combined::type({2}))" ).format(test, body, orelse) diff --git a/pythran/tests/test_base.py b/pythran/tests/test_base.py index e19d713b6..273b16f75 100644 --- a/pythran/tests/test_base.py +++ b/pythran/tests/test_base.py @@ -247,6 +247,11 @@ def test_multidef4(self): def test_tuple(self): self.run_test("def tuple_(t): return t[0]+t[1]", (0,1), tuple_=[Tuple[int, int]]) + def test_tuple_implicit_empty(self): + self.run_test("def tuple_implicit_empty_(t): return 1 if t else 0", + (0,1), + tuple_implicit_empty_=[Tuple[int, int]]) + def test_nested_list_comprehension(self): self.run_test("def nested_list_comprehension(): return [ [ x+y for x in range(10) ] for y in range(20) ]", nested_list_comprehension=[])