Commit 44ee235b by Ralf W. Grosse-Kunstleve Committed by Copybara-Service

Enable `type_caster_std_function_specializations` with or without…

Enable `type_caster_std_function_specializations` with or without `return_value_policy_pack` feature.

This is for https://github.com/pybind/pybind11/pull/5289 applied to smart_holder.

PiperOrigin-RevId: 659973774
parent 8ccb4c7b
...@@ -120,10 +120,8 @@ struct type_caster<absl::Status> : public type_caster_base<absl::Status> { ...@@ -120,10 +120,8 @@ struct type_caster<absl::Status> : public type_caster_base<absl::Status> {
} }
}; };
#if defined(PYBIND11_HAS_RETURN_VALUE_POLICY_PACK) && \ #if defined(PYBIND11_HAS_TYPE_CASTER_STD_FUNCTION_SPECIALIZATIONS)
defined(PYBIND11_HAS_TYPE_CASTER_STD_FUNCTION_SPECIALIZATIONS)
// This code requires https://github.com/google/pybind11k
// IMPORTANT: // IMPORTANT:
// KEEP // KEEP
// type_caster<absl::Status> // type_caster<absl::Status>
...@@ -141,7 +139,11 @@ struct func_wrapper<absl::Status, Args...> : func_wrapper_base { ...@@ -141,7 +139,11 @@ struct func_wrapper<absl::Status, Args...> : func_wrapper_base {
gil_scoped_acquire acq; gil_scoped_acquire acq;
try { try {
object py_result = object py_result =
#if defined(PYBIND11_HAS_RETURN_VALUE_POLICY_PACK)
hfunc.f.call_with_policies(rvpp, std::forward<Args>(args)...); hfunc.f.call_with_policies(rvpp, std::forward<Args>(args)...);
#else
hfunc.f(std::forward<Args>(args)...);
#endif
try { try {
return py_result.template cast<absl::Status>(); return py_result.template cast<absl::Status>();
} catch (cast_error& e) { } catch (cast_error& e) {
......
...@@ -124,9 +124,8 @@ struct type_caster<absl::StatusOr<PayloadType>> { ...@@ -124,9 +124,8 @@ struct type_caster<absl::StatusOr<PayloadType>> {
} }
}; };
#if defined(PYBIND11_HAS_RETURN_VALUE_POLICY_PACK) #if defined(PYBIND11_HAS_TYPE_CASTER_STD_FUNCTION_SPECIALIZATIONS)
// This code requires https://github.com/google/pybind11k
// IMPORTANT: // IMPORTANT:
// KEEP // KEEP
// type_caster<absl::StatusOr<PayloadType>> // type_caster<absl::StatusOr<PayloadType>>
...@@ -144,7 +143,11 @@ struct func_wrapper<absl::StatusOr<PayloadType>, Args...> : func_wrapper_base { ...@@ -144,7 +143,11 @@ struct func_wrapper<absl::StatusOr<PayloadType>, Args...> : func_wrapper_base {
gil_scoped_acquire acq; gil_scoped_acquire acq;
try { try {
object py_result = object py_result =
#if defined(PYBIND11_HAS_RETURN_VALUE_POLICY_PACK)
hfunc.f.call_with_policies(rvpp, std::forward<Args>(args)...); hfunc.f.call_with_policies(rvpp, std::forward<Args>(args)...);
#else
hfunc.f(std::forward<Args>(args)...);
#endif
try { try {
auto cpp_result = auto cpp_result =
py_result.template cast<absl::StatusOr<PayloadType>>(); py_result.template cast<absl::StatusOr<PayloadType>>();
......
...@@ -16,13 +16,6 @@ namespace status_testing_no_cpp_eh { ...@@ -16,13 +16,6 @@ namespace status_testing_no_cpp_eh {
PYBIND11_MODULE(status_testing_no_cpp_eh_pybind, m) { PYBIND11_MODULE(status_testing_no_cpp_eh_pybind, m) {
pybind11::google::ImportStatusModule(); pybind11::google::ImportStatusModule();
m.attr("defined_PYBIND11_HAS_TYPE_CASTER_STD_FUNCTION_SPECIALIZATIONS") =
#if defined(PYBIND11_HAS_TYPE_CASTER_STD_FUNCTION_SPECIALIZATIONS)
true;
#else
false;
#endif
m.def("CallCallbackWithStatusReturn", &CallCallbackWithStatusReturn); m.def("CallCallbackWithStatusReturn", &CallCallbackWithStatusReturn);
m.def("CallCallbackWithStatusOrIntReturn", m.def("CallCallbackWithStatusOrIntReturn",
&CallCallbackWithStatusOrIntReturn); &CallCallbackWithStatusOrIntReturn);
...@@ -31,8 +24,8 @@ PYBIND11_MODULE(status_testing_no_cpp_eh_pybind, m) { ...@@ -31,8 +24,8 @@ PYBIND11_MODULE(status_testing_no_cpp_eh_pybind, m) {
pybind11::return_value_policy::take_ownership); pybind11::return_value_policy::take_ownership);
m.def("GenerateErrorStatusNotOk", &GenerateErrorStatusNotOk); m.def("GenerateErrorStatusNotOk", &GenerateErrorStatusNotOk);
m.attr("defined_PYBIND11_HAS_RETURN_VALUE_POLICY_PACK") = m.attr("defined_PYBIND11_HAS_TYPE_CASTER_STD_FUNCTION_SPECIALIZATIONS") =
#if defined(PYBIND11_HAS_RETURN_VALUE_POLICY_PACK) #if defined(PYBIND11_HAS_TYPE_CASTER_STD_FUNCTION_SPECIALIZATIONS)
true; true;
#else #else
false; false;
......
...@@ -185,7 +185,7 @@ class StatusOrPyObjectPtrTest(absltest.TestCase): ...@@ -185,7 +185,7 @@ class StatusOrPyObjectPtrTest(absltest.TestCase):
if ( if (
hasattr(self.tm, '__pyclif_codegen_mode__') hasattr(self.tm, '__pyclif_codegen_mode__')
or self.tm.defined_PYBIND11_HAS_RETURN_VALUE_POLICY_PACK or self.tm.defined_PYBIND11_HAS_TYPE_CASTER_STD_FUNCTION_SPECIALIZATIONS
): ):
res = cc_fn(cb, 'exc') res = cc_fn(cb, 'exc')
self.assertEqual(res, "!obj.ok()@ValueError: Unknown arg: 'exc'") self.assertEqual(res, "!obj.ok()@ValueError: Unknown arg: 'exc'")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment