summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAarni Koskela <akx@iki.fi>2018-05-28 12:57:45 +0300
committerAarni Koskela <akx@iki.fi>2018-05-28 13:01:43 +0300
commit34a6ccce1d93843d53efb5985ff5bbb7ea063e31 (patch)
treeeb2617714bac85d4a00c61d67bd21f5c2cebdcb9
parent0c6b3c87749dad880fbfdb0ddfbac4997ef04055 (diff)
downloadbabel-34a6ccce1d93843d53efb5985ff5bbb7ea063e31.tar.gz
_compat: add force_text a la Django
-rw-r--r--babel/_compat.py12
1 files changed, 11 insertions, 1 deletions
diff --git a/babel/_compat.py b/babel/_compat.py
index aea3389..1131f44 100644
--- a/babel/_compat.py
+++ b/babel/_compat.py
@@ -8,11 +8,12 @@ _identity = lambda x: x
if not PY2:
text_type = str
+ binary_type = bytes
string_types = (str,)
integer_types = (int, )
- unichr = chr
text_to_native = lambda s, enc: s
+ unichr = chr
iterkeys = lambda d: iter(d.keys())
itervalues = lambda d: iter(d.values())
@@ -31,6 +32,7 @@ if not PY2:
else:
text_type = unicode
+ binary_type = str
string_types = (str, unicode)
integer_types = (int, long)
@@ -57,6 +59,14 @@ else:
number_types = integer_types + (float,)
+def force_text(s, encoding='utf-8', errors='strict'):
+ if isinstance(s, text_type):
+ return s
+ if isinstance(s, binary_type):
+ return s.decode(encoding, errors)
+ return text_type(s)
+
+
#
# Since Python 3.3, a fast decimal implementation is already included in the
# standard library. Otherwise use cdecimal when available