Skip to content

Commit 63cd953

Browse files
committed
Always explicitly chain exceptions
So we have "exception 1 caused by exception 2" instead of "during handling of exception 2, another exception was raised".
1 parent ca51b8d commit 63cd953

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

src/execnet/gateway.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ def _source_of_function(function):
201201

202202
try:
203203
source = inspect.getsource(function)
204-
except OSError:
205-
raise ValueError("can't find source file for %s" % function)
204+
except OSError as e:
205+
raise ValueError("can't find source file for %s" % function) from e
206206

207207
source = textwrap.dedent(source) # just for inner functions
208208

src/execnet/gateway_base.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ def get(self, timeout=None):
289289
try:
290290
return self._result
291291
except AttributeError:
292-
raise self._exc
292+
raise self._exc from None
293293

294294
def waitfinish(self, timeout=None):
295295
if not self._result_ready.wait(timeout):
@@ -528,7 +528,7 @@ def from_io(io):
528528
if not header:
529529
raise EOFError("empty read")
530530
except EOFError as e:
531-
raise EOFError("couldn't load message header, " + e.args[0])
531+
raise EOFError("couldn't load message header, " + e.args[0]) from None
532532
msgtype, channel, payload = struct.unpack("!bii", header)
533533
return Message(msgtype, channel, io.read(payload))
534534

@@ -851,7 +851,7 @@ def receive(self, timeout=None):
851851
try:
852852
x = itemqueue.get(timeout=timeout)
853853
except self.gateway.execmodel.queue.Empty:
854-
raise self.TimeoutError("no item after %r seconds" % timeout)
854+
raise self.TimeoutError("no item after %r seconds" % timeout) from None
855855
if x is ENDMARKER:
856856
itemqueue.put(x) # for other receivers
857857
raise self._getremoteerror() or EOFError()
@@ -865,7 +865,7 @@ def next(self):
865865
try:
866866
return self.receive()
867867
except EOFError:
868-
raise StopIteration
868+
raise StopIteration from None
869869

870870
__next__ = next
871871

@@ -1108,7 +1108,7 @@ def _send(self, msgcode, channelid=0, data=b""):
11081108
except (OSError, ValueError) as e:
11091109
self._trace("failed to send", message, e)
11101110
# ValueError might be because the IO is already closed
1111-
raise OSError("cannot send (already closed?)")
1111+
raise OSError("cannot send (already closed?)") from e
11121112

11131113
def _local_schedulexec(self, channel, sourcetask):
11141114
channel.close("execution disallowed")
@@ -1316,12 +1316,12 @@ def load(self, versioned=False):
13161316
loader = self.num2func[opcode]
13171317
except KeyError:
13181318
raise LoadError(
1319-
"unknown opcode %r - " "wire protocol corruption?" % (opcode,)
1320-
)
1319+
"unknown opcode %r - wire protocol corruption?" % (opcode,)
1320+
) from None
13211321
loader(self)
13221322
except _Stop:
13231323
if len(self.stack) != 1:
1324-
raise LoadError("internal unserialization error")
1324+
raise LoadError("internal unserialization error") from None
13251325
return self.stack.pop(0)
13261326
else:
13271327
raise LoadError("didn't get STOP")
@@ -1550,7 +1550,7 @@ def _save(self, obj):
15501550
methodname = "save_" + tp.__name__
15511551
meth = getattr(self.__class__, methodname, None)
15521552
if meth is None:
1553-
raise DumpError(f"can't serialize {tp}")
1553+
raise DumpError(f"can't serialize {tp}") from None
15541554
dispatch = self._dispatch[tp] = meth
15551555
dispatch(self, obj)
15561556

@@ -1574,8 +1574,8 @@ def save_str(self, s):
15741574
def _write_unicode_string(self, s):
15751575
try:
15761576
as_bytes = s.encode("utf-8")
1577-
except UnicodeEncodeError:
1578-
raise DumpError("strings must be utf-8 encodable")
1577+
except UnicodeEncodeError as e:
1578+
raise DumpError("strings must be utf-8 encodable") from e
15791579
self._write_byte_sequence(as_bytes)
15801580

15811581
def _write_byte_sequence(self, bytes_):

src/execnet/gateway_bootstrap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def bootstrap_exec(io, spec):
5050
except EOFError:
5151
ret = io.wait()
5252
if ret == 255:
53-
raise HostNotFound(io.remoteaddress)
53+
raise HostNotFound(io.remoteaddress) from None
5454

5555

5656
def bootstrap_socket(io, id):

0 commit comments

Comments
 (0)