bpo-38615: Add timeout parameter for IMAP4 and IMAP4_SSL constructor (GH-17203)

imaplib.IMAP4 and imaplib.IMAP4_SSL now have an 
optional *timeout* parameter for their constructors.
Also, the imaplib.IMAP4.open() method now has an optional *timeout* parameter
with this change. The overridden methods of imaplib.IMAP4_SSL and
imaplib.IMAP4_stream were applied to this change.
This commit is contained in:
Dong-hee Na 2020-01-08 02:28:10 +09:00 committed by Victor Stinner
parent 950c6795aa
commit 13a7ee8d62
5 changed files with 90 additions and 25 deletions

View file

@ -440,6 +440,29 @@ class NewIMAPTestsMixin():
with self.imap_class(*server.server_address):
pass
def test_imaplib_timeout_test(self):
_, server = self._setup(SimpleIMAPHandler)
addr = server.server_address[1]
client = self.imap_class("localhost", addr, timeout=None)
self.assertEqual(client.sock.timeout, None)
client.shutdown()
client = self.imap_class("localhost", addr, timeout=support.LOOPBACK_TIMEOUT)
self.assertEqual(client.sock.timeout, support.LOOPBACK_TIMEOUT)
client.shutdown()
with self.assertRaises(ValueError):
client = self.imap_class("localhost", addr, timeout=0)
def test_imaplib_timeout_functionality_test(self):
class TimeoutHandler(SimpleIMAPHandler):
def handle(self):
time.sleep(1)
SimpleIMAPHandler.handle(self)
_, server = self._setup(TimeoutHandler)
addr = server.server_address[1]
with self.assertRaises(socket.timeout):
client = self.imap_class("localhost", addr, timeout=0.001)
def test_with_statement(self):
_, server = self._setup(SimpleIMAPHandler, connect=False)
with self.imap_class(*server.server_address) as imap: