[paramiko] Adding support for the "with" statement

Michael van der Kolff mvanderkolff at gmail.com
Tue Mar 13 20:05:40 PDT 2007


I feel that adding support for the with statement could be somewhat
useful.  I just quickly wrote some support for the 2 classes that I
used in my project, but I'm certain a similar pattern could be
replicated across the board.  Any stylistic comments are, of course,
welcome.

Cheers,

Michael

=== modified file 'paramiko/client.py'
--- paramiko/client.py	2007-02-11 02:26:13 +0000
+++ paramiko/client.py	2007-03-14 02:59:39 +0000
@@ -409,3 +409,6 @@
     def _log(self, level, msg):
         self._transport._log(level, msg)

+    #Support for "with" statement
+    __enter__ = (lambda self: self)
+    __exit__ = (lambda self, *args: self._transport and self.close())

=== modified file 'paramiko/sftp_client.py'
--- paramiko/sftp_client.py	2006-09-11 17:56:04 +0000
+++ paramiko/sftp_client.py	2007-03-14 02:57:30 +0000
@@ -663,6 +663,9 @@
         if self._cwd == '/':
             return self._cwd + path
         return self._cwd + '/' + path
+    #Support for the "with" statement
+    __enter__ = (lambda self: self)
+    __exit__ = (lambda self, *args: self.sock and self.close())


 class SFTP (SFTPClient):



More information about the paramiko mailing list