Skip to content

Commit

Permalink
sftp_get: write bytes directly, don't expect them to be unicode
Browse files Browse the repository at this point in the history
  • Loading branch information
Qalthos committed Jun 11, 2021
1 parent 2daf85f commit a64c637
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/changelog-fragments/216.bugfix.2.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Changed ``sftp.sftp_get`` to write files as bytes rather than assuming files are valid UTF8 -- by :user: `Qalthos`
2 changes: 1 addition & 1 deletion docs/changelog-fragments/216.bugfix.rst
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Added additional error reporting to SFTP write errors -- :user: Qalthos
Added additional error reporting to SFTP write errors -- by :user: `Qalthos`
8 changes: 4 additions & 4 deletions src/pylibsshext/sftp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit a64c637

Please sign in to comment.