Kaydet (Commit) 7b313971 authored tarafından Romuald Brunet's avatar Romuald Brunet Kaydeden (comit) Giampaolo Rodola

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()
üst 2b2758d0
...@@ -216,6 +216,8 @@ class SMTP: ...@@ -216,6 +216,8 @@ class SMTP:
method called 'sendmail' that will do an entire mail transaction. method called 'sendmail' that will do an entire mail transaction.
""" """
debuglevel = 0 debuglevel = 0
sock = None
file = None file = None
helo_resp = None helo_resp = None
ehlo_msg = "ehlo" ehlo_msg = "ehlo"
...@@ -344,7 +346,7 @@ class SMTP: ...@@ -344,7 +346,7 @@ class SMTP:
"""Send `s' to the server.""" """Send `s' to the server."""
if self.debuglevel > 0: if self.debuglevel > 0:
self._print_debug('send:', repr(s)) self._print_debug('send:', repr(s))
if hasattr(self, 'sock') and self.sock: if self.sock:
if isinstance(s, str): if isinstance(s, str):
# send is used by the 'data' command, where command_encoding # send is used by the 'data' command, where command_encoding
# should not be used, but 'data' needs to convert the string to # should not be used, but 'data' needs to convert the string to
......
...@@ -602,6 +602,13 @@ class NonConnectingTests(unittest.TestCase): ...@@ -602,6 +602,13 @@ class NonConnectingTests(unittest.TestCase):
self.assertRaises(OSError, smtplib.SMTP, self.assertRaises(OSError, smtplib.SMTP,
"localhost:bogus") "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): class DefaultArgumentsTests(unittest.TestCase):
......
:class:`smtplib.SMTP` objects now always have a `sock` attribute present
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment