Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c39b52f

Browse files
corona10vstinner
authored andcommitted
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.
1 parent 4c53e63 commit c39b52f

5 files changed

Lines changed: 26 additions & 6 deletions

File tree

Doc/library/poplib.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ The :mod:`poplib` module provides two classes:
4747
``poplib.putline`` with arguments ``self`` and ``line``,
4848
where ``line`` is the bytes about to be sent to the remote host.
4949

50+
.. versionchanged:: 3.9
51+
If the *timeout* parameter is set to be zero, it will raise a
52+
:class:`ValueError` to prevent the creation of a non-blocking socket.
5053

5154
.. class:: POP3_SSL(host, port=POP3_SSL_PORT, keyfile=None, certfile=None, timeout=None, context=None)
5255

@@ -85,6 +88,10 @@ The :mod:`poplib` module provides two classes:
8588
:func:`ssl.create_default_context` select the system's trusted CA
8689
certificates for you.
8790

91+
.. versionchanged:: 3.9
92+
If the *timeout* parameter is set to be zero, it will raise a
93+
:class:`ValueError` to prevent the creation of a non-blocking socket.
94+
8895
One exception is defined as an attribute of the :mod:`poplib` module:
8996

9097

@@ -268,4 +275,3 @@ retrieves and prints all messages::
268275

269276
At the end of the module, there is a test section that contains a more extensive
270277
example of usage.
271-

Doc/whatsnew/3.9.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ Exposed the Linux-specific :func:`os.pidfd_open` (:issue:`38692`) and
187187
:data:`os.P_PIDFD` (:issue:`38713`) for process management with file
188188
descriptors.
189189

190+
poplib
191+
------
192+
193+
:class:`~poplib.POP3` and :class:`~poplib.POP3_SSL` now raise a :class:`ValueError`
194+
if the given timeout for their constructor is zero to prevent the creation of
195+
a non-blocking socket. (Contributed by Dong-hee Na in :issue:`39259`.)
196+
190197
threading
191198
---------
192199

Lib/poplib.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ def __init__(self, host, port=POP3_PORT,
107107
self.welcome = self._getresp()
108108

109109
def _create_socket(self, timeout):
110+
if timeout is not None and not timeout:
111+
raise ValueError('Non-blocking socket (timeout=0) is not supported')
110112
return socket.create_connection((self.host, self.port), timeout)
111113

112114
def _putline(self, line):

Lib/test/test_poplib.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ def setUp(self):
481481
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
482482
self.sock.settimeout(60) # Safety net. Look issue 11812
483483
self.port = test_support.bind_port(self.sock)
484-
self.thread = threading.Thread(target=self.server, args=(self.evt,self.sock))
484+
self.thread = threading.Thread(target=self.server, args=(self.evt, self.sock))
485485
self.thread.daemon = True
486486
self.thread.start()
487487
self.evt.wait()
@@ -505,12 +505,12 @@ def server(self, evt, serv):
505505

506506
def testTimeoutDefault(self):
507507
self.assertIsNone(socket.getdefaulttimeout())
508-
socket.setdefaulttimeout(30)
508+
socket.setdefaulttimeout(test_support.LOOPBACK_TIMEOUT)
509509
try:
510510
pop = poplib.POP3(HOST, self.port)
511511
finally:
512512
socket.setdefaulttimeout(None)
513-
self.assertEqual(pop.sock.gettimeout(), 30)
513+
self.assertEqual(pop.sock.gettimeout(), test_support.LOOPBACK_TIMEOUT)
514514
pop.close()
515515

516516
def testTimeoutNone(self):
@@ -524,9 +524,11 @@ def testTimeoutNone(self):
524524
pop.close()
525525

526526
def testTimeoutValue(self):
527-
pop = poplib.POP3(HOST, self.port, timeout=30)
528-
self.assertEqual(pop.sock.gettimeout(), 30)
527+
pop = poplib.POP3(HOST, self.port, timeout=test_support.LOOPBACK_TIMEOUT)
528+
self.assertEqual(pop.sock.gettimeout(), test_support.LOOPBACK_TIMEOUT)
529529
pop.close()
530+
with self.assertRaises(ValueError):
531+
poplib.POP3(HOST, self.port, timeout=0)
530532

531533

532534
def test_main():
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:class:`~poplib.POP3` and :class:`~poplib.POP3_SSL` 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)