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

Fixes the compatibility with latest nlohmann-json. #909

Merged
merged 1 commit into from
Aug 11, 2022
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
22 changes: 18 additions & 4 deletions python/core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ using namespace py::literals; // NOLINT(build/namespaces_literals)

namespace vineyard {

namespace detail {
/// Extends pybind11::detail::iterator_state to holds reference to a
/// stable (unchanged) globally available "argument".
template <typename Arg>
struct metadata_iterator_state {
ObjectMeta::const_iterator it;
ObjectMeta::const_iterator end;
bool first_or_done;
Arg arg;
};
} // namespace detail

void bind_core(py::module& mod) {
// ObjectIDWrapper
py::class_<ObjectIDWrapper>(mod, "ObjectID")
Expand Down Expand Up @@ -285,8 +297,9 @@ void bind_core(py::module& mod) {
fn = [](std::true_type, ObjectMeta::const_iterator& iter) {
return py::cast(iter.key());
};
return py::make_iterator_fmap(meta.begin(), meta.end(), fn,
std::true_type{});
using state_t = detail::metadata_iterator_state<std::true_type>;
return py::detail::make_iterator_fmap(
state_t{meta.begin(), meta.end(), true, std::true_type{}}, fn);
},
py::keep_alive<0, 1>())
.def(
Expand All @@ -304,8 +317,9 @@ void bind_core(py::module& mod) {
iter.key(), detail::from_json(iter.value())));
}
};
return py::make_iterator_fmap(meta.begin(), meta.end(), fn,
std::cref(meta));
using state_t = detail::metadata_iterator_state<const ObjectMeta&>;
return py::detail::make_iterator_fmap(
state_t{meta.begin(), meta.end(), true, std::cref(meta)}, fn);
},
py::keep_alive<0, 1>())
.def("__repr__",
Expand Down
42 changes: 13 additions & 29 deletions python/pybind11_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,50 +68,34 @@ namespace pybind11 {

namespace detail {

/// Extends pybind11::detail::iterator_state to holds reference to a
/// stable (unchanged) globally available "argument".
template <typename Iterator, typename Sentinel, typename Arg, bool KeyIterator,
return_value_policy Policy>
struct iterator_state_ext {
Iterator it;
Sentinel end;
bool first_or_done;
Arg arg;
};

} // namespace detail

/// Makes a python iterator from a first and past-the-end C++ InputIterator.
template <return_value_policy Policy = return_value_policy::reference_internal,
typename Iterator, typename Sentinel, typename F, typename Arg,
typename... Args, typename... Extra>
iterator make_iterator_fmap(Iterator first, Sentinel last, F functor, Arg arg,
Extra&&... extra) {
using state =
detail::iterator_state_ext<Iterator, Sentinel, Arg, false, Policy>;

if (!detail::get_type_info(typeid(state), false)) {
class_<state>(handle(), "iterator", pybind11::module_local())
.def("__iter__", [](state& s) -> state& { return s; })
template <py::return_value_policy Policy =
py::return_value_policy::reference_internal,
typename IteratorState, typename F, typename... Extra>
py::iterator make_iterator_fmap(IteratorState const& state, F functor,
Extra&&... extra) {
if (!py::detail::get_type_info(typeid(IteratorState), false)) {
py::class_<IteratorState>(py::handle(), "iterator",
pybind11::module_local())
.def("__iter__", [](IteratorState& s) -> IteratorState& { return s; })
.def(
"__next__",
[functor](state& s) -> object {
[functor](IteratorState& s) -> py::object {
if (!s.first_or_done)
++s.it;
else
s.first_or_done = false;
if (s.it == s.end) {
s.first_or_done = true;
throw stop_iteration();
throw py::stop_iteration();
}
return functor(s.arg, s.it);
},
std::forward<Extra>(extra)..., Policy);
}

return cast(state{first, last, true, std::forward<Arg>(arg)});
return py::cast(state);
}

} // namespace detail
} // namespace pybind11

namespace vineyard {
Expand Down
6 changes: 3 additions & 3 deletions src/common/util/protocols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,15 @@ Status ReadRegisterRequest(const json& root, std::string& version,

// When the "version" field is missing from the client, we treat it
// as default unknown version number: 0.0.0.
version = root.value<std::string>("version", "0.0.0");
version = root.value<std::string>("version", std::string("0.0.0"));

// Keep backwards compatibility.
if (root.contains("store_type")) {
if (root["store_type"].is_number()) {
store_type = root.value("store_type", /* default */ StoreType::kDefault);
} else {
std::string store_type_name =
root.value("store_type", /* default */ "Normal");
root.value("store_type", /* default */ std::string("Normal"));
if (store_type_name == "Plasma") {
store_type = StoreType::kPlasma;
} else {
Expand Down Expand Up @@ -216,7 +216,7 @@ Status ReadRegisterReply(const json& root, std::string& ipc_socket,

// When the "version" field is missing from the server, we treat it
// as default unknown version number: 0.0.0.
version = root.value<std::string>("version", "0.0.0");
version = root.value<std::string>("version", std::string("0.0.0"));
store_match = root["store_match"].get<bool>();
return Status::OK();
}
Expand Down
2 changes: 1 addition & 1 deletion thirdparty/nlohmann-json
Submodule nlohmann-json updated 1079 files