[Python-checkins] cpython: Fixes #14314: Improved SMTP timeout handling.
vinay.sajip
python-checkins at python.org
Thu Mar 15 13:02:15 CET 2012
http://hg.python.org/cpython/rev/4b3f81720809
changeset: 75692:4b3f81720809
user: Vinay Sajip <vinay_sajip at yahoo.co.uk>
date: Thu Mar 15 12:02:08 2012 +0000
summary:
Fixes #14314: Improved SMTP timeout handling.
files:
Lib/logging/handlers.py | 7 +++++--
Lib/test/test_logging.py | 3 ++-
2 files changed, 7 insertions(+), 3 deletions(-)
diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py
--- a/Lib/logging/handlers.py
+++ b/Lib/logging/handlers.py
@@ -867,7 +867,7 @@
A handler class which sends an SMTP email for each logging event.
"""
def __init__(self, mailhost, fromaddr, toaddrs, subject,
- credentials=None, secure=None):
+ credentials=None, secure=None, timeout=1.0):
"""
Initialize the handler.
@@ -881,6 +881,8 @@
will be either an empty tuple, or a single-value tuple with the name
of a keyfile, or a 2-value tuple with the names of the keyfile and
certificate file. (This tuple is passed to the `starttls` method).
+ A timeout in seconds can be specified for the SMTP connection (the
+ default is one second).
"""
logging.Handler.__init__(self)
if isinstance(mailhost, tuple):
@@ -897,6 +899,7 @@
self.toaddrs = toaddrs
self.subject = subject
self.secure = secure
+ self.timeout = timeout
def getSubject(self, record):
"""
@@ -919,7 +922,7 @@
port = self.mailport
if not port:
port = smtplib.SMTP_PORT
- smtp = smtplib.SMTP(self.mailhost, port)
+ smtp = smtplib.SMTP(self.mailhost, port, timeout=self.timeout)
msg = self.format(record)
msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\nDate: %s\r\n\r\n%s" % (
self.fromaddr,
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -936,8 +936,9 @@
r = logging.makeLogRecord({'msg': 'Hello'})
self.handled = threading.Event()
h.handle(r)
- self.handled.wait()
+ self.handled.wait(5.0) # 14314: don't wait forever
server.stop()
+ self.assertTrue(self.handled.is_set())
self.assertEqual(len(self.messages), 1)
peer, mailfrom, rcpttos, data = self.messages[0]
self.assertEqual(mailfrom, 'me')
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list