[Python-checkins] bpo-39259: ftplib.FTP/FTP_TLS now reject timeout = 0 (GH-17959)

Victor Stinner webhook-mailer at python.org
Mon Jan 13 14:34:41 EST 2020


https://github.com/python/cpython/commit/a190e2ade1a704a6b5a94464a0a19b140c7dd031
commit: a190e2ade1a704a6b5a94464a0a19b140c7dd031
branch: master
author: Dong-hee Na <donghee.na92 at gmail.com>
committer: Victor Stinner <vstinner at python.org>
date: 2020-01-13T20:34:34+01:00
summary:

bpo-39259: ftplib.FTP/FTP_TLS now reject timeout = 0 (GH-17959)

files:
A Misc/NEWS.d/next/Library/2020-01-12-17-19-40.bpo-39259.iax06r.rst
M Doc/library/ftplib.rst
M Doc/whatsnew/3.9.rst
M Lib/ftplib.py
M Lib/test/test_ftplib.py

diff --git a/Doc/library/ftplib.rst b/Doc/library/ftplib.rst
index 79a0286fb544d..a4bb695a9ab10 100644
--- a/Doc/library/ftplib.rst
+++ b/Doc/library/ftplib.rst
@@ -72,6 +72,9 @@ The module defines the following items:
    .. versionchanged:: 3.3
       *source_address* parameter was added.
 
+   .. versionchanged:: 3.9
+      If the *timeout* parameter is set to be zero, it will raise a
+      :class:`ValueError` to prevent the creation of a non-blocking socket
 
 .. class:: FTP_TLS(host='', user='', passwd='', acct='', keyfile=None, certfile=None, context=None, timeout=None, source_address=None)
 
@@ -105,6 +108,10 @@ The module defines the following items:
        :func:`ssl.create_default_context` select the system's trusted CA
        certificates for you.
 
+   .. versionchanged:: 3.9
+      If the *timeout* parameter is set to be zero, it will raise a
+      :class:`ValueError` to prevent the creation of a non-blocking socket
+
    Here's a sample session using the :class:`FTP_TLS` class::
 
       >>> ftps = FTP_TLS('ftp.pureftpd.org')
diff --git a/Doc/whatsnew/3.9.rst b/Doc/whatsnew/3.9.rst
index b6ffa23453626..859bf440f89af 100644
--- a/Doc/whatsnew/3.9.rst
+++ b/Doc/whatsnew/3.9.rst
@@ -159,6 +159,13 @@ Added constants :data:`~fcntl.F_OFD_GETLK`, :data:`~fcntl.F_OFD_SETLK`
 and :data:`~fcntl.F_OFD_SETLKW`.
 (Contributed by Dong-hee Na in :issue:`38602`.)
 
+ftplib
+-------
+
+:class:`~ftplib.FTP` and :class:`~ftplib.FTP_TLS` now raise a :class:`ValueError`
+if the given timeout for their constructor is zero to prevent the creation of
+a non-blocking socket. (Contributed by Dong-hee Na in :issue:`39259`.)
+
 gc
 --
 
diff --git a/Lib/ftplib.py b/Lib/ftplib.py
index c339eb25bc2d7..71b3c289551fd 100644
--- a/Lib/ftplib.py
+++ b/Lib/ftplib.py
@@ -146,6 +146,8 @@ def connect(self, host='', port=0, timeout=-999, source_address=None):
             self.port = port
         if timeout != -999:
             self.timeout = timeout
+        if self.timeout is not None and not self.timeout:
+            raise ValueError('Non-blocking socket (timeout=0) is not supported')
         if source_address is not None:
             self.source_address = source_address
         sys.audit("ftplib.connect", self, self.host, self.port)
@@ -725,12 +727,12 @@ def __init__(self, host='', user='', passwd='', acct='', keyfile=None,
                                                      keyfile=keyfile)
             self.context = context
             self._prot_p = False
-            FTP.__init__(self, host, user, passwd, acct, timeout, source_address)
+            super().__init__(host, user, passwd, acct, timeout, source_address)
 
         def login(self, user='', passwd='', acct='', secure=True):
             if secure and not isinstance(self.sock, ssl.SSLSocket):
                 self.auth()
-            return FTP.login(self, user, passwd, acct)
+            return super().login(user, passwd, acct)
 
         def auth(self):
             '''Set up secure control connection by using TLS/SSL.'''
@@ -740,8 +742,7 @@ def auth(self):
                 resp = self.voidcmd('AUTH TLS')
             else:
                 resp = self.voidcmd('AUTH SSL')
-            self.sock = self.context.wrap_socket(self.sock,
-                                                 server_hostname=self.host)
+            self.sock = self.context.wrap_socket(self.sock, server_hostname=self.host)
             self.file = self.sock.makefile(mode='r', encoding=self.encoding)
             return resp
 
@@ -778,7 +779,7 @@ def prot_c(self):
         # --- Overridden FTP methods
 
         def ntransfercmd(self, cmd, rest=None):
-            conn, size = FTP.ntransfercmd(self, cmd, rest)
+            conn, size = super().ntransfercmd(cmd, rest)
             if self._prot_p:
                 conn = self.context.wrap_socket(conn,
                                                 server_hostname=self.host)
diff --git a/Lib/test/test_ftplib.py b/Lib/test/test_ftplib.py
index 62f673c06156a..f40f3a4d9f7ab 100644
--- a/Lib/test/test_ftplib.py
+++ b/Lib/test/test_ftplib.py
@@ -1045,6 +1045,10 @@ def testTimeoutValue(self):
         self.evt.wait()
         ftp.close()
 
+        # bpo-39259
+        with self.assertRaises(ValueError):
+            ftplib.FTP(HOST, timeout=0)
+
     def testTimeoutConnect(self):
         ftp = ftplib.FTP()
         ftp.connect(HOST, timeout=30)
diff --git a/Misc/NEWS.d/next/Library/2020-01-12-17-19-40.bpo-39259.iax06r.rst b/Misc/NEWS.d/next/Library/2020-01-12-17-19-40.bpo-39259.iax06r.rst
new file mode 100644
index 0000000000000..bfcaff3c3d006
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-01-12-17-19-40.bpo-39259.iax06r.rst
@@ -0,0 +1,3 @@
+:class:`~ftplib.FTP_TLS` and :class:`~ftplib.FTP_TLS` now raise a
+:class:`ValueError` if the given timeout for their constructor is zero to
+prevent the creation of a non-blocking socket. Patch by Dong-hee Na.



More information about the Python-checkins mailing list