Commit 9f13dde9 by Ralf W. Grosse-Kunstleve Committed by Copybara-Service

Internal change: Raise a derived `StatusNotOk` type if the Python import is available.

PiperOrigin-RevId: 487063854
parent c4baef68
...@@ -113,11 +113,46 @@ std::size_t boost_hash_combine(std::size_t lhs, std::size_t rhs) { ...@@ -113,11 +113,46 @@ std::size_t boost_hash_combine(std::size_t lhs, std::size_t rhs) {
return lhs; return lhs;
} }
handle ThisModule(handle m = nullptr) {
static handle this_module = nullptr;
if (m) {
this_module = m;
}
return this_module;
}
handle PyStatusNotOkTypeInUse() {
static handle type_in_use = nullptr;
if (type_in_use) {
return type_in_use;
}
object module_in_use;
// Import any module with a derived or alternative StatusNotOk type here
// and assign to module_in_use.
if (!module_in_use) {
module_in_use = reinterpret_borrow<object>(ThisModule());
if (!module_in_use) {
throw std::runtime_error(
"Internal error: ThisModule() undefined (" __FILE__ ":" +
std::to_string(__LINE__) + ")");
}
}
// Intentionally leak this Python reference:
// https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables
type_in_use = object(module_in_use.attr("StatusNotOk")).release();
return type_in_use;
}
} // namespace } // namespace
namespace internal { namespace internal {
void RegisterStatusBindings(module m) { void RegisterStatusBindings(module m) {
ThisModule(m);
enum_<InitFromTag>(m, "InitFromTag") enum_<InitFromTag>(m, "InitFromTag")
.value("capsule", InitFromTag::capsule) .value("capsule", InitFromTag::capsule)
.value("capsule_direct_only", InitFromTag::capsule_direct_only) .value("capsule_direct_only", InitFromTag::capsule_direct_only)
...@@ -389,25 +424,22 @@ void RegisterStatusBindings(module m) { ...@@ -389,25 +424,22 @@ void RegisterStatusBindings(module m) {
)", )",
m.attr("__dict__"), m.attr("__dict__")); m.attr("__dict__"), m.attr("__dict__"));
// Intentionally leak this Python reference:
// https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables
static handle PyStatusNotOk = object(m.attr("StatusNotOk")).release();
// Register a custom handler which converts a C++ StatusNotOk to a // Register a custom handler which converts a C++ StatusNotOk to a
// PyStatusNotOk. // PyStatusNotOk.
register_exception_translator([](std::exception_ptr p) { register_exception_translator([](std::exception_ptr p) {
try { try {
if (p) std::rethrow_exception(p); if (p) std::rethrow_exception(p);
} catch (const StatusNotOk& e) { } catch (const StatusNotOk& e) {
PyErr_SetObject( PyErr_SetObject(PyStatusNotOkTypeInUse().ptr(),
PyStatusNotOk.ptr(), PyStatusNotOkTypeInUse()(
PyStatusNotOk(google::NoThrowStatus<absl::Status>(e.status())).ptr()); google::NoThrowStatus<absl::Status>(e.status()))
.ptr());
} }
}); });
m.def("BuildStatusNotOk", [](absl::StatusCode code, const std::string& msg) { m.def("BuildStatusNotOk", [](absl::StatusCode code, const std::string& msg) {
return PyStatusNotOk(google::NoThrowStatus<absl::Status>( return PyStatusNotOkTypeInUse()(
absl::Status(code, msg))); google::NoThrowStatus<absl::Status>(absl::Status(code, msg)));
}); });
} }
......
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