From 112b72511eb186175839fc0b8d66b6fc6dcdb534 Mon Sep 17 00:00:00 2001 From: Robey Pointer Date: Sun, 18 Sep 2005 07:25:54 +0000 Subject: [PATCH] [project @ Arch-1:robey@lag.net--2005-master-shake%paramiko--dev--1--patch-56] patch from nathaniel smith: fix SFTPClient.open() 'a' flag, and guard against multiple close() of the same file --- paramiko/sftp_client.py | 2 +- paramiko/sftp_file.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) 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)