Skip to content

Commit

Permalink
Fix re-raising of KjException
Browse files Browse the repository at this point in the history
- The `KjException._to_python()` function neglected to check if the wrapper was
  set when attempting to convert to `AttributeError`, leading to exceptions while
  raising an exception.
- The syntax `raise A, B, C` hasn't existed since Python 3. The only reason it
  works is because Cython supports it. Lets get rid of it.
- There was an attempt to convert a certain kind of `KjException` to an
  `AttributeError`. However, the original exception remains in the context when
  the new exception is raised. This is confusing. We get rid of the original
  exception by doing `raise e._to_python() from None`.
  • Loading branch information
LasseBlaauwbroek authored and haata committed Nov 8, 2023
1 parent ef5e039 commit 49bda5c
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions capnp/lib/capnp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,9 @@ class KjException(Exception):

def _to_python(self):
message = self.message
if self.wrapper.type == 'FAILED':
if self.wrapper is not None and self.wrapper.type == 'FAILED':
if 'has no such' in self.message:
return AttributeError(message)
return AttributeError(message).with_traceback(self.__traceback__)
return self


Expand Down Expand Up @@ -1147,7 +1147,7 @@ cdef class _DynamicStructReader:
try:
return self._get(field)
except KjException as e:
raise e._to_python(), None, _sys.exc_info()[2]
raise e._to_python() from None

cpdef _get_by_field(self, _StructSchemaField field):
return to_python_reader(self.thisptr.getByField(field.thisptr), self)
Expand Down Expand Up @@ -1394,7 +1394,7 @@ cdef class _DynamicStructBuilder:
try:
return self._get(field)
except KjException as e:
raise e._to_python(), None, _sys.exc_info()[2]
raise e._to_python() from None

cpdef _set(self, field, value):
_setDynamicField(self.thisptr, field, value, self._parent)
Expand All @@ -1406,7 +1406,7 @@ cdef class _DynamicStructBuilder:
try:
self._set(field, value)
except KjException as e:
raise e._to_python(), None, _sys.exc_info()[2]
raise e._to_python() from None

cpdef _has(self, field):
return self.thisptr.has(field)
Expand Down Expand Up @@ -1658,7 +1658,7 @@ cdef class _DynamicStructPipeline:
try:
return self._get(field)
except KjException as e:
raise e._to_python(), None, _sys.exc_info()[2]
raise e._to_python() from None

property schema:
"""A property that returns the _StructSchema object matching this reader"""
Expand Down Expand Up @@ -2044,7 +2044,7 @@ cdef class _RemotePromise:
try:
return self._get(field)
except KjException as e:
raise e._to_python(), None, _sys.exc_info()[2]
raise e._to_python() from None

property schema:
"""A property that returns the _StructSchema object matching this reader"""
Expand Down Expand Up @@ -2198,7 +2198,7 @@ cdef class _DynamicCapabilityClient:
raise AttributeError('Method named %s not found' % name)
return _partial(self._send, name)
except KjException as e:
raise e._to_python(), None, _sys.exc_info()[2]
raise e._to_python() from None

cpdef upcast(self, schema) except +reraise_kj_exception:
cdef _InterfaceSchema s
Expand Down

0 comments on commit 49bda5c

Please sign in to comment.