Coerce **s** to six.binary_type. For Python 2: - `unicode` -> encoded to `str` - `str` -> `str` For Python 3: - `str` -> encoded to `bytes` - `bytes` -> `bytes`
(s, encoding='utf-8', errors='strict')
| 896 | |
| 897 | |
| 898 | def ensure_binary(s, encoding='utf-8', errors='strict'): |
| 899 | """Coerce **s** to six.binary_type. |
| 900 | |
| 901 | For Python 2: |
| 902 | - `unicode` -> encoded to `str` |
| 903 | - `str` -> `str` |
| 904 | |
| 905 | For Python 3: |
| 906 | - `str` -> encoded to `bytes` |
| 907 | - `bytes` -> `bytes` |
| 908 | """ |
| 909 | if isinstance(s, binary_type): |
| 910 | return s |
| 911 | if isinstance(s, text_type): |
| 912 | return s.encode(encoding, errors) |
| 913 | raise TypeError("not expecting type '%s'" % type(s)) |
| 914 | |
| 915 | |
| 916 | def ensure_str(s, encoding='utf-8', errors='strict'): |