Coerce *s* to six.text_type. For Python 2: - `unicode` -> `unicode` - `str` -> `unicode` For Python 3: - `str` -> `str` - `bytes` -> decoded to `str`
(s, encoding='utf-8', errors='strict')
| 942 | |
| 943 | |
| 944 | def ensure_text(s, encoding='utf-8', errors='strict'): |
| 945 | """Coerce *s* to six.text_type. |
| 946 | |
| 947 | For Python 2: |
| 948 | - `unicode` -> `unicode` |
| 949 | - `str` -> `unicode` |
| 950 | |
| 951 | For Python 3: |
| 952 | - `str` -> `str` |
| 953 | - `bytes` -> decoded to `str` |
| 954 | """ |
| 955 | if isinstance(s, binary_type): |
| 956 | return s.decode(encoding, errors) |
| 957 | elif isinstance(s, text_type): |
| 958 | return s |
| 959 | else: |
| 960 | raise TypeError("not expecting type '%s'" % type(s)) |
| 961 | |
| 962 | |
| 963 | def python_2_unicode_compatible(klass): |