[Python-checkins] [3.9] bpo-42756: Configure LMTP Unix-domain socket to use global default timeout when timeout not provided (GH-23969) (GH-24050)

corona10 webhook-mailer at python.org
Fri Jan 1 22:44:13 EST 2021


https://github.com/python/cpython/commit/69120613c071e9327a9dc6e4b1ff21b2e94d885e
commit: 69120613c071e9327a9dc6e4b1ff21b2e94d885e
branch: 3.9
author: Ross <rrhodes at users.noreply.github.com>
committer: corona10 <donghee.na92 at gmail.com>
date: 2021-01-02T12:44:04+09:00
summary:

[3.9] bpo-42756: Configure LMTP Unix-domain socket to use global default timeout when timeout not provided (GH-23969) (GH-24050)

files:
A Misc/NEWS.d/next/Library/2020-12-27-21-22-01.bpo-42756.dHMPJ9.rst
M Lib/smtplib.py
M Lib/test/mock_socket.py
M Lib/test/test_smtplib.py

diff --git a/Lib/smtplib.py b/Lib/smtplib.py
index 7808ba01cba88..f0472317de919 100755
--- a/Lib/smtplib.py
+++ b/Lib/smtplib.py
@@ -1082,7 +1082,8 @@ def connect(self, host='localhost', port=0, source_address=None):
         # Handle Unix-domain sockets.
         try:
             self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
-            self.sock.settimeout(self.timeout)
+            if self.timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
+                self.sock.settimeout(self.timeout)
             self.file = None
             self.sock.connect(host)
         except OSError:
diff --git a/Lib/test/mock_socket.py b/Lib/test/mock_socket.py
index cda4db25cba59..c7abddcf5fafd 100644
--- a/Lib/test/mock_socket.py
+++ b/Lib/test/mock_socket.py
@@ -107,6 +107,9 @@ def getpeername(self):
     def close(self):
         pass
 
+    def connect(self, host):
+        pass
+
 
 def socket(family=None, type=None, proto=None):
     return MockSocket(family)
@@ -152,8 +155,12 @@ def getaddrinfo(*args, **kw):
 
 
 # Constants
+_GLOBAL_DEFAULT_TIMEOUT = socket_module._GLOBAL_DEFAULT_TIMEOUT
 AF_INET = socket_module.AF_INET
 AF_INET6 = socket_module.AF_INET6
 SOCK_STREAM = socket_module.SOCK_STREAM
 SOL_SOCKET = None
 SO_REUSEADDR = None
+
+if hasattr(socket_module, 'AF_UNIX'):
+    AF_UNIX = socket_module.AF_UNIX
diff --git a/Lib/test/test_smtplib.py b/Lib/test/test_smtplib.py
index 576299900318d..3451f3a411e9a 100644
--- a/Lib/test/test_smtplib.py
+++ b/Lib/test/test_smtplib.py
@@ -165,6 +165,17 @@ class LMTPGeneralTests(GeneralTests, unittest.TestCase):
 
     client = smtplib.LMTP
 
+    @unittest.skipUnless(hasattr(socket, 'AF_UNIX'), "test requires Unix domain socket")
+    def testUnixDomainSocketTimeoutDefault(self):
+        local_host = '/some/local/lmtp/delivery/program'
+        mock_socket.reply_with(b"220 Hello world")
+        try:
+            client = self.client(local_host, self.port)
+        finally:
+            mock_socket.setdefaulttimeout(None)
+        self.assertIsNone(client.sock.gettimeout())
+        client.close()
+
     def testTimeoutZero(self):
         super().testTimeoutZero()
         local_host = '/some/local/lmtp/delivery/program'
diff --git a/Misc/NEWS.d/next/Library/2020-12-27-21-22-01.bpo-42756.dHMPJ9.rst b/Misc/NEWS.d/next/Library/2020-12-27-21-22-01.bpo-42756.dHMPJ9.rst
new file mode 100644
index 0000000000000..93a0bb010df2b
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-12-27-21-22-01.bpo-42756.dHMPJ9.rst
@@ -0,0 +1,2 @@
+Configure LMTP Unix-domain socket to use socket global default timeout when
+a timeout is not explicitly provided.



More information about the Python-checkins mailing list