Fixed #27368 -- Modifed BaseEmailBackend.__enter__() to close the connection if an exception occurs.

Fixes unclosed socket ResourceWarning in mail test.

Thanks Claude Paroz for the review.
This commit is contained in:
Jon Dufresne 2016-10-21 05:59:07 -07:00 committed by GitHub
parent 1f5b69917d
commit 9b9c8c4a81
2 changed files with 16 additions and 17 deletions

View File

@ -40,7 +40,11 @@ class BaseEmailBackend(object):
pass pass
def __enter__(self): def __enter__(self):
self.open() try:
self.open()
except Exception:
self.close()
raise
return self return self
def __exit__(self, exc_type, exc_value, traceback): def __exit__(self, exc_type, exc_value, traceback):

View File

@ -1289,11 +1289,9 @@ class SMTPBackendTests(BaseEmailBackendTests, SMTPBackendTestsBase):
""" """
backend = smtp.EmailBackend( backend = smtp.EmailBackend(
username='not empty username', password='not empty password') username='not empty username', password='not empty password')
try: with self.assertRaisesMessage(SMTPException, 'SMTP AUTH extension not supported by server.'):
with self.assertRaisesMessage(SMTPException, 'SMTP AUTH extension not supported by server.'): with backend:
backend.open() pass
finally:
backend.close()
def test_server_open(self): def test_server_open(self):
""" """
@ -1315,7 +1313,8 @@ class SMTPBackendTests(BaseEmailBackendTests, SMTPBackendTestsBase):
backend = CustomEmailBackend(username='username', password='password') backend = CustomEmailBackend(username='username', password='password')
with self.assertRaises(SMTPAuthenticationError): with self.assertRaises(SMTPAuthenticationError):
backend.open() with backend:
pass
@override_settings(EMAIL_USE_TLS=True) @override_settings(EMAIL_USE_TLS=True)
def test_email_tls_use_settings(self): def test_email_tls_use_settings(self):
@ -1377,21 +1376,17 @@ class SMTPBackendTests(BaseEmailBackendTests, SMTPBackendTestsBase):
def test_email_tls_attempts_starttls(self): def test_email_tls_attempts_starttls(self):
backend = smtp.EmailBackend() backend = smtp.EmailBackend()
self.assertTrue(backend.use_tls) self.assertTrue(backend.use_tls)
try: with self.assertRaisesMessage(SMTPException, 'STARTTLS extension not supported by server.'):
with self.assertRaisesMessage(SMTPException, 'STARTTLS extension not supported by server.'): with backend:
backend.open() pass
finally:
backend.close()
@override_settings(EMAIL_USE_SSL=True) @override_settings(EMAIL_USE_SSL=True)
def test_email_ssl_attempts_ssl_connection(self): def test_email_ssl_attempts_ssl_connection(self):
backend = smtp.EmailBackend() backend = smtp.EmailBackend()
self.assertTrue(backend.use_ssl) self.assertTrue(backend.use_ssl)
try: with self.assertRaises(SSLError):
with self.assertRaises(SSLError): with backend:
backend.open() pass
finally:
backend.close()
def test_connection_timeout_default(self): def test_connection_timeout_default(self):
"""Test that the connection's timeout value is None by default.""" """Test that the connection's timeout value is None by default."""