summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-09-29 00:49:16 +0100
committerMichael Williamson <mike@zwobble.org>2014-09-07 18:15:16 +0100
commit979838040f9ff2f52e369812be87f0c7c9c1a0db (patch)
tree8844108f454c11f463d42d9cbc55a2220165e9c4
parentec9f8a26d4ea77ce6e4a1afe1a9e3b29dbf002bf (diff)
Introduce ClosingContextManager for classes that already implement close()
-rw-r--r--paramiko/sftp_file.py9
-rw-r--r--paramiko/util.py8
2 files changed, 10 insertions, 7 deletions
diff --git a/paramiko/sftp_file.py b/paramiko/sftp_file.py
index 03d67b33..e0fb2297 100644
--- a/paramiko/sftp_file.py
+++ b/paramiko/sftp_file.py
@@ -34,9 +34,10 @@ from paramiko.py3compat import long
from paramiko.sftp import CMD_CLOSE, CMD_READ, CMD_DATA, SFTPError, CMD_WRITE, \
CMD_STATUS, CMD_FSTAT, CMD_ATTRS, CMD_FSETSTAT, CMD_EXTENDED
from paramiko.sftp_attr import SFTPAttributes
+from paramiko.util import ClosingContextManager
-class SFTPFile (BufferedFile):
+class SFTPFile (BufferedFile, ClosingContextManager):
"""
Proxy object for a file on the remote server, in client mode SFTP.
@@ -488,9 +489,3 @@ class SFTPFile (BufferedFile):
x = self._saved_exception
self._saved_exception = None
raise x
-
- def __enter__(self):
- return self
-
- def __exit__(self, type, value, traceback):
- self.close()
diff --git a/paramiko/util.py b/paramiko/util.py
index f4ee3adc..ee1a3abb 100644
--- a/paramiko/util.py
+++ b/paramiko/util.py
@@ -320,3 +320,11 @@ def constant_time_bytes_eq(a, b):
for i in (xrange if PY2 else range)(len(a)):
res |= byte_ord(a[i]) ^ byte_ord(b[i])
return res == 0
+
+
+class ClosingContextManager(object):
+ def __enter__(self):
+ return self
+
+ def __exit__(self, type, value, traceback):
+ self.close()