8000 LMTP Unix-domain socket assumes global default timeout when timeout n… · python/cpython@b09e617 · GitHub
[go: up one dir, main page]

Skip to content

Commit b09e617

Browse files
author
Ross Rhodes
committed
LMTP Unix-domain socket assumes global default timeout when timeout not provided
1 parent bf64d90 commit b09e617

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

Lib/smtplib.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ def __init__(self, host='', port=LMTP_PORT, local_hostname=None,
10701070
"""Initialize a new instance."""
10711071
super().__init__(host, port, local_hostname=local_hostname,
10721072
source_address=source_address, timeout=timeout)
1073-
1073+
10741074
def connect(self, host='localhost', port=0, source_address=None):
10751075
"""Connect to the LMTP daemon, on either a Unix or a TCP socket."""
10761076
if host[0] != '/':
@@ -1082,7 +1082,9 @@ 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+
1086+
if self.timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
1087+
self.sock.settimeout(self.timeout)
10861088
self.file = None
10871089
self.sock.connect(host)
10881090
except OSError:

Lib/test/mock_socket.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,16 @@ 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)
113116

117+
114118
def create_connection(address, timeout=socket_module._GLOBAL_DEFAULT_TIMEOUT,
115119
source_address=None):
116-
try:
117-
int_port = int(address[1])
118-
except ValueError:
119-
raise error
120120
ms = MockSocket()
121121
if timeout is socket_module._GLOBAL_DEFAULT_TIMEOUT:
122122
timeout = getdefaulttimeout()
@@ -152,8 +152,10 @@ def getaddrinfo(*args, **kw):
152152

153153

154154
# Constants
155+
_GLOBAL_DEFAULT_TIMEOUT = socket_module._GLOBAL_DEFAULT_TIMEOUT
155156
AF_INET = socket_module.AF_INET
156157
AF_INET6 = socket_module.AF_INET6
158+
AF_UNIX = socket_module.AF_UNIX
157159
SOCK_STREAM = socket_module.SOCK_STREAM
158160
SOL_SOCKET = None
159161
SO_REUSEADDR = None

Lib/test/test_smtplib.py

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

166166
client = smtplib.LMTP
167167

168+
def testTimeoutDefault(self):
169+
super().testTimeoutDefault()
170+
local_host = '/some/local/lmtp/delivery/program'
171+
mock_socket.reply_with(b"220 Hello world")
172+
173+
try:
174+
client = self.client(local_host, self.port)
175+
finally:
176+
mock_socket.setdefaulttimeout(None)
177+
178+
self.assertIsNone(client.sock.gettimeout())
179+
client.close()
180+
168181
def testTimeoutZero(self):
169182
super().testTimeoutZero()
170183
local_host = '/some/local/lmtp/delivery/program'
171184
with self.assertRaises(ValueError):
172185
self.client(local_host, timeout=0)
173186

187+
174188
# Test server thread using the specified SMTP server class
175189
def debugging_server(serv, serv_evt, client_evt):
176190
serv_evt.set()

0 commit comments

Comments
 (0)
0