Skip to content

Commit 7b31397

Browse files
romualdgiampaolo
authored andcommitted
bpo-32680 add default "sock" on SMTP objects (#5345)
By default the smtplib.SMTP objects did not have a sock attribute, it was only created during connect()
1 parent 2b2758d commit 7b31397

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

Lib/smtplib.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ class SMTP:
216216
method called 'sendmail' that will do an entire mail transaction.
217217
"""
218218
debuglevel = 0
219+
220+
sock = None
219221
file = None
220222
helo_resp = None
221223
ehlo_msg = "ehlo"
@@ -344,7 +346,7 @@ def send(self, s):
344346
"""Send `s' to the server."""
345347
if self.debuglevel > 0:
346348
self._print_debug('send:', repr(s))
347-
if hasattr(self, 'sock') and self.sock:
349+
if self.sock:
348350
if isinstance(s, str):
349351
# send is used by the 'data' command, where command_encoding
350352
# should not be used, but 'data' needs to convert the string to

Lib/test/test_smtplib.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,13 @@ def testNonnumericPort(self):
602602
self.assertRaises(OSError, smtplib.SMTP,
603603
"localhost:bogus")
604604

605+
def testSockAttributeExists(self):
606+
# check that sock attribute is present outside of a connect() call
607+
# (regression test, the previous behavior raised an
608+
# AttributeError: 'SMTP' object has no attribute 'sock')
609+
with smtplib.SMTP() as smtp:
610+
self.assertIsNone(smtp.sock)
611+
605612

606613
class DefaultArgumentsTests(unittest.TestCase):
607614

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:class:`smtplib.SMTP` objects now always have a `sock` attribute present

0 commit comments

Comments
 (0)