diff options
author | IWAMOTO Toshihiro <iwamoto@valinux.co.jp> | 2015-06-29 19:51:12 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2015-06-29 22:09:41 +0900 |
commit | 01a3cde6ac4ae05fff4423eff952a277d9c481ec (patch) | |
tree | 1b2a61259b4c231fdcef63c287367f1755dea62d | |
parent | 2f74871758e6e813d47dd1ca155ca6e56da8c4bf (diff) |
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>
-rw-r--r-- | ryu/lib/stringify.py | 4 | ||||
-rw-r--r-- | ryu/lib/type_desc.py | 8 |
2 files changed, 11 insertions, 1 deletions
diff --git a/ryu/lib/stringify.py b/ryu/lib/stringify.py index ca762416..8a865b7e 100644 --- a/ryu/lib/stringify.py +++ b/ryu/lib/stringify.py @@ -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): diff --git a/ryu/lib/type_desc.py b/ryu/lib/type_desc.py index 612be379..d96fc51f 100644 --- a/ryu/lib/type_desc.py +++ b/ryu/lib/type_desc.py @@ -112,5 +112,11 @@ class IPv6Addr(TypeDescr): class UnknownType(TypeDescr): import base64 - to_user = staticmethod(base64.b64encode) + 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) |