Skip to content

bpo-42756: Configure LMTP Unix-domain socket to use global default timeout when timeout not provided #23969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 1, 2021
Merged
3 changes: 2 additions & 1 deletion Lib/smtplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/mock_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
11 changes: 11 additions & 0 deletions Lib/test/test_smtplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Configure LMTP Unix-domain socket to use socket global default timeout when
a timeout is not explicitly provided.