Skip to content

Commit a190e2a

Browse files
corona10vstinner
authored andcommitted
bpo-39259: ftplib.FTP/FTP_TLS now reject timeout = 0 (GH-17959)
1 parent 31d6de5 commit a190e2a

File tree

5 files changed

+27
-5
lines changed

5 files changed

+27
-5
lines changed

Doc/library/ftplib.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ The module defines the following items:
7272
.. versionchanged:: 3.3
7373
*source_address* parameter was added.
7474

75+
.. versionchanged:: 3.9
76+
If the *timeout* parameter is set to be zero, it will raise a
77+
:class:`ValueError` to prevent the creation of a non-blocking socket
7578

7679
.. class:: FTP_TLS(host='', user='', passwd='', acct='', keyfile=None, certfile=None, context=None, timeout=None, source_address=None)
7780

@@ -105,6 +108,10 @@ The module defines the following items:
105108
:func:`ssl.create_default_context` select the system's trusted CA
106109
certificates for you.
107110

111+
.. versionchanged:: 3.9
112+
If the *timeout* parameter is set to be zero, it will raise a
113+
:class:`ValueError` to prevent the creation of a non-blocking socket
114+
108115
Here's a sample session using the :class:`FTP_TLS` class::
109116

110117
>>> ftps = FTP_TLS('ftp.pureftpd.org')

Doc/whatsnew/3.9.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ Added constants :data:`~fcntl.F_OFD_GETLK`, :data:`~fcntl.F_OFD_SETLK`
159159
and :data:`~fcntl.F_OFD_SETLKW`.
160160
(Contributed by Dong-hee Na in :issue:`38602`.)
161161

162+
ftplib
163+
-------
164+
165+
:class:`~ftplib.FTP` and :class:`~ftplib.FTP_TLS` now raise a :class:`ValueError`
166+
if the given timeout for their constructor is zero to prevent the creation of
167+
a non-blocking socket. (Contributed by Dong-hee Na in :issue:`39259`.)
168+
162169
gc
163170
--
164171

Lib/ftplib.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ def connect(self, host='', port=0, timeout=-999, source_address=None):
146146
self.port = port
147147
if timeout != -999:
148148
self.timeout = timeout
149+
if self.timeout is not None and not self.timeout:
150+
raise ValueError('Non-blocking socket (timeout=0) is not supported')
149151
if source_address is not None:
150152
self.source_address = source_address
151153
sys.audit("ftplib.connect", self, self.host, self.port)
@@ -725,12 +727,12 @@ def __init__(self, host='', user='', passwd='', acct='', keyfile=None,
725727
keyfile=keyfile)
726728
self.context = context
727729
self._prot_p = False
728-
FTP.__init__(self, host, user, passwd, acct, timeout, source_address)
730+
super().__init__(host, user, passwd, acct, timeout, source_address)
729731

730732
def login(self, user='', passwd='', acct='', secure=True):
731733
if secure and not isinstance(self.sock, ssl.SSLSocket):
732734
self.auth()
733-
return FTP.login(self, user, passwd, acct)
735+
return super().login(user, passwd, acct)
734736

735737
def auth(self):
736738
'''Set up secure control connection by using TLS/SSL.'''
@@ -740,8 +742,7 @@ def auth(self):
740742
resp = self.voidcmd('AUTH TLS')
741743
else:
742744
resp = self.voidcmd('AUTH SSL')
743-
self.sock = self.context.wrap_socket(self.sock,
744-
server_hostname=self.host)
745+
self.sock = self.context.wrap_socket(self.sock, server_hostname=self.host)
745746
self.file = self.sock.makefile(mode='r', encoding=self.encoding)
746747
return resp
747748

@@ -778,7 +779,7 @@ def prot_c(self):
778779
# --- Overridden FTP methods
779780

780781
def ntransfercmd(self, cmd, rest=None):
781-
conn, size = FTP.ntransfercmd(self, cmd, rest)
782+
conn, size = super().ntransfercmd(cmd, rest)
782783
if self._prot_p:
783784
conn = self.context.wrap_socket(conn,
784785
server_hostname=self.host)

Lib/test/test_ftplib.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,10 @@ def testTimeoutValue(self):
10451045
self.evt.wait()
10461046
ftp.close()
10471047

1048+
# bpo-39259
1049+
with self.assertRaises(ValueError):
1050+
ftplib.FTP(HOST, timeout=0)
1051+
10481052
def testTimeoutConnect(self):
10491053
ftp = ftplib.FTP()
10501054
ftp.connect(HOST, timeout=30)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:class:`~ftplib.FTP_TLS` and :class:`~ftplib.FTP_TLS` now raise a
2+
:class:`ValueError` if the given timeout for their constructor is zero to
3+
prevent the creation of a non-blocking socket. Patch by Dong-hee Na.

0 commit comments

Comments
 (0)