Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Lib/smtplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ class SMTP:
method called 'sendmail' that will do an entire mail transaction.
"""
debuglevel = 0

sock = None
file = None
helo_resp = None
ehlo_msg = "ehlo"
Expand Down Expand Up @@ -344,7 +346,7 @@ def send(self, s):
"""Send `s' to the server."""
if self.debuglevel > 0:
self._print_debug('send:', repr(s))
if hasattr(self, 'sock') and self.sock:
if self.sock:
if isinstance(s, str):
# send is used by the 'data' command, where command_encoding
# should not be used, but 'data' needs to convert the string to
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_smtplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,13 @@ def testNonnumericPort(self):
self.assertRaises(OSError, smtplib.SMTP,
"localhost:bogus")

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


class DefaultArgumentsTests(unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:class:`smtplib.SMTP` objects now always have a `sock` attribute present