1 2 3 4 5 6 7 8 9 | def b64d(source):
# Base64 decode some data, accepting bytes or unicode/str, and returning
# str/unicode if the result is utf-8 compatible, otherwise returning bytes.
decoded = b64decode(source)
if isinstance(decoded, bytes):
try:
return decoded.decode('utf-8')
except UnicodeDecodeError:
return decoded
|