[Python-checkins] cpython: Refactor ftplib by getting rid of 4 methods (retr*() and stor*()) which were

giampaolo.rodola python-checkins at python.org
Thu Mar 7 14:03:57 CET 2013


http://hg.python.org/cpython/rev/154d6335a2c6
changeset:   82522:154d6335a2c6
user:        Giampaolo Rodola' <g.rodola at gmail.com>
date:        Thu Mar 07 14:03:27 2013 +0100
summary:
  Refactor ftplib by getting rid of 4 methods (retr*() and stor*()) which were duplicated in TLS_FTP class.

files:
  Lib/ftplib.py |  86 ++++++--------------------------------
  1 files changed, 15 insertions(+), 71 deletions(-)


diff --git a/Lib/ftplib.py b/Lib/ftplib.py
--- a/Lib/ftplib.py
+++ b/Lib/ftplib.py
@@ -439,6 +439,9 @@
                 if not data:
                     break
                 callback(data)
+            # shutdown ssl layer
+            if isinstance(conn, _SSLSocket):
+                conn.unwrap()
         return self.voidresp()
 
     def retrlines(self, cmd, callback = None):
@@ -469,6 +472,9 @@
                 elif line[-1:] == '\n':
                     line = line[:-1]
                 callback(line)
+            # shutdown ssl layer
+            if isinstance(conn, _SSLSocket):
+                conn.unwrap()
         return self.voidresp()
 
     def storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None):
@@ -495,6 +501,9 @@
                 conn.sendall(buf)
                 if callback:
                     callback(buf)
+            # shutdown ssl layer
+            if isinstance(conn, _SSLSocket):
+                conn.unwrap()
         return self.voidresp()
 
     def storlines(self, cmd, fp, callback=None):
@@ -521,6 +530,9 @@
                 conn.sendall(buf)
                 if callback:
                     callback(buf)
+            # shutdown ssl layer
+            if isinstance(conn, _SSLSocket):
+                conn.unwrap()
         return self.voidresp()
 
     def acct(self, password):
@@ -655,8 +667,10 @@
 try:
     import ssl
 except ImportError:
-    pass
+    _SSLSocket = None
 else:
+    _SSLSocket = ssl.SSLSocket
+
     class FTP_TLS(FTP):
         '''A FTP subclass which adds TLS support to FTP as described
         in RFC-4217.
@@ -771,76 +785,6 @@
                                            ssl_version=self.ssl_version)
             return conn, size
 
-        def retrbinary(self, cmd, callback, blocksize=8192, rest=None):
-            self.voidcmd('TYPE I')
-            with self.transfercmd(cmd, rest) as conn:
-                while 1:
-                    data = conn.recv(blocksize)
-                    if not data:
-                        break
-                    callback(data)
-                # shutdown ssl layer
-                if isinstance(conn, ssl.SSLSocket):
-                    conn.unwrap()
-            return self.voidresp()
-
-        def retrlines(self, cmd, callback = None):
-            if callback is None:
-                callback = print_line
-            resp = self.sendcmd('TYPE A')
-            conn = self.transfercmd(cmd)
-            fp = conn.makefile('r', encoding=self.encoding)
-            with fp, conn:
-                while 1:
-                    line = fp.readline()
-                    if self.debugging > 2:
-                        print('*retr*', repr(line))
-                    if not line:
-                        break
-                    if line[-2:] == CRLF:
-                        line = line[:-2]
-                    elif line[-1:] == '\n':
-                        line = line[:-1]
-                    callback(line)
-                # shutdown ssl layer
-                if isinstance(conn, ssl.SSLSocket):
-                    conn.unwrap()
-            return self.voidresp()
-
-        def storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None):
-            self.voidcmd('TYPE I')
-            with self.transfercmd(cmd, rest) as conn:
-                while 1:
-                    buf = fp.read(blocksize)
-                    if not buf:
-                        break
-                    conn.sendall(buf)
-                    if callback:
-                        callback(buf)
-                # shutdown ssl layer
-                if isinstance(conn, ssl.SSLSocket):
-                    conn.unwrap()
-            return self.voidresp()
-
-        def storlines(self, cmd, fp, callback=None):
-            self.voidcmd('TYPE A')
-            with self.transfercmd(cmd) as conn:
-                while 1:
-                    buf = fp.readline()
-                    if not buf:
-                        break
-                    if buf[-2:] != B_CRLF:
-                        if buf[-1] in B_CRLF:
-                            buf = buf[:-1]
-                        buf = buf + B_CRLF
-                    conn.sendall(buf)
-                    if callback:
-                        callback(buf)
-                # shutdown ssl layer
-                if isinstance(conn, ssl.SSLSocket):
-                    conn.unwrap()
-            return self.voidresp()
-
         def abort(self):
             # overridden as we can't pass MSG_OOB flag to sendall()
             line = b'ABOR' + B_CRLF

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list