Skip to content

Commit

Permalink
Merge pull request #1304 from STEllAR-GROUP/fixing_1301
Browse files Browse the repository at this point in the history
Fixing handling of tuples as return values
  • Loading branch information
hkaiser authored Jan 21, 2021
2 parents dd5cb13 + 64916dd commit 8e2441e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
2 changes: 1 addition & 1 deletion python/phylanx/ast/physl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,7 @@ def _Return(self, node):
symbol = get_symbol_info(node, "return")

if type(node.value) == ast.Tuple:
return [symbol, self._apply_rule(node.value)]
return [symbol, (self._apply_rule(node.value),)]

value = self._apply_rule(node.value)
if value is None:
Expand Down
30 changes: 29 additions & 1 deletion src/execution_tree/compiler/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,29 @@ namespace phylanx { namespace execution_tree { namespace compiler {
name_, id));
}

std::string list_of_arguments(std::vector<std::size_t> const& args)
{
if (args.size() == 1)
{
return std::to_string(args[0]);
}

std::string result;
for (std::size_t i = 0; i != args.size(); ++i)
{
result += std::to_string(args[i]);
if (i == args.size() - 2)
{
result += " or ";
}
else if (i != args.size() - 1)
{
result += ", ";
}
}
return result;
}

// handle function call arguments (default values, keyword arguments)
void handle_function_call_argument(std::string const& function_name,
primitive_arguments_type& fargs,
Expand Down Expand Up @@ -1243,17 +1266,22 @@ namespace phylanx { namespace execution_tree { namespace compiler {

// find the first pattern that has a sufficient number of
// placeholders
std::vector<std::size_t> args;
args.reserve(std::distance(p.first, p.second));

auto it = p.first;
auto jt = it;
while (jt != p.second && !jt->second.expect_variadics() &&
exprs.size() > jt->second.args_.size())
{
args.push_back(jt->second.args_.size());
it = jt;
jt = ++p.first;
}

if (jt == p.second)
{
std::sort(args.begin(), args.end());
HPX_THROW_EXCEPTION(hpx::bad_parameter,
"phylanx::execution_tree::compiler::"
"handle_function_call_argument",
Expand All @@ -1262,7 +1290,7 @@ namespace phylanx { namespace execution_tree { namespace compiler {
"attempt to call function '{}' "
"with too many arguments (expected: {}, "
"supplied: {})",
function_name, it->second.args_.size(),
function_name, list_of_arguments(args),
exprs.size()),
name_, id));
}
Expand Down
25 changes: 25 additions & 0 deletions tests/regressions/python/1301_return_tuple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright (c) 2021 Steven R. Brandt
#
# Distributed under the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

# #1301: Error processing if statement

from phylanx import Phylanx
import numpy as np


def __type(x): # silence Flake
pass


@Phylanx
def lshape(x):
if (__type(x) == __type(list()) and len(x) == 0):
return (0,)
else:
return np.shape(x)


result = lshape(np.linspace(0, 1, 100))
assert result == [100], result
1 change: 1 addition & 0 deletions tests/regressions/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ set(tests
1279_global_keyword
1280_hstack_problem
1281_hostname
1301_return_tuple
array_len_494
array_shape_486
array_subscript_403
Expand Down

0 comments on commit 8e2441e

Please sign in to comment.