[Python-checkins] bpo-39259: poplib now rejects timeout = 0 (GH-17912)

Victor Stinner webhook-mailer at python.org
Fri Jan 10 09:34:12 EST 2020


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

bpo-39259: poplib now rejects timeout = 0 (GH-17912)

poplib.POP3 and poplib.POP3_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-09-10-58-58.bpo-39259.RmDgCC.rst
M Doc/library/poplib.rst
M Doc/whatsnew/3.9.rst
M Lib/poplib.py
M Lib/test/test_poplib.py

diff --git a/Doc/library/poplib.rst b/Doc/library/poplib.rst
index 28b42fa60c185..2f349b35b7e0f 100644
--- a/Doc/library/poplib.rst
+++ b/Doc/library/poplib.rst
@@ -47,6 +47,9 @@ The :mod:`poplib` module provides two classes:
       ``poplib.putline`` with arguments ``self`` and ``line``,
       where ``line`` is the bytes about to be sent to the remote host.
 
+   .. 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:: POP3_SSL(host, port=POP3_SSL_PORT, keyfile=None, certfile=None, timeout=None, context=None)
 
@@ -85,6 +88,10 @@ The :mod:`poplib` module provides two classes:
        :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.
+
 One exception is defined as an attribute of the :mod:`poplib` module:
 
 
@@ -268,4 +275,3 @@ retrieves and prints all messages::
 
 At the end of the module, there is a test section that contains a more extensive
 example of usage.
-
diff --git a/Doc/whatsnew/3.9.rst b/Doc/whatsnew/3.9.rst
index ea6d8f515a944..3320b7cff42db 100644
--- a/Doc/whatsnew/3.9.rst
+++ b/Doc/whatsnew/3.9.rst
@@ -187,6 +187,13 @@ Exposed the Linux-specific :func:`os.pidfd_open` (:issue:`38692`) and
 :data:`os.P_PIDFD` (:issue:`38713`) for process management with file
 descriptors.
 
+poplib
+------
+
+:class:`~poplib.POP3` and :class:`~poplib.POP3_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`.)
+
 threading
 ---------
 
diff --git a/Lib/poplib.py b/Lib/poplib.py
index 0b6750d230376..0f8587317c2bb 100644
--- a/Lib/poplib.py
+++ b/Lib/poplib.py
@@ -107,6 +107,8 @@ def __init__(self, host, port=POP3_PORT,
         self.welcome = self._getresp()
 
     def _create_socket(self, timeout):
+        if timeout is not None and not timeout:
+            raise ValueError('Non-blocking socket (timeout=0) is not supported')
         return socket.create_connection((self.host, self.port), timeout)
 
     def _putline(self, line):
diff --git a/Lib/test/test_poplib.py b/Lib/test/test_poplib.py
index 911cba1f1dcd4..7f06d1950e1e1 100644
--- a/Lib/test/test_poplib.py
+++ b/Lib/test/test_poplib.py
@@ -481,7 +481,7 @@ def setUp(self):
         self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         self.sock.settimeout(60)  # Safety net. Look issue 11812
         self.port = test_support.bind_port(self.sock)
-        self.thread = threading.Thread(target=self.server, args=(self.evt,self.sock))
+        self.thread = threading.Thread(target=self.server, args=(self.evt, self.sock))
         self.thread.daemon = True
         self.thread.start()
         self.evt.wait()
@@ -505,12 +505,12 @@ def server(self, evt, serv):
 
     def testTimeoutDefault(self):
         self.assertIsNone(socket.getdefaulttimeout())
-        socket.setdefaulttimeout(30)
+        socket.setdefaulttimeout(test_support.LOOPBACK_TIMEOUT)
         try:
             pop = poplib.POP3(HOST, self.port)
         finally:
             socket.setdefaulttimeout(None)
-        self.assertEqual(pop.sock.gettimeout(), 30)
+        self.assertEqual(pop.sock.gettimeout(), test_support.LOOPBACK_TIMEOUT)
         pop.close()
 
     def testTimeoutNone(self):
@@ -524,9 +524,11 @@ def testTimeoutNone(self):
         pop.close()
 
     def testTimeoutValue(self):
-        pop = poplib.POP3(HOST, self.port, timeout=30)
-        self.assertEqual(pop.sock.gettimeout(), 30)
+        pop = poplib.POP3(HOST, self.port, timeout=test_support.LOOPBACK_TIMEOUT)
+        self.assertEqual(pop.sock.gettimeout(), test_support.LOOPBACK_TIMEOUT)
         pop.close()
+        with self.assertRaises(ValueError):
+            poplib.POP3(HOST, self.port, timeout=0)
 
 
 def test_main():
diff --git a/Misc/NEWS.d/next/Library/2020-01-09-10-58-58.bpo-39259.RmDgCC.rst b/Misc/NEWS.d/next/Library/2020-01-09-10-58-58.bpo-39259.RmDgCC.rst
new file mode 100644
index 0000000000000..c7ef8be7e3adb
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-01-09-10-58-58.bpo-39259.RmDgCC.rst
@@ -0,0 +1,3 @@
+:class:`~poplib.POP3` and :class:`~poplib.POP3_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