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

Upgrade pybind and refine pybind/protobuf.cc #7563

Closed
wants to merge 5 commits into from

Conversation

qingqing01
Copy link
Contributor

@qingqing01 qingqing01 commented Jan 16, 2018

Fix #7562

  1. Upgrade pybind11 from v2.1.1 to v2.2.1 .
  2. Remove the code about casting boost::variant for PyBind in pybind/protobuf.cc

@jacquesqiao
Copy link
Member

what is the improvement of upgraded pybind11?

@qingqing01
Copy link
Contributor Author

qingqing01 commented Jan 17, 2018

There are two reasons to upgrade pybind11:

  1. The Refine profiler and expose to Python. #7576 needs add_ostream_redirect to redirect std::ostream to Python's streams . This must upgrade pybind11, the v2.1.1 does not support this feature.
  2. As mentioned in Upgrade pybind and refine pybind/protobuf.cc #7562, the pybind11 v2.2.0 and v2.2.1 have supported to convert variant types, if upgrade pybind11, the following code in pybind/protobuf.cc can be removed.

// Can be replaced by a generic lambda in C++14
struct variant_caster_visitor : public boost::static_visitor<handle> {
return_value_policy policy;
handle parent;
variant_caster_visitor(return_value_policy policy, handle parent)
: policy(policy), parent(parent) {}
template <class T>
handle operator()(T const &src) const {
return make_caster<T>::cast(src, policy, parent);
}
};
template <class Variant>
struct variant_caster;
template <template <class...> class V, class... Ts>
struct variant_caster<V<Ts...>> {
using Type = V<Ts...>;
template <typename T>
typename std::enable_if<
!std::is_same<T, boost::detail::variant::void_>::value, bool>::type
try_load(handle src, bool convert) {
auto caster = make_caster<T>();
if (!load_success_ && caster.load(src, convert)) {
load_success_ = true;
value = cast_op<T>(caster);
return true;
}
return false;
}
template <typename T>
typename std::enable_if<std::is_same<T, boost::detail::variant::void_>::value,
bool>::type
try_load(handle src, bool convert) {
return false;
}
bool load(handle src, bool convert) {
auto unused = {false, try_load<Ts>(src, convert)...};
(void)(unused);
return load_success_;
}
static handle cast(Type const &src, return_value_policy policy,
handle parent) {
variant_caster_visitor visitor(policy, parent);
return boost::apply_visitor(visitor, src);
}
PYBIND11_TYPE_CASTER(Type, _("Variant"));
bool load_success_{false};
};

@qingqing01 qingqing01 mentioned this pull request Mar 12, 2018
@qingqing01 qingqing01 closed this Jul 10, 2018
@qingqing01 qingqing01 deleted the upgrade_pybind branch November 14, 2019 05:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants