From a64c63704998aedda810e9e469efd63901cea0f2 Mon Sep 17 00:00:00 2001 From: Nathaniel Case Date: Mon, 7 Jun 2021 12:49:22 -0400 Subject: [PATCH] sftp_get: write bytes directly, don't expect them to be unicode --- docs/changelog-fragments/216.bugfix.2.rst | 1 + docs/changelog-fragments/216.bugfix.rst | 2 +- src/pylibsshext/sftp.pyx | 8 ++++---- 3 files changed, 6 insertions(+), 5 deletions(-) create mode 100644 docs/changelog-fragments/216.bugfix.2.rst diff --git a/docs/changelog-fragments/216.bugfix.2.rst b/docs/changelog-fragments/216.bugfix.2.rst new file mode 100644 index 000000000..56ad82fe7 --- /dev/null +++ b/docs/changelog-fragments/216.bugfix.2.rst @@ -0,0 +1 @@ +Changed ``sftp.sftp_get`` to write files as bytes rather than assuming files are valid UTF8 -- by :user: `Qalthos` diff --git a/docs/changelog-fragments/216.bugfix.rst b/docs/changelog-fragments/216.bugfix.rst index 7ece219ed..0d329afe6 100644 --- a/docs/changelog-fragments/216.bugfix.rst +++ b/docs/changelog-fragments/216.bugfix.rst @@ -1 +1 @@ -Added additional error reporting to SFTP write errors -- :user: Qalthos +Added additional error reporting to SFTP write errors -- by :user: `Qalthos` diff --git a/src/pylibsshext/sftp.pyx b/src/pylibsshext/sftp.pyx index 65ef59ec2..53688561e 100644 --- a/src/pylibsshext/sftp.pyx +++ b/src/pylibsshext/sftp.pyx @@ -101,14 +101,14 @@ cdef class SFTP: raise LibsshSFTPException("Reading data from remote file [%s] failed with error [%s]" % (remote_file, MSG_MAP.get(self._get_sftp_error_str()))) - with open(local_file, 'w+') as f: - bytes_wrote = f.write(read_buffer[:file_data].decode('utf-8')) - if bytes_wrote and file_data != bytes_wrote: + with open(local_file, 'wb+') as f: + bytes_written = f.write(read_buffer[:file_data]) + if bytes_written and file_data != bytes_written: sftp.sftp_close(rf) 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_wrote, local_file, MSG_MAP.get(self._get_sftp_error_str()))) + % (file_data, remote_file, bytes_written, local_file, MSG_MAP.get(self._get_sftp_error_str()))) sftp.sftp_close(rf) def close(self):