Commit f735c948 by Ralf W. Grosse-Kunstleve Committed by Copybara-Service

Change third_party/pybind11_abseil `StatusNotOk` to raise an exception when…

Change third_party/pybind11_abseil `StatusNotOk` to raise an exception when constructed with a plain `str`.

Step 2 of third_party/pybind11_abseil `StatusNotOk` convergence with the Google-internal type.

PiperOrigin-RevId: 473295335
parent 0c45d0f5
...@@ -173,13 +173,7 @@ void RegisterStatusBindings(module m) { ...@@ -173,13 +173,7 @@ void RegisterStatusBindings(module m) {
pybind11::exec(R"( pybind11::exec(R"(
class StatusNotOk(Exception): class StatusNotOk(Exception):
def __init__(self, *args): def __init__(self, status):
if len(args) != 1 or isinstance(args[0], str):
# THIS WILL BECOME AN ERROR IN THE FUTURE.
self._status = None
Exception.__init__(self, *args)
return
status = args[0]
assert status is not None assert status is not None
assert not status.ok() assert not status.ok()
self._status = status self._status = status
...@@ -187,19 +181,16 @@ void RegisterStatusBindings(module m) { ...@@ -187,19 +181,16 @@ void RegisterStatusBindings(module m) {
@property @property
def status(self): def status(self):
assert self._status is not None
return self._status return self._status
@property @property
def code(self): def code(self):
assert self._status is not None
# code is int by choice. Sorry it would be a major API break to make # code is int by choice. Sorry it would be a major API break to make
# this an enum. # this an enum.
return self._status.raw_code() return self._status.raw_code()
@property @property
def message(self): def message(self):
assert self._status is not None
return self._status.message() return self._status.message()
)", )",
m.attr("__dict__"), m.attr("__dict__")); m.attr("__dict__"), m.attr("__dict__"));
......
...@@ -57,8 +57,9 @@ class StatusTest(absltest.TestCase): ...@@ -57,8 +57,9 @@ class StatusTest(absltest.TestCase):
self.assertEqual(e.message, 'Cnclld') self.assertEqual(e.message, 'Cnclld')
def test_status_nok_ok_str(self): def test_status_nok_ok_str(self):
e = status.StatusNotOk('Deprecated.') with self.assertRaises(AttributeError) as cm:
self.assertEqual(str(e), 'Deprecated.') status.StatusNotOk('')
self.assertEqual(str(cm.exception), "'str' object has no attribute 'ok'")
def test_status_nok_ok_none(self): def test_status_nok_ok_none(self):
with self.assertRaises(AssertionError) as cm: with self.assertRaises(AssertionError) as cm:
......
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