-
Notifications
You must be signed in to change notification settings - Fork 118
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
Add helper for parsing tuples using nanobind #3025
Draft
mgeplf
wants to merge
5
commits into
master
Choose a base branch
from
nanobind-parse-tuple
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#pragma once | ||
#include <cstddef> | ||
#include <stdexcept> | ||
|
||
#include <nanobind/nanobind.h> | ||
#include <fmt/format.h> | ||
|
||
namespace nrn { | ||
namespace detail { | ||
|
||
template <typename T, size_t I> | ||
T get(const nanobind::tuple& t) { | ||
return nanobind::cast<T>(t[I]); | ||
} | ||
|
||
template <typename... Ts, typename Tuple, std::size_t... Is> | ||
auto cast_tuple_impl(Tuple&& tuple, std::index_sequence<Is...>) { | ||
auto count = sizeof...(Is); | ||
if (count > tuple.size()) { | ||
throw std::runtime_error(fmt::format( | ||
"Not enough arguments, expected {}, but only received {}", count, tuple.size())); | ||
} | ||
return std::make_tuple(get<Ts, Is>(std::forward<Tuple>(tuple))...); | ||
} | ||
} // namespace detail | ||
|
||
/*! | ||
* Extract an exact number of elements from a tuple, including casting them to certain types. | ||
* | ||
* This uses `nanobind::cast`, so expect a `nanobind::cast_error()` if there are incompatible types. | ||
* If the number of supplied elements in the tuple is fewer than the expected number, a std::runtime | ||
* error is thrown. | ||
* | ||
* @param tuple can either be a nanobind::args or a nanobind::tuple | ||
*/ | ||
template <typename... Ts, typename Tuple> | ||
auto cast_tuple(Tuple&& tuple) { | ||
return detail::cast_tuple_impl<Ts...>(std::forward<Tuple>(tuple), | ||
std::make_index_sequence<sizeof...(Ts)>{}); | ||
} | ||
|
||
} // namespace nrn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include <nanobind/nanobind.h> | ||
#include <nanobind/stl/string.h> | ||
|
||
#include "cast_tuple.hpp" | ||
|
||
#include <catch2/catch_test_macros.hpp> | ||
|
||
TEST_CASE("can_cast_nanobind_tuple", "[Neuron][nrnpython]") { | ||
auto tup = nanobind::make_tuple(42, "test", 3.14); | ||
auto [i, s, d] = nrn::cast_tuple<int, std::string, double>(tup); | ||
REQUIRE(i == 42); | ||
REQUIRE(s == "test"); | ||
REQUIRE(d == 3.14); | ||
} | ||
|
||
TEST_CASE("can_cast_nanobind_args", "[Neuron][nrnpython]") { | ||
auto tup = nanobind::make_tuple(42, "test", 3.14); | ||
auto args = nanobind::cast<nanobind::args>(tup); | ||
auto [i, s, d] = nrn::cast_tuple<int, std::string, double>(args); | ||
REQUIRE(i == 42); | ||
REQUIRE(s == "test"); | ||
REQUIRE(d == 3.14); | ||
} | ||
|
||
TEST_CASE("can_cast_extra_arguments", "[Neuron][nrnpython]") { | ||
auto tup = nanobind::make_tuple("test", 3.14, 42); | ||
auto [s, d] = nrn::cast_tuple<std::string, double>(tup); | ||
REQUIRE(s == "test"); | ||
REQUIRE(d == 3.14); | ||
} | ||
|
||
TEST_CASE("not_enough_arguments", "[Neuron][nrnpython]") { | ||
auto tup = nanobind::make_tuple(42, std::string("test")); | ||
REQUIRE_THROWS(nrn::cast_tuple<int, std::string, double>(tup)); | ||
|
||
auto empty_tuple = nanobind::tuple(); | ||
REQUIRE_THROWS(nrn::cast_tuple<int>(empty_tuple)); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have noticed that some of these constructors are the equivalent of the python constructor of the same name, which is also the case here.
nb::tuple()
calls eitherPySequence_Tuple
orPyTuple_New
effectively behaving as python'stuple()
I have manually
steal
orborrow
and let the object be moved(
nb::tuple(nb::borrow(args))
in this case). Not sure if there is anything to be gained, it's mostly for info.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I follow what you mean by:
ie: what you mean by
moved
in the above, and why would one add the extranb::borrow()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My comment is basically, there's a lot going on in the background when you do
nb::tuple()
, whereas if you would have donenb::tuple(nb::borrow(args)
there's an explicit creation ofnb::object
with a borrowed reference, which being an rvalue-ref it's moved to be anb::tuple
in an almost-no-op.Since I don't fully know what your direct call implicitly does, sometimes being explicit is better. But if you find out it does the very right (and minimal) thing I'd be glad to know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I was on vacation and missed this.
I'm all for being explicit, but I'm not sure why I want to
nb::borrow
; the refcnt doesn't need to be incremented from what I can tell, why would we need to do that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nb::tuple
extendsnb::object
, so there will be decref at the end of its life. I believe in this case we must borrow, not steal. Hence my comment, maybe we would better be explicit about it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see why that is a problem. The
nb::tuple
calls https://github.com/wjakob/nanobind/blob/master/include/nanobind/nb_types.h#L478 which then calls https://github.com/wjakob/nanobind/blob/master/src/common.cpp#L623This is then used to initialize the
nb::object
, so it has the correct refcount, and thus when it is dtor'd, everything is correct.Note that both code paths go through
PySequence_Tuple
, so a new python tuple is created with its own lifetime and refcount.