cpython (2.7): #22215: have the smtplib 'quit' command reset the state.

http://hg.python.org/cpython/rev/7288519594de changeset: 92273:7288519594de branch: 2.7 parent: 92261:3e7f88550788 user: R David Murray <rdmurray@bitdance.com> date: Sat Aug 30 16:56:49 2014 -0400 summary: #22215: have the smtplib 'quit' command reset the state. 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. files: Lib/smtplib.py | 4 ++++ Lib/test/test_smtplib.py | 15 +++++++++++++++ Misc/NEWS | 4 ++++ 3 files changed, 23 insertions(+), 0 deletions(-) diff --git a/Lib/smtplib.py b/Lib/smtplib.py --- a/Lib/smtplib.py +++ b/Lib/smtplib.py @@ -754,6 +754,10 @@ 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 diff --git a/Lib/test/test_smtplib.py b/Lib/test/test_smtplib.py --- a/Lib/test/test_smtplib.py +++ b/Lib/test/test_smtplib.py @@ -507,6 +507,21 @@ #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, diff --git a/Misc/NEWS b/Misc/NEWS --- a/Misc/NEWS +++ b/Misc/NEWS @@ -19,6 +19,10 @@ 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. -- Repository URL: http://hg.python.org/cpython
participants (1)
-
r.david.murray