Commit d5915677 by Ralf W. Grosse-Kunstleve Committed by Copybara-Service

Use `return_value_policy::_clif_automatic` in status_caster.h

This change supports the PyCLIF-pybind11 integration.

PiperOrigin-RevId: 492044996
parent c4365058
...@@ -81,6 +81,7 @@ pybind_library( ...@@ -81,6 +81,7 @@ pybind_library(
deps = [ deps = [
":check_status_module_imported", ":check_status_module_imported",
":no_throw_status", ":no_throw_status",
":ok_status_singleton_lib",
":raw_ptr_from_capsule", ":raw_ptr_from_capsule",
":status_not_ok_exception", ":status_not_ok_exception",
"@com_google_absl//absl/status", "@com_google_absl//absl/status",
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "absl/status/status.h" #include "absl/status/status.h"
#include "pybind11_abseil/check_status_module_imported.h" #include "pybind11_abseil/check_status_module_imported.h"
#include "pybind11_abseil/no_throw_status.h" #include "pybind11_abseil/no_throw_status.h"
#include "pybind11_abseil/ok_status_singleton_lib.h"
#include "pybind11_abseil/raw_ptr_from_capsule.h" #include "pybind11_abseil/raw_ptr_from_capsule.h"
#include "pybind11_abseil/status_not_ok_exception.h" #include "pybind11_abseil/status_not_ok_exception.h"
...@@ -90,6 +91,11 @@ struct type_caster<absl::Status> : public type_caster_base<absl::Status> { ...@@ -90,6 +91,11 @@ struct type_caster<absl::Status> : public type_caster_base<absl::Status> {
static handle cast_impl(CType&& src, return_value_policy policy, static handle cast_impl(CType&& src, return_value_policy policy,
handle parent, bool throw_exception) { handle parent, bool throw_exception) {
google::internal::CheckStatusModuleImported(); google::internal::CheckStatusModuleImported();
#if defined(PYBIND11_HAS_RETURN_VALUE_POLICY_CLIF_AUTOMATIC)
if (src.ok() && policy == return_value_policy::_clif_automatic) {
return pybind11_abseil::PyOkStatusSingleton();
}
#endif
if (!throw_exception) { if (!throw_exception) {
// Use the built-in/standard pybind11 caster. // Use the built-in/standard pybind11 caster.
return type_caster_base<absl::Status>::cast(std::forward<CType>(src), return type_caster_base<absl::Status>::cast(std::forward<CType>(src),
......
...@@ -102,6 +102,13 @@ absl::StatusOr<int> CallGetRedirectToPython(IntGetter* ptr, int i) { ...@@ -102,6 +102,13 @@ absl::StatusOr<int> CallGetRedirectToPython(IntGetter* ptr, int i) {
} }
PYBIND11_MODULE(status_example, m) { PYBIND11_MODULE(status_example, m) {
m.attr("PYBIND11_HAS_RETURN_VALUE_POLICY_CLIF_AUTOMATIC") =
#if defined(PYBIND11_HAS_RETURN_VALUE_POLICY_CLIF_AUTOMATIC)
true;
#else
false;
#endif
auto status_module = pybind11::google::ImportStatusModule(); auto status_module = pybind11::google::ImportStatusModule();
m.attr("StatusNotOk") = status_module.attr("StatusNotOk"); m.attr("StatusNotOk") = status_module.attr("StatusNotOk");
...@@ -193,6 +200,21 @@ PYBIND11_MODULE(status_example, m) { ...@@ -193,6 +200,21 @@ PYBIND11_MODULE(status_example, m) {
return google::DoNotThrowStatus( return google::DoNotThrowStatus(
absl::Status(static_cast<absl::StatusCode>(code), msg)); absl::Status(static_cast<absl::StatusCode>(code), msg));
}); });
m.def("return_ok_status", [](bool use_return_value_policy_clif_automatic) {
if (use_return_value_policy_clif_automatic) {
#if defined(PYBIND11_HAS_RETURN_VALUE_POLICY_CLIF_AUTOMATIC)
return cast(absl::OkStatus(), return_value_policy::_clif_automatic);
#endif
}
return cast(absl::OkStatus());
});
#if defined(PYBIND11_HAS_RETURN_VALUE_POLICY_CLIF_AUTOMATIC)
m.def(
"return_ok_status_direct", []() { return absl::OkStatus(); },
return_value_policy::_clif_automatic);
#endif
} }
} // namespace test } // namespace test
......
...@@ -415,6 +415,19 @@ class StatusTest(parameterized.TestCase): ...@@ -415,6 +415,19 @@ class StatusTest(parameterized.TestCase):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
status_example.extract_code_message(something_random) status_example.extract_code_message(something_random)
def test_return_ok_status_return_value_policy_clif_automatic(self):
self.assertIsNone(status_example.return_ok_status(False))
if not status_example.PYBIND11_HAS_RETURN_VALUE_POLICY_CLIF_AUTOMATIC:
self.skipTest('return_value_policy::_clif_automatic not available')
obj = status_example.return_ok_status(True)
self.assertIs(obj, status.Status.OkStatus())
def test_return_ok_status_direct_return_value_policy_clif_automatic(self):
if not status_example.PYBIND11_HAS_RETURN_VALUE_POLICY_CLIF_AUTOMATIC:
self.skipTest('return_value_policy::_clif_automatic not available')
obj = status_example.return_ok_status_direct()
self.assertIs(obj, status.Status.OkStatus())
class IntGetter(status_example.IntGetter): class IntGetter(status_example.IntGetter):
......
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