From d009b35ab2f2d6c7d5665e92c3fcba5e1a7cccda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Mon, 26 Mar 2018 10:06:25 +0200 Subject: [PATCH] python3: force_text/force_byte --- eopayment/common.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/eopayment/common.py b/eopayment/common.py index ac37e77..a16efe5 100644 --- a/eopayment/common.py +++ b/eopayment/common.py @@ -5,6 +5,8 @@ import logging import cgi from datetime import date +import six + __all__ = ['PaymentCommon', 'URL', 'HTML', 'RANDOM', 'RECEIVED', 'ACCEPTED', 'PAID', 'ERROR', 'WAITING'] @@ -29,15 +31,27 @@ ORDERID_TRANSACTION_SEPARATOR = '!' def force_text(s, encoding='utf-8'): - if isinstance(s, unicode): + if issubclass(type(s), six.text_type): return s try: - return unicode(s, encoding) + if not issubclass(type(s), six.string_types): + if six.PY3: + if isinstance(s, bytes): + s = six.text_type(s, encoding) + else: + s = six.text_type(s) + elif hasattr(s, '__unicode__'): + s = six.text_type(s) + else: + s = six.text_type(bytes(s), encoding) + else: + s = s.decode(encoding) except UnicodeDecodeError: - return unicode(s) + return six.text_type(s, encoding, 'ignore') + return s def force_byte(s, encoding='utf-8'): - if isinstance(s, str): + if isinstance(s, bytes): return s try: return s.encode(encoding)