Commit 38b42686 by pybind11_abseil authors Committed by Copybara-Service

internal change.

PiperOrigin-RevId: 508543167
parent 87c41a4e
#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "util/task/status_builder.h"
namespace pybind11 {
namespace google {
namespace {
constexpr absl::string_view kDisplay = "1";
constexpr absl::string_view kDoNotDisplay = "0";
constexpr absl::string_view kDisplaySourceLocationInPython =
"pybind11_abseil_display_source_location";
} // namespace
bool HasDisplaySourceLocationInPython(absl::Status s) {
auto optional_payload = s.GetPayload(kDisplaySourceLocationInPython);
return optional_payload.has_value() &&
optional_payload.value() == kDisplay;
}
bool HasDoNotDisplaySourceLocationInPython(absl::Status s) {
auto optional_payload = s.GetPayload(kDisplaySourceLocationInPython);
return optional_payload.has_value() &&
optional_payload.value() == kDoNotDisplay;
}
absl::Status DisplaySourceLocationInPython(absl::Status s) {
s.SetPayload(kDisplaySourceLocationInPython, absl::Cord(kDisplay));
return s;
}
absl::Status DoNotDisplaySourceLocationInPython(absl::Status s) {
s.SetPayload(kDisplaySourceLocationInPython, absl::Cord(kDoNotDisplay));
return s;
}
util::StatusBuilder DisplaySourceLocationInPython(util::StatusBuilder sb) {
return sb.SetPayload(kDisplaySourceLocationInPython, absl::Cord(kDisplay));
}
util::StatusBuilder DoNotDisplaySourceLocationInPython(util::StatusBuilder sb) {
return sb.SetPayload(kDisplaySourceLocationInPython,
absl::Cord(kDoNotDisplay));
}
} // namespace google
} // namespace pybind11
#ifndef PYBIND11_ABSEIL_DISPLAY_SOURCE_LOCATION_IN_PYTHON_H_
#define PYBIND11_ABSEIL_DISPLAY_SOURCE_LOCATION_IN_PYTHON_H_
// This file exists to simplify adding C++ source location to python StatusNotOk
// traces. See: b/266066084 for details.
#include "absl/status/status.h"
#include "util/task/status_builder.h"
namespace pybind11 {
namespace google {
// Return true if the status was set with DisplaySourceLocationInPython.
bool HasDisplaySourceLocationInPython(absl::Status s);
bool HasDoNotDisplaySourceLocationInPython(absl::Status s);
// Annotate the Status to display the c++ SourceLocation in python.
absl::Status DisplaySourceLocationInPython(absl::Status s);
absl::Status DoNotDisplaySourceLocationInPython(absl::Status s);
// Annotate the StatusBuilder to display the c++ SourceLocation in python.
util::StatusBuilder DisplaySourceLocationInPython(util::StatusBuilder sb);
util::StatusBuilder DoNotDisplaySourceLocationInPython(util::StatusBuilder sb);
// Annotate the StatusOr<T> to display the c++ SourceLocation in python.
template<typename StatusOrT>
StatusOrT DisplaySourceLocationInPython(StatusOrT&& s_or_t) {
if (s_or_t.ok()) {
return s_or_t;
}
absl::Status status_with_payload = DisplaySourceLocationInPython(
s_or_t.status());
return StatusOrT{status_with_payload};
}
template<typename StatusOrT>
StatusOrT DoNotDisplaySourceLocationInPython(StatusOrT&& s_or_t) {
if (s_or_t.ok()) {
return s_or_t;
}
absl::Status status_with_payload = DoNotDisplaySourceLocationInPython(
s_or_t.status());
return StatusOrT{status_with_payload};
}
} // namespace google
} // namespace pybind11
#endif // PYBIND11_ABSEIL_DISPLAY_SOURCE_LOCATION_IN_PYTHON_H_
...@@ -264,9 +264,11 @@ void RegisterStatusBindings(module m) { ...@@ -264,9 +264,11 @@ void RegisterStatusBindings(module m) {
(void(absl::Status::*)(const absl::Status&)) & absl::Status::Update, (void(absl::Status::*)(const absl::Status&)) & absl::Status::Update,
arg("other")) arg("other"))
.def("to_string", .def("to_string",
[](const absl::Status& s) { [](const absl::Status& s, bool with_source_location) {
if (with_source_location) {
}
return decode_utf8_replace(s.ToString()); return decode_utf8_replace(s.ToString());
}) }, kw_only{}, arg("with_source_location") = false)
.def("status_not_ok_str", .def("status_not_ok_str",
[](const absl::Status& s) { [](const absl::Status& s) {
std::string code_str = absl::StatusCodeToString(s.code()); std::string code_str = absl::StatusCodeToString(s.code());
......
...@@ -302,7 +302,8 @@ class StatusOrTest(absltest.TestCase): ...@@ -302,7 +302,8 @@ class StatusOrTest(absltest.TestCase):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
int_getter.Get(100) int_getter.Get(100)
self.assertEqual( self.assertEqual(
status_example.call_get_redirect_to_python(int_getter, 5), 5) status_example.call_get_redirect_to_python(int_getter, 5), 5
)
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
status_example.call_get_redirect_to_python(int_getter, 100) status_example.call_get_redirect_to_python(int_getter, 100)
......
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