Commit 87c41a4e by Xiaofei Wang Committed by Copybara-Service

Make the absl::Status type caster convert Python None to OkStatus.

PiperOrigin-RevId: 508428006
parent 43b546dc
......@@ -72,6 +72,11 @@ struct type_caster<absl::Status> : public type_caster_base<absl::Status> {
bool load(handle src, bool convert) {
if (type_caster_base<absl::Status>::load(src, convert)) {
// Behavior change 2023-02-09: previously `value` was simply left as
// `nullptr`.
if (!value) {
value = const_cast<absl::Status*>(pybind11_abseil::OkStatusSingleton());
}
return true;
}
if (convert) {
......
......@@ -192,11 +192,16 @@ class StatusTest(parameterized.TestCase):
with self.assertRaises(TypeError):
status_example.extract_code_message(cap)
@parameterized.parameters(None, '', 0)
@parameterized.parameters('', 0)
def test_status_caster_load_no_as_absl_status(self, something_random):
with self.assertRaises(TypeError):
status_example.extract_code_message(something_random)
def test_status_caster_load_none(self):
code, msg = status_example.extract_code_message(None)
self.assertEqual(code, status.StatusCode.OK)
self.assertEqual(msg, '')
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:
......
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