Skip to content

Commit

Permalink
Clean up SFTP exception handling
Browse files Browse the repository at this point in the history
This commit cleans up SFTP exception handling to avoid reporting nested
exceptions in a few places. It also takes advantage of the new
SFTPEOFError exception in one place that was missed in the previous
round of changes for that.
  • Loading branch information
ronf committed Mar 28, 2020
1 parent f3d7c0f commit 27af348
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions asyncssh/sftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def _setstat(path, attrs):
try:
os.chown(path, attrs.uid, attrs.gid)
except AttributeError: # pragma: no cover
raise NotImplementedError
raise NotImplementedError from None

if attrs.permissions is not None:
os.chmod(path, stat.S_IMODE(attrs.permissions))
Expand Down Expand Up @@ -241,7 +241,7 @@ async def match_glob(fs, pattern, error_handler=None):
if error_handler:
error_handler(exc)
else:
raise exc
raise

return names

Expand Down Expand Up @@ -1315,9 +1315,9 @@ async def start(self):
data = resp.get_string()
extensions.append((name, data))
except PacketDecodeError as exc:
raise SFTPBadMessage(str(exc))
raise SFTPBadMessage(str(exc)) from None
except (asyncio.IncompleteReadError, Error) as exc:
raise SFTPFailure(str(exc))
raise SFTPFailure(str(exc)) from None

self.logger.debug1('Received version=%d%s', version,
', extensions:' if extensions else '')
Expand Down Expand Up @@ -1667,9 +1667,8 @@ async def read(self, size=-1, offset=None):
else:
data = await self._handler.read(self._handle, offset, size)
self._offset = offset + len(data)
except SFTPError as exc:
if exc.code != FX_EOF:
raise
except SFTPEOFError:
pass

if self._encoding:
data = data.decode(self._encoding, self._errors)
Expand Down Expand Up @@ -4656,7 +4655,7 @@ def statvfs(self, path):
try:
return os.statvfs(_to_local_path(self.map_path(path)))
except AttributeError: # pragma: no cover
raise SFTPOpUnsupported('statvfs not supported')
raise SFTPOpUnsupported('statvfs not supported') from None

def fstatvfs(self, file_obj):
"""Return attributes of the file system containing an open file
Expand All @@ -4675,7 +4674,7 @@ def fstatvfs(self, file_obj):
try:
return os.statvfs(file_obj.fileno())
except AttributeError: # pragma: no cover
raise SFTPOpUnsupported('fstatvfs not supported')
raise SFTPOpUnsupported('fstatvfs not supported') from None

def link(self, oldpath, newpath):
"""Create a hard link
Expand Down

0 comments on commit 27af348

Please sign in to comment.