[Python-checkins] bpo-39259: nntplib.NNTP/NNTP_SSL now reject timeout = 0 (GH-17936)

Victor Stinner webhook-mailer at python.org
Sat Jan 11 12:39:19 EST 2020


https://github.com/python/cpython/commit/1b335ae281631a12201fdec29b3c55d97166fc06
commit: 1b335ae281631a12201fdec29b3c55d97166fc06
branch: master
author: Dong-hee Na <donghee.na92 at gmail.com>
committer: Victor Stinner <vstinner at python.org>
date: 2020-01-11T18:39:15+01:00
summary:

bpo-39259: nntplib.NNTP/NNTP_SSL now reject timeout = 0 (GH-17936)

nntplib.NNTP and nntplib.NNTP_SSL now raise a ValueError
if the given timeout for their constructor is zero to
prevent the creation of a non-blocking socket.

files:
A Misc/NEWS.d/next/Library/2020-01-11-00-32-45.bpo-39259._S5VjC.rst
M Doc/library/nntplib.rst
M Doc/whatsnew/3.9.rst
M Lib/nntplib.py
M Lib/test/test_nntplib.py

diff --git a/Doc/library/nntplib.rst b/Doc/library/nntplib.rst
index e8480b548073a..76973651526a8 100644
--- a/Doc/library/nntplib.rst
+++ b/Doc/library/nntplib.rst
@@ -93,6 +93,10 @@ The module itself defines the following classes:
    .. versionchanged:: 3.3
       Support for the :keyword:`with` statement 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:: NNTP_SSL(host, port=563, user=None, password=None, ssl_context=None, readermode=None, usenetrc=False, [timeout])
 
    Return a new :class:`NNTP_SSL` object, representing an encrypted
@@ -122,6 +126,10 @@ The module itself defines the following classes:
       :attr:`ssl.SSLContext.check_hostname` and *Server Name Indication* (see
       :data:`ssl.HAS_SNI`).
 
+   .. 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.
+
 .. exception:: NNTPError
 
    Derived from the standard exception :exc:`Exception`, this is the base
diff --git a/Doc/whatsnew/3.9.rst b/Doc/whatsnew/3.9.rst
index 3320b7cff42db..8cfb5725bb5e1 100644
--- a/Doc/whatsnew/3.9.rst
+++ b/Doc/whatsnew/3.9.rst
@@ -177,6 +177,13 @@ with this change. The overridden methods of :class:`~imaplib.IMAP4_SSL` and
 :class:`~imaplib.IMAP4_stream` were applied to this change.
 (Contributed by Dong-hee Na in :issue:`38615`.)
 
+nntplib
+-------
+
+:class:`~nntplib.NNTP` and :class:`~nntplib.NNTP_SSL` 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`.)
+
 os
 --
 
diff --git a/Lib/nntplib.py b/Lib/nntplib.py
index 0ab51853b52fd..8951203f325c8 100644
--- a/Lib/nntplib.py
+++ b/Lib/nntplib.py
@@ -1056,6 +1056,8 @@ def __init__(self, host, port=NNTP_PORT, user=None, password=None,
             raise
 
     def _create_socket(self, timeout):
+        if timeout is not None and not timeout:
+            raise ValueError('Non-blocking socket (timeout=0) is not supported')
         sys.audit("nntplib.connect", self, self.host, self.port)
         return socket.create_connection((self.host, self.port), timeout)
 
diff --git a/Lib/test/test_nntplib.py b/Lib/test/test_nntplib.py
index 88c54f4e6f37d..fdd76f9e9b355 100644
--- a/Lib/test/test_nntplib.py
+++ b/Lib/test/test_nntplib.py
@@ -258,6 +258,10 @@ def wrapped(self):
             # value
             setattr(cls, name, wrap_meth(meth))
 
+    def test_timeout(self):
+        with self.assertRaises(ValueError):
+            self.NNTP_CLASS(self.NNTP_HOST, timeout=0, usenetrc=False)
+
     def test_with_statement(self):
         def is_connected():
             if not hasattr(server, 'file'):
diff --git a/Misc/NEWS.d/next/Library/2020-01-11-00-32-45.bpo-39259._S5VjC.rst b/Misc/NEWS.d/next/Library/2020-01-11-00-32-45.bpo-39259._S5VjC.rst
new file mode 100644
index 0000000000000..a454572c80db4
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-01-11-00-32-45.bpo-39259._S5VjC.rst
@@ -0,0 +1,3 @@
+:class:`~nntplib.NNTP` and :class:`~nntplib.NNTP_SSL` 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