Skip to content

Commit 3bf0532

Browse files
authored
bpo-42756: Configure LMTP Unix-domain socket to use global default timeout when timeout not provided (GH-23969)
1 parent de6f20a commit 3bf0532

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

Lib/smtplib.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,8 @@ def connect(self, host='localhost', port=0, source_address=None):
10821082
# Handle Unix-domain sockets.
10831083
try:
10841084
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
1085-
self.sock.settimeout(self.timeout)
1085+
if self.timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
1086+
self.sock.settimeout(self.timeout)
10861087
self.file = None
10871088
self.sock.connect(host)
10881089
except OSError:

Lib/test/mock_socket.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ def getpeername(self):
107107
def close(self):
108108
pass
109109

110+
def connect(self, host):
111+
pass
112+
110113

111114
def socket(family=None, type=None, proto=None):
112115
return MockSocket(family)
@@ -152,8 +155,12 @@ def getaddrinfo(*args, **kw):
152155

153156

154157
# Constants
158+
_GLOBAL_DEFAULT_TIMEOUT = socket_module._GLOBAL_DEFAULT_TIMEOUT
155159
AF_INET = socket_module.AF_INET
156160
AF_INET6 = socket_module.AF_INET6
157161
SOCK_STREAM = socket_module.SOCK_STREAM
158162
SOL_SOCKET = None
159163
SO_REUSEADDR = None
164+
165+
if hasattr(socket_module, 'AF_UNIX'):
166+
AF_UNIX = socket_module.AF_UNIX

Lib/test/test_smtplib.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,17 @@ class LMTPGeneralTests(GeneralTests, unittest.TestCase):
165165

166166
client = smtplib.LMTP
167167

168+
@unittest.skipUnless(hasattr(socket, 'AF_UNIX'), "test requires Unix domain socket")
169+
def testUnixDomainSocketTimeoutDefault(self):
170+
local_host = '/some/local/lmtp/delivery/program'
171+
mock_socket.reply_with(b"220 Hello world")
172+
try:
173+
client = self.client(local_host, self.port)
174+
finally:
175+
mock_socket.setdefaulttimeout(None)
176+
self.assertIsNone(client.sock.gettimeout())
177+
client.close()
178+
168179
def testTimeoutZero(self):
169180
super().testTimeoutZero()
170181
local_host = '/some/local/lmtp/delivery/program'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Configure LMTP Unix-domain socket to use socket global default timeout when
2+
a timeout is not explicitly provided.

0 commit comments

Comments
 (0)