[Python-3000-checkins] r58803 - python/branches/py3k-pep3137/Lib/base64.py
guido.van.rossum
python-3000-checkins at python.org
Fri Nov 2 21:57:23 CET 2007
Author: guido.van.rossum
Date: Fri Nov 2 21:57:22 2007
New Revision: 58803
Modified:
python/branches/py3k-pep3137/Lib/base64.py
Log:
Some more base64 stuff.
(binascii needs to change too though.)
Modified: python/branches/py3k-pep3137/Lib/base64.py
==============================================================================
--- python/branches/py3k-pep3137/Lib/base64.py (original)
+++ python/branches/py3k-pep3137/Lib/base64.py Fri Nov 2 21:57:22 2007
@@ -27,8 +27,11 @@
]
+bytes_buffer = (bytes, buffer) # Types acceptable as binary data
+
+
def _translate(s, altchars):
- if not isinstance(s, (bytes, buffer)):
+ if not isinstance(s, bytes_buffer):
raise TypeError("expected bytes, not %s" % s.__class__.__name__)
translation = buffer(range(256))
for k, v in altchars.items():
@@ -49,12 +52,12 @@
The encoded byte string is returned.
"""
- if not isinstance(s, bytes):
+ if not isinstance(s, bytes_buffer):
s = bytes(s, "ascii")
# Strip off the trailing newline
encoded = binascii.b2a_base64(s)[:-1]
if altchars is not None:
- if not isinstance(altchars, bytes):
+ if not isinstance(altchars, bytes_buffer):
altchars = bytes(altchars, "ascii")
assert len(altchars) == 2, repr(altchars)
return _translate(encoded, {'+': altchars[0:1], '/': altchars[1:2]})
@@ -72,10 +75,10 @@
s were incorrectly padded or if there are non-alphabet characters
present in the string.
"""
- if not isinstance(s, bytes):
+ if not isinstance(s, bytes_buffer):
s = bytes(s)
if altchars is not None:
- if not isinstance(altchars, bytes):
+ if not isinstance(altchars, bytes_buffer):
altchars = bytes(altchars, "ascii")
assert len(altchars) == 2, repr(altchars)
s = _translate(s, {chr(altchars[0]): b'+', chr(altchars[1]): b'/'})
@@ -144,7 +147,7 @@
s is the byte string to encode. The encoded byte string is returned.
"""
- if not isinstance(s, bytes):
+ if not isinstance(s, bytes_buffer):
s = bytes(s)
quanta, leftover = divmod(len(s), 5)
# Pad the last quantum with zero bits if necessary
@@ -201,7 +204,7 @@
the input is incorrectly padded or if there are non-alphabet
characters present in the input.
"""
- if not isinstance(s, bytes):
+ if not isinstance(s, bytes_buffer):
s = bytes(s)
quanta, leftover = divmod(len(s), 8)
if leftover:
@@ -210,12 +213,12 @@
# False, or the character to map the digit 1 (one) to. It should be
# either L (el) or I (eye).
if map01:
- if not isinstance(map01, bytes):
+ if not isinstance(map01, bytes_buffer):
map01 = bytes(map01)
assert len(map01) == 1, repr(map01)
- s = _translate(s, {'0': b'O', '1': map01})
+ s = _translate(s, {b'0': b'O', b'1': map01})
if casefold:
- s = bytes(str(s, "ascii").upper(), "ascii")
+ s = s.upper()
# Strip off pad characters from the right. We need to count the pad
# characters because this will tell us how many null bytes to remove from
# the end of the decoded string.
@@ -266,7 +269,7 @@
s is the byte string to encode. The encoded byte string is returned.
"""
- return bytes(str(binascii.hexlify(s), "ascii").upper(), "ascii")
+ return binascii.hexlify(s).upper()
def b16decode(s, casefold=False):
@@ -280,10 +283,10 @@
s were incorrectly padded or if there are non-alphabet characters
present in the string.
"""
- if not isinstance(s, bytes):
+ if not isinstance(s, bytes_buffer):
s = bytes(s)
if casefold:
- s = bytes(str(s, "ascii").upper(), "ascii")
+ s = s.upper()
if re.search('[^0-9A-F]', s):
raise binascii.Error('Non-base16 digit found')
return binascii.unhexlify(s)
@@ -327,7 +330,7 @@
Argument and return value are bytes.
"""
- if not isinstance(s, bytes):
+ if not isinstance(s, bytes_buffer):
raise TypeError("expected bytes, not %s" % s.__class__.__name__)
pieces = []
for i in range(0, len(s), MAXBINSIZE):
@@ -341,7 +344,7 @@
Argument and return value are bytes.
"""
- if not isinstance(s, bytes):
+ if not isinstance(s, bytes_buffer):
raise TypeError("expected bytes, not %s" % s.__class__.__name__)
return binascii.a2b_base64(s)
More information about the Python-3000-checkins
mailing list