Commit 2c4932ed by Xiaofei Wang Committed by Copybara-Service

Internal change

PiperOrigin-RevId: 473369330
parent f735c948
...@@ -44,12 +44,12 @@ struct type_caster<absl::StatusOr<PayloadType>> { ...@@ -44,12 +44,12 @@ struct type_caster<absl::StatusOr<PayloadType>> {
PYBIND11_TYPE_CASTER(absl::StatusOr<PayloadType>, PayloadCaster::name); PYBIND11_TYPE_CASTER(absl::StatusOr<PayloadType>, PayloadCaster::name);
// We need this to support overriding virtual functions in Python. See the bool load(handle src, bool convert) {
// test cases for example. PayloadCaster base_caster;
bool load(handle /*src*/, bool /*convert*/) { if (base_caster.load(src, convert)) {
// This will not be called as long as we do not call C++ functions that value = cast_op<PayloadType>(std::move(base_caster));
// redirect virtual calls back to Python. return true;
// TODO(wangxf): Implement the load function. }
return false; return false;
} }
......
...@@ -93,6 +93,14 @@ class PyIntGetter : public IntGetter { ...@@ -93,6 +93,14 @@ class PyIntGetter : public IntGetter {
} }
}; };
absl::StatusOr<int> CallGetRedirectToPython(IntGetter* ptr, int i) {
if (ptr) {
return ptr->Get(i);
}
return absl::InvalidArgumentError(
"Function parameter should not be nullptr.");
}
PYBIND11_MODULE(status_example, m) { PYBIND11_MODULE(status_example, m) {
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");
...@@ -153,6 +161,8 @@ PYBIND11_MODULE(status_example, m) { ...@@ -153,6 +161,8 @@ PYBIND11_MODULE(status_example, m) {
class_<IntGetter, PyIntGetter>(m, "IntGetter") class_<IntGetter, PyIntGetter>(m, "IntGetter")
.def(init()) .def(init())
.def("Get", &IntGetter::Get); .def("Get", &IntGetter::Get);
m.def("call_get_redirect_to_python", &CallGetRedirectToPython,
arg("ptr"), arg("i"));
// Needed to exercise raw_code() != code(). // Needed to exercise raw_code() != code().
m.def("status_from_int_code", [](int code, const std::string& msg) { m.def("status_from_int_code", [](int code, const std::string& msg) {
......
...@@ -258,7 +258,10 @@ class StatusOrTest(absltest.TestCase): ...@@ -258,7 +258,10 @@ class StatusOrTest(absltest.TestCase):
self.assertEqual(int_getter.Get(5), 5) self.assertEqual(int_getter.Get(5), 5)
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
int_getter.Get(100) int_getter.Get(100)
self.assertEqual(
status_example.call_get_redirect_to_python(int_getter, 5), 5)
with self.assertRaises(ValueError):
status_example.call_get_redirect_to_python(int_getter, 100)
if __name__ == '__main__': if __name__ == '__main__':
absltest.main() absltest.main()
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