Skip to content

Commit fd9eaf7

Browse files
committed
Avoid circular import in _base64
This requires some code duplication, but oh well.
1 parent 2c40ba0 commit fd9eaf7

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

Lib/_base64.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
"""C accelerator wrappers for originally pure-Python parts of base64."""
22

33
from binascii import Error, a2b_ascii85, a2b_base85, b2a_ascii85, b2a_base85
4-
from base64 import _bytes_from_decode_data, bytes_types
54

65

7-
# Base 85 encoder functions in base64 silently convert input to bytes.
6+
# Base 85 functions in base64 silently convert input to bytes.
7+
# Copy the conversion logic from base64 to avoid circular imports.
8+
9+
bytes_types = (bytes, bytearray) # Types acceptable as binary data
10+
11+
12+
def _bytes_from_decode_data(s):
13+
if isinstance(s, str):
14+
try:
15+
return s.encode('ascii')
16+
except UnicodeEncodeError:
17+
raise ValueError('string argument should contain only ASCII characters')
18+
if isinstance(s, bytes_types):
19+
return s
20+
try:
21+
return memoryview(s).tobytes()
22+
except TypeError:
23+
raise TypeError("argument should be a bytes-like object or ASCII "
24+
"string, not %r" % s.__class__.__name__) from None
25+
26+
827
def _bytes_from_encode_data(b):
928
return b if isinstance(b, bytes_types) else memoryview(b).tobytes()
1029

0 commit comments

Comments
 (0)