Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace bool cast by call to bool builtin #2242

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/TUTORIAL.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <pythonic/include/operator_/add.hpp>\n#include <pythonic/include/operator_/lt.hpp>\n#include <pythonic/include/operator_/sub.hpp>\n#include <pythonic/operator_/add.hpp>\n#include <pythonic/operator_/lt.hpp>\n#include <pythonic/operator_/sub.hpp>\nnamespace \n{\n namespace __pythran_tutorial_module\n {\n struct fib\n {\n typedef void callable;\n typedef void pure;\n template <typename argument_type0 >\n struct type\n {\n typedef typename std::remove_cv<typename std::remove_reference<argument_type0>::type>::type __type0;\n typedef __type0 __type1;\n typedef typename pythonic::returnable<__type1>::type __type2;\n typedef __type2 result_type;\n } \n ;\n template <typename argument_type0 >\n inline\n typename type<argument_type0>::result_type operator()(argument_type0 n) const\n ;\n } ;\n template <typename argument_type0 >\n inline\n typename fib::type<argument_type0>::result_type fib::operator()(argument_type0 n) const\n {\n return (((bool)pythonic::operator_::lt(n, 2L)) ? typename __combined<decltype(n), decltype(pythonic::operator_::add(pythonic::types::call(fib(), pythonic::operator_::sub(n, 1L)), pythonic::types::call(fib(), pythonic::operator_::sub(n, 2L))))>::type(n) : typename __combined<decltype(n), decltype(pythonic::operator_::add(pythonic::types::call(fib(), pythonic::operator_::sub(n, 1L)), pythonic::types::call(fib(), pythonic::operator_::sub(n, 2L))))>::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 <pythonic/include/operator_/add.hpp>\n#include <pythonic/include/operator_/lt.hpp>\n#include <pythonic/include/operator_/sub.hpp>\n#include <pythonic/operator_/add.hpp>\n#include <pythonic/operator_/lt.hpp>\n#include <pythonic/operator_/sub.hpp>\nnamespace \n{\n namespace __pythran_tutorial_module\n {\n struct fib\n {\n typedef void callable;\n typedef void pure;\n template <typename argument_type0 >\n struct type\n {\n typedef typename std::remove_cv<typename std::remove_reference<argument_type0>::type>::type __type0;\n typedef __type0 __type1;\n typedef typename pythonic::returnable<__type1>::type __type2;\n typedef __type2 result_type;\n } \n ;\n template <typename argument_type0 >\n inline\n typename type<argument_type0>::result_type operator()(argument_type0 n) const\n ;\n } ;\n template <typename argument_type0 >\n inline\n typename fib::type<argument_type0>::result_type fib::operator()(argument_type0 n) const\n {\n return (pythonic::builtins::functor::bool_{}(pythonic::operator_::lt(n, 2L)) ? typename __combined<decltype(n), decltype(pythonic::operator_::add(pythonic::types::call(fib(), pythonic::operator_::sub(n, 1L)), pythonic::types::call(fib(), pythonic::operator_::sub(n, 2L))))>::type(n) : typename __combined<decltype(n), decltype(pythonic::operator_::add(pythonic::types::call(fib(), pythonic::operator_::sub(n, 1L)), pythonic::types::call(fib(), pythonic::operator_::sub(n, 2L))))>::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::

Expand Down
2 changes: 1 addition & 1 deletion pythran/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<decltype({1}), decltype({2})>::type({1}) "
": typename __combined<decltype({1}), decltype({2})>::type({2}))"
).format(test, body, orelse)
Expand Down
5 changes: 5 additions & 0 deletions pythran/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=[])

Expand Down
Loading