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