From cae90a088676a150e4a05e64ea64cc6f4d11f693 Mon Sep 17 00:00:00 2001 From: Ram Rachum Date: Mon, 15 Jun 2020 10:38:12 +0300 Subject: [PATCH] Fix exception causes all over the codebase --- jupyter_client/asynchronous/client.py | 4 ++-- jupyter_client/blocking/client.py | 4 ++-- jupyter_client/session.py | 13 +++++++------ jupyter_client/ssh/tunnel.py | 4 ++-- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/jupyter_client/asynchronous/client.py b/jupyter_client/asynchronous/client.py index 3cf49ed4b..c4ab1f179 100644 --- a/jupyter_client/asynchronous/client.py +++ b/jupyter_client/asynchronous/client.py @@ -169,8 +169,8 @@ async def _recv_reply(self, msg_id, timeout=None, channel='shell'): reply = await self.get_control_msg(timeout=timeout) else: reply = await self.get_shell_msg(timeout=timeout) - except Empty: - raise TimeoutError("Timeout waiting for reply") + except Empty as e: + raise TimeoutError("Timeout waiting for reply") from e if reply['parent_header'].get('msg_id') != msg_id: # not my reply, someone may have forgotten to retrieve theirs continue diff --git a/jupyter_client/blocking/client.py b/jupyter_client/blocking/client.py index 83fed8587..81ccd0628 100644 --- a/jupyter_client/blocking/client.py +++ b/jupyter_client/blocking/client.py @@ -134,8 +134,8 @@ def _recv_reply(self, msg_id, timeout=None, channel='shell'): reply = self.get_control_msg(timeout=timeout) else: reply = self.get_shell_msg(timeout=timeout) - except Empty: - raise TimeoutError("Timeout waiting for reply") + except Empty as e: + raise TimeoutError("Timeout waiting for reply") from e if reply['parent_header'].get('msg_id') != msg_id: # not my reply, someone may have forgotten to retrieve theirs continue diff --git a/jupyter_client/session.py b/jupyter_client/session.py index 45d5f8101..35ca0f2d9 100644 --- a/jupyter_client/session.py +++ b/jupyter_client/session.py @@ -379,8 +379,9 @@ def _signature_scheme_changed(self, change): hash_name = new.split('-', 1)[1] try: self.digest_mod = getattr(hashlib, hash_name) - except AttributeError: - raise TraitError("hashlib has no such attribute: %s" % hash_name) + except AttributeError as e: + raise TraitError("hashlib has no such attribute: %s" % + hash_name) from e self._new_auth() digest_mod = Any() @@ -537,7 +538,7 @@ def _check_packers(self): jsonmsg = "" raise ValueError( msg.format(packer=self.packer, e=e, jsonmsg=jsonmsg) - ) + ) from e # ensure packed message is bytes if not isinstance(packed, bytes): @@ -555,7 +556,7 @@ def _check_packers(self): jsonmsg = "" raise ValueError( msg.format(packer=self.packer, unpacker=self.unpacker, e=e, jsonmsg=jsonmsg) - ) + ) from e # check datetime support msg = dict(t=utcnow()) @@ -733,8 +734,8 @@ def send(self, stream, msg_or_type, content=None, parent=None, ident=None, try: # check to see if buf supports the buffer protocol. view = memoryview(buf) - except TypeError: - raise TypeError("Buffer objects must support the buffer protocol.") + except TypeError as e: + raise TypeError("Buffer objects must support the buffer protocol.") from e # memoryview.contiguous is new in 3.3, # just skip the check on Python 2 if hasattr(view, 'contiguous') and not view.contiguous: diff --git a/jupyter_client/ssh/tunnel.py b/jupyter_client/ssh/tunnel.py index e1cd08027..754c11c7a 100644 --- a/jupyter_client/ssh/tunnel.py +++ b/jupyter_client/ssh/tunnel.py @@ -241,12 +241,12 @@ def openssh_tunnel(lport, rport, server, remoteip='127.0.0.1', keyfile=None, pas raise SSHException('The authenticity of the host can\'t be established.') except pexpect.TIMEOUT: continue - except pexpect.EOF: + except pexpect.EOF as e: if tunnel.exitstatus: print(tunnel.exitstatus) print(tunnel.before) print(tunnel.after) - raise RuntimeError("tunnel '%s' failed to start" % (cmd)) + raise RuntimeError("tunnel '%s' failed to start" % (cmd)) from e else: return tunnel.pid else: