]> granicus.if.org Git - python/commitdiff
#22215: have the smtplib 'quit' command reset the state.
authorR David Murray <rdmurray@bitdance.com>
Sat, 30 Aug 2014 20:56:49 +0000 (16:56 -0400)
committerR David Murray <rdmurray@bitdance.com>
Sat, 30 Aug 2014 20:56:49 +0000 (16:56 -0400)
Without this reset, starttls would fail if a connect/starttls was done after a
quit, because smtplib assumed the existing value of emspt_features was
accurate, but it gets reset when starttls completes (and the new value does
not contain the starttls capability, since tls is already started at that
point).  (There may be additional places where this lack of reset was an
issue as well.)

Patch by Milan Oberkirch.

Lib/smtplib.py
Lib/test/test_smtplib.py
Misc/NEWS

index 7f07840044b4b7fa1b44db57f170e308f175ac02..3e1672ae0b142debfa598bcc3cb4319fdb5bdc73 100755 (executable)
@@ -754,6 +754,10 @@ class SMTP:
     def quit(self):
         """Terminate the SMTP session."""
         res = self.docmd("quit")
+        # A new EHLO is required after reconnecting with connect()
+        self.ehlo_resp = self.helo_resp = None
+        self.esmtp_features = {}
+        self.does_esmtp = False
         self.close()
         return res
 
index aa90eab8570c5c981cd7e5db1a617a633d18a624..a97aa8f80da61f2f549fa658e41d1345062aaa8d 100644 (file)
@@ -507,6 +507,21 @@ class SMTPSimTests(unittest.TestCase):
     #TODO: add tests for correct AUTH method fallback now that the
     #test infrastructure can support it.
 
+    def test_quit_resets_greeting(self):
+        smtp = smtplib.SMTP(HOST, self.port,
+                            local_hostname='localhost',
+                            timeout=15)
+        code, message = smtp.ehlo()
+        self.assertEqual(code, 250)
+        self.assertIn('size', smtp.esmtp_features)
+        smtp.quit()
+        self.assertNotIn('size', smtp.esmtp_features)
+        smtp.connect(HOST, self.port)
+        self.assertNotIn('size', smtp.esmtp_features)
+        smtp.ehlo_or_helo_if_needed()
+        self.assertIn('size', smtp.esmtp_features)
+        smtp.quit()
+
 
 def test_main(verbose=None):
     test_support.run_unittest(GeneralTests, DebuggingServerTests,
index 6ca9692cb1142901c60c9a5c4d4616470b5df28c..5d800347f48454c25cf67cce1480390551a42ce6 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -19,6 +19,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #22216: smtplib now resets its state more completely after a quit.  The
+  most obvious consequence of the previous behavior was a STARTTLS failure
+  during a connect/starttls/quit/connect/starttls sequence.
+
 - Issue #21305: os.urandom now caches a fd to /dev/urandom. This is a PEP 466
   backport from Python 3.