From a14cb2b28cc506b789a1d389c6b17d06830b8087 Mon Sep 17 00:00:00 2001 From: Nathaniel Case Date: Tue, 8 Jun 2021 11:26:33 -0400 Subject: [PATCH] Change _get_sftp_error_str to actually return strs --- src/pylibsshext/sftp.pyx | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/pylibsshext/sftp.pyx b/src/pylibsshext/sftp.pyx index 53688561e..6331c529d 100644 --- a/src/pylibsshext/sftp.pyx +++ b/src/pylibsshext/sftp.pyx @@ -62,7 +62,7 @@ cdef class SFTP: rf = sftp.sftp_open(self._libssh_sftp_session, remote_file_b, O_WRONLY | O_CREAT | O_TRUNC, sftp.S_IRWXU) if rf is NULL: - raise LibsshSFTPException("Opening remote file [%s] for write failed with error [%s]" % (remote_file, MSG_MAP.get(self._get_sftp_error_str()))) + raise LibsshSFTPException("Opening remote file [%s] for write failed with error [%s]" % (remote_file, self._get_sftp_error_str())) buffer = f.read(1024) while buffer != b"": @@ -71,10 +71,9 @@ cdef class SFTP: if written != length: sftp.sftp_close(rf) raise LibsshSFTPException( - "Writing to remote file [%s] failed with error [%s: %s]" % ( + "Writing to remote file [%s] failed with error [%s]" % ( remote_file, - MSG_MAP.get(self._get_sftp_error_str()), - self.session._get_session_error_str(), + self._get_sftp_error_str(), ) ) buffer = f.read(1024) @@ -90,7 +89,7 @@ cdef class SFTP: rf = sftp.sftp_open(self._libssh_sftp_session, remote_file_b, O_RDONLY, sftp.S_IRWXU) if rf is NULL: - raise LibsshSFTPException("Opening remote file [%s] for read failed with error [%s]" % (remote_file, MSG_MAP.get(self._get_sftp_error_str()))) + raise LibsshSFTPException("Opening remote file [%s] for read failed with error [%s]" % (remote_file, self._get_sftp_error_str())) while True: file_data = sftp.sftp_read(rf, read_buffer, sizeof(char) * 1024) @@ -99,7 +98,7 @@ cdef class SFTP: elif file_data < 0: sftp.sftp_close(rf) raise LibsshSFTPException("Reading data from remote file [%s] failed with error [%s]" - % (remote_file, MSG_MAP.get(self._get_sftp_error_str()))) + % (remote_file, self._get_sftp_error_str())) with open(local_file, 'wb+') as f: bytes_written = f.write(read_buffer[:file_data]) @@ -108,7 +107,7 @@ cdef class SFTP: raise LibsshSFTPException("Number of bytes [%s] read from remote file [%s]" " does not match number of bytes [%s] written to local file [%s]" " due to error [%s]" - % (file_data, remote_file, bytes_written, local_file, MSG_MAP.get(self._get_sftp_error_str()))) + % (file_data, remote_file, bytes_written, local_file, self._get_sftp_error_str())) sftp.sftp_close(rf) def close(self): @@ -117,4 +116,7 @@ cdef class SFTP: self._libssh_sftp_session = NULL def _get_sftp_error_str(self): - return sftp.sftp_get_error(self._libssh_sftp_session) + error = sftp.sftp_get_error(self._libssh_sftp_session) + if error in MSG_MAP and error != sftp.SSH_FX_FAILURE: + return MSG_MAP[error] + return "Generic failure: %s" % self.session._get_session_error_str()