Code

Ticket #19134: 19134-with-test.diff

File 19134-with-test.diff, 1.3 KB (added by hongshuning, 19 months ago)
Line 
1diff --git a/django/core/mail/backends/smtp.py b/django/core/mail/backends/smtp.py
2index b6f7f56..baffa8f 100644
3--- a/django/core/mail/backends/smtp.py
4+++ b/django/core/mail/backends/smtp.py
5@@ -63,9 +63,10 @@ class EmailBackend(BaseEmailBackend):
6         try:
7             try:
8                 self.connection.quit()
9-            except socket.sslerror:
10+            except (socket.sslerror, smtplib.SMTPServerDisconnected):
11                 # This happens when calling quit() on a TLS connection
12-                # sometimes.
13+                # sometimes, or when the connection was already disconnected
14+                # by the server.
15                 self.connection.close()
16             except:
17                 if self.fail_silently:
18diff --git a/tests/regressiontests/mail/tests.py b/tests/regressiontests/mail/tests.py
19index a8fbf20..061c3f5 100644
20--- a/tests/regressiontests/mail/tests.py
21+++ b/tests/regressiontests/mail/tests.py
22@@ -678,8 +678,13 @@ class SMTPBackendTests(BaseEmailBackendTests, TestCase):
23 
24     @classmethod
25     def tearDownClass(cls):
26+        # for Ticket #19134
27+        backend = smtp.EmailBackend()
28+        backend.username = None
29+        backend.open()
30         cls._settings_override.disable()
31         cls.server.stop()
32+        backend.close()
33 
34     def setUp(self):
35         super(SMTPBackendTests, self).setUp()