Skip to content

Commit

Permalink
fix(unflatten): fix passing empty array span
Browse files Browse the repository at this point in the history
  • Loading branch information
XuehaiPan committed Nov 7, 2024
1 parent af33117 commit 0a9dfe7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/treespec/treespec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ namespace optree {
const py::object children[],
const size_t& num_children) {
EXPECT_EQ(py::ssize_t_cast(num_children), node.arity, "Node arity did not match.");
EXPECT_TRUE(children != nullptr || num_children == 0, "Node children is null.");

switch (node.kind) {
case PyTreeKind::Leaf:
INTERNAL_ERROR("PyTreeSpec::MakeNode() not implemented for leaves.");
Expand Down
4 changes: 3 additions & 1 deletion src/treespec/unflatten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ py::object PyTreeSpec::UnflattenImpl(const Span& leaves) const {
case PyTreeKind::StructSequence:
case PyTreeKind::Custom: {
const ssize_t size = py::ssize_t_cast(agenda.size());
py::object out = MakeNode(node, &agenda[size - node.arity], node.arity);
py::object out = MakeNode(node,
(node.arity > 0 ? &agenda[size - node.arity] : nullptr),
node.arity);
agenda.resize(size - node.arity);
agenda.emplace_back(std::move(out));
break;
Expand Down

0 comments on commit 0a9dfe7

Please sign in to comment.