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.
diff --git a/Lib/test/test_imaplib.py b/Lib/test/test_imaplib.py
index 795276e..91aa771 100644
--- a/Lib/test/test_imaplib.py
+++ b/Lib/test/test_imaplib.py
@@ -440,6 +440,29 @@
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: