diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index f0f8fcb..3a65a68 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -185,7 +185,7 @@ class SFTPClient (BaseSFTP): if ('w' in mode): imode |= SFTP_FLAG_CREATE | SFTP_FLAG_TRUNC if ('a' in mode): - imode |= SFTP_FLAG_APPEND + imode |= SFTP_FLAG_APPEND | SFTP_FLAG_CREATE | SFTP_FLAG_WRITE attrblock = SFTPAttributes() t, msg = self._request(CMD_OPEN, filename, imode, attrblock) if t != CMD_HANDLE: diff --git a/paramiko/sftp_file.py b/paramiko/sftp_file.py index 6b8e935..854ccf4 100644 --- a/paramiko/sftp_file.py +++ b/paramiko/sftp_file.py @@ -45,6 +45,15 @@ class SFTPFile (BufferedFile): self.close() def close(self): + # We allow double-close without signaling an error, because real + # Python file objects do. However, we must protect against actually + # sending multiple CMD_CLOSE packets, because after we close our + # handle, the same handle may be re-allocated by the server, and we + # may end up mysteriously closing some random other file. (This is + # especially important because we unconditionally call close() from + # __del__.) + if self._closed: + return BufferedFile.close(self) try: self.sftp._request(CMD_CLOSE, self.handle)