Skip to content

Commit 30aa7d4

Browse files
committed
Add _get_valid_base64_regex as proxy function in order to lazy compile VALID_BASE64_REGEX
1 parent 715f313 commit 30aa7d4

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

Lib/base64.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,17 @@
2828
'urlsafe_b64encode', 'urlsafe_b64decode',
2929
]
3030

31-
VALID_BASE64_REGEX = re.compile(b'^[A-Za-z0-9+/]*={0,2}$')
31+
VALID_BASE64_REGEX = None
3232
bytes_types = (bytes, bytearray) # Types acceptable as binary data
3333

34+
def _get_valid_base64_regex():
35+
global VALID_BASE64_REGEX
36+
if VALID_BASE64_REGEX:
37+
return VALID_BASE64_REGEX
38+
39+
VALID_BASE64_REGEX = re.compile(b'^[A-Za-z0-9+/]*={0,2}$')
40+
return VALID_BASE64_REGEX
41+
3442
def _bytes_from_decode_data(s):
3543
if isinstance(s, str):
3644
try:
@@ -82,7 +90,7 @@ def b64decode(s, altchars=None, validate=False):
8290
altchars = _bytes_from_decode_data(altchars)
8391
assert len(altchars) == 2, repr(altchars)
8492
s = s.translate(bytes.maketrans(altchars, b'+/'))
85-
if validate and not VALID_BASE64_REGEX.match(s):
93+
if validate and not _get_valid_base64_regex().match(s):
8694
raise binascii.Error('Non-base64 digit found')
8795
return binascii.a2b_base64(s)
8896

0 commit comments

Comments
 (0)