Skip to content

Commit

Permalink
Change third_party/pybind11_abseil StatusNotOk to raise an exceptio…
Browse files Browse the repository at this point in the history
…n when constructed with a plain `str`.

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

PiperOrigin-RevId: 473295335
  • Loading branch information
Ralf W. Grosse-Kunstleve authored and copybara-github committed Sep 9, 2022
1 parent 0c45d0f commit f735c94
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 12 deletions.
11 changes: 1 addition & 10 deletions pybind11_abseil/register_status_bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,33 +173,24 @@ void RegisterStatusBindings(module m) {

pybind11::exec(R"(
class StatusNotOk(Exception):
def __init__(self, *args):
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]
def __init__(self, status):
assert status is not None
assert not status.ok()
self._status = status
Exception.__init__(self, status.to_string_status_not_ok())
@property
def status(self):
assert self._status is not None
return self._status
@property
def code(self):
assert self._status is not None
# code is int by choice. Sorry it would be a major API break to make
# this an enum.
return self._status.raw_code()
@property
def message(self):
assert self._status is not None
return self._status.message()
)",
m.attr("__dict__"), m.attr("__dict__"));
Expand Down
5 changes: 3 additions & 2 deletions pybind11_abseil/tests/status_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ def test_status_not_ok_status(self):
self.assertEqual(e.message, 'Cnclld')

def test_status_nok_ok_str(self):
e = status.StatusNotOk('Deprecated.')
self.assertEqual(str(e), 'Deprecated.')
with self.assertRaises(AttributeError) as cm:
status.StatusNotOk('')
self.assertEqual(str(cm.exception), "'str' object has no attribute 'ok'")

def test_status_nok_ok_none(self):
with self.assertRaises(AssertionError) as cm:
Expand Down

0 comments on commit f735c94

Please sign in to comment.