summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrédéric Péters <fpeters@entrouvert.com>2018-03-26 08:06:25 (GMT)
committerFrédéric Péters <fpeters@entrouvert.com>2018-07-25 08:14:25 (GMT)
commitd009b35ab2f2d6c7d5665e92c3fcba5e1a7cccda (patch)
treef92a8efd317c731110a4ad33d35496800aeb6d12
parentfe718e1159027e48daa3cfdbaace46acc4fb9575 (diff)
downloadeopayment-d009b35ab2f2d6c7d5665e92c3fcba5e1a7cccda.zip
eopayment-d009b35ab2f2d6c7d5665e92c3fcba5e1a7cccda.tar.gz
eopayment-d009b35ab2f2d6c7d5665e92c3fcba5e1a7cccda.tar.bz2
python3: force_text/force_byte
-rw-r--r--eopayment/common.py22
1 files 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)