python3: Decode return value of b64encode into str

b64encode is mainly used to encode binary data into JSON.  As binary_type
isn't JSON serializable in python3, it makes more sense to convert to
binary_type early.  Also, allow text_type to be base64 encoded.

Signed-off-by: IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
IWAMOTO Toshihiro 2015-06-29 19:51:12 +09:00 committed by FUJITA Tomonori
parent 2f74871758
commit 01a3cde6ac
2 changed files with 11 additions and 1 deletions

View File

@ -188,7 +188,11 @@ class StringifyMixin(object):
def _get_default_encoder(cls, encode_string):
def _encode(v):
if isinstance(v, (bytes, six.text_type)):
if isinstance(v, six.text_type):
v = v.encode('utf-8')
json_value = encode_string(v)
if six.PY3:
json_value = json_value.decode('ascii')
elif isinstance(v, list):
json_value = list(map(_encode, v))
elif isinstance(v, dict):

View File

@ -112,5 +112,11 @@ class IPv6Addr(TypeDescr):
class UnknownType(TypeDescr):
import base64
b64encode = base64.b64encode
if six.PY3:
@classmethod
def to_user(cls, data):
return cls.b64encode(data).decode('ascii')
else:
to_user = staticmethod(base64.b64encode)
from_user = staticmethod(base64.b64decode)