Skip to content

Commit e131e66

Browse files
committed
simplify runtime code
1 parent 4928210 commit e131e66

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

Lib/smtplib.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import email.utils
4646
import email.message
4747
import email.generator
48-
import functools
4948
import base64
5049
import hmac
5150
import copy
@@ -179,19 +178,12 @@ def _fix_eols(data):
179178
return re.sub(r'(?:\r\n|\n|\r(?!\n))', CRLF, data)
180179

181180

182-
# Use unbounded LRU cache instead of global variable to ease mocking.
183-
@functools.cache
184-
def _have_cram_md5_support():
185-
"""Check if CRAM-MD5 is supported by the host.
186-
187-
Note that CRAM-MD5 may be supported by the server
188-
but not by the client if HMAC-MD5 is not supported.
189-
"""
190-
try:
191-
hmac.digest(b'', b'', 'md5')
192-
return True
193-
except ValueError:
194-
return False
181+
try:
182+
hmac.digest(b'', b'', 'md5')
183+
except ValueError:
184+
_have_cram_md5_support = False
185+
else:
186+
_have_cram_md5_support = True
195187

196188

197189
try:
@@ -682,7 +674,7 @@ def auth_cram_md5(self, challenge=None):
682674
# CRAM-MD5 does not support initial-response.
683675
if challenge is None:
684676
return None
685-
if not _have_cram_md5_support():
677+
if not _have_cram_md5_support:
686678
raise SMTPException("CRAM-MD5 is not supported")
687679
password = self.password.encode('ascii')
688680
authcode = hmac.HMAC(password, challenge, 'md5')
@@ -738,7 +730,7 @@ def login(self, user, password, *, initial_response_ok=True):
738730
advertised_authlist = self.esmtp_features["auth"].split()
739731

740732
# Authentication methods we can handle in our preferred order:
741-
if _have_cram_md5_support():
733+
if _have_cram_md5_support:
742734
preferred_auths = ['CRAM-MD5', 'PLAIN', 'LOGIN']
743735
else:
744736
preferred_auths = ['PLAIN', 'LOGIN']

Lib/test/test_smtplib.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import threading
1818

1919
import unittest
20+
import unittest.mock as mock
2021
from test import support, mock_socket
2122
from test.support import hashlib_helper
2223
from test.support import socket_helper
@@ -1034,7 +1035,6 @@ def handle_error(self):
10341035
class SMTPSimTests(unittest.TestCase):
10351036

10361037
def setUp(self):
1037-
smtplib._have_cram_md5_support.cache_clear()
10381038
self.thread_key = threading_helper.threading_setup()
10391039
self.real_getfqdn = socket.getfqdn
10401040
socket.getfqdn = mock_socket.getfqdn
@@ -1186,6 +1186,7 @@ def testAUTH_CRAM_MD5(self):
11861186
smtp.close()
11871187

11881188
@hashlib_helper.block_algorithm('md5')
1189+
@mock.patch("smtplib._have_cram_md5_support", False)
11891190
def testAUTH_CRAM_MD5_blocked(self):
11901191
# CRAM-MD5 is the only "known" method by the server,
11911192
# but it is not supported by the client. In particular,
@@ -1199,6 +1200,7 @@ def testAUTH_CRAM_MD5_blocked(self):
11991200
smtp.login(sim_auth[0], sim_auth[1])
12001201

12011202
@hashlib_helper.block_algorithm('md5')
1203+
@mock.patch("smtplib._have_cram_md5_support", False)
12021204
def testAUTH_CRAM_MD5_blocked_and_fallback(self):
12031205
# Test that PLAIN is tried after CRAM-MD5 failed
12041206
self.serv.add_feature("AUTH CRAM-MD5 PLAIN")

0 commit comments

Comments
 (0)