Refine exception invoking which on non-union type

Related to https://github.com/capnproto/pycapnp/issues/254
This commit is contained in:
John Vandenberg
2021-06-01 15:40:35 +08:00
parent 927d8b5128
commit 0f6df849cd
2 changed files with 32 additions and 14 deletions

View File

@@ -1147,8 +1147,10 @@ cdef class _DynamicStructReader:
cpdef _which_str(self): cpdef _which_str(self):
try: try:
return <char *>helpers.fixMaybe(self.thisptr.which()).getProto().getName().cStr() return <char *>helpers.fixMaybe(self.thisptr.which()).getProto().getName().cStr()
except: except RuntimeError as e:
raise KjException("Attempted to call which on a non-union type") if str(e) == "Member was null.":
raise KjException("Attempted to call which on a non-union type")
raise
cpdef _DynamicEnumField _which(self): cpdef _DynamicEnumField _which(self):
"""Returns the enum corresponding to the union in this struct """Returns the enum corresponding to the union in this struct
@@ -1161,8 +1163,10 @@ cdef class _DynamicStructReader:
try: try:
which = _DynamicEnumField()._init( which = _DynamicEnumField()._init(
_StructSchemaField()._init(helpers.fixMaybe(self.thisptr.which()), self).proto) _StructSchemaField()._init(helpers.fixMaybe(self.thisptr.which()), self).proto)
except: except RuntimeError as e:
raise KjException("Attempted to call which on a non-union type") if str(e) == "Member was null.":
raise KjException("Attempted to call which on a non-union type")
raise
return which return which
@@ -1445,8 +1449,10 @@ cdef class _DynamicStructBuilder:
cpdef _which_str(self): cpdef _which_str(self):
try: try:
return <char *>helpers.fixMaybe(self.thisptr.which()).getProto().getName().cStr() return <char *>helpers.fixMaybe(self.thisptr.which()).getProto().getName().cStr()
except: except RuntimeError as e:
raise KjException("Attempted to call which on a non-union type") if str(e) == "Member was null.":
raise KjException("Attempted to call which on a non-union type")
raise
cpdef _DynamicEnumField _which(self): cpdef _DynamicEnumField _which(self):
"""Returns the enum corresponding to the union in this struct """Returns the enum corresponding to the union in this struct
@@ -1459,8 +1465,10 @@ cdef class _DynamicStructBuilder:
try: try:
which = _DynamicEnumField()._init( which = _DynamicEnumField()._init(
_StructSchemaField()._init(helpers.fixMaybe(self.thisptr.which()), self).proto) _StructSchemaField()._init(helpers.fixMaybe(self.thisptr.which()), self).proto)
except: except RuntimeError as e:
raise KjException("Attempted to call which on a non-union type") if str(e) == "Member was null.":
raise KjException("Attempted to call which on a non-union type")
raise
return which return which

View File

@@ -4,6 +4,8 @@ import os
import tempfile import tempfile
import sys import sys
from capnp.lib.capnp import KjException
this_dir = os.path.dirname(__file__) this_dir = os.path.dirname(__file__)
@@ -37,9 +39,13 @@ def test_which_builder(addressbook):
assert bob.employment.which == addressbook.Person.Employment.unemployed assert bob.employment.which == addressbook.Person.Employment.unemployed
assert bob.employment.which == "unemployed" assert bob.employment.which == "unemployed"
with pytest.raises(Exception): with pytest.raises(KjException):
addresses.which addresses._which()
with pytest.raises(Exception):
with pytest.raises(KjException):
addresses._which_str()
with pytest.raises(KjException):
addresses.which addresses.which
@@ -71,9 +77,13 @@ def test_which_reader(addressbook):
bob = people[1] bob = people[1]
assert bob.employment.which == "unemployed" assert bob.employment.which == "unemployed"
with pytest.raises(Exception): with pytest.raises(KjException):
addresses.which addresses._which_str()
with pytest.raises(Exception):
with pytest.raises(KjException):
addresses._which()
with pytest.raises(KjException):
addresses.which addresses.which