comparison env/lib/python3.7/site-packages/virtualenv/util/six.py @ 5:9b1c78e6ba9c draft default tip

"planemo upload commit 6c0a8142489327ece472c84e558c47da711a9142"
author shellac
date Mon, 01 Jun 2020 08:59:25 -0400
parents 79f47841a781
children
comparison
equal deleted inserted replaced
4:79f47841a781 5:9b1c78e6ba9c
1 """Backward compatibility layer with older version of six.
2
3 This is used to avoid virtualenv requring a version of six newer than what
4 the system may have.
5 """
6 from __future__ import absolute_import
7
8 from six import PY2, PY3, binary_type, text_type
9
10 try:
11 from six import ensure_text
12 except ImportError:
13
14 def ensure_text(s, encoding="utf-8", errors="strict"):
15 """Coerce *s* to six.text_type.
16 For Python 2:
17 - `unicode` -> `unicode`
18 - `str` -> `unicode`
19 For Python 3:
20 - `str` -> `str`
21 - `bytes` -> decoded to `str`
22 """
23 if isinstance(s, binary_type):
24 return s.decode(encoding, errors)
25 elif isinstance(s, text_type):
26 return s
27 else:
28 raise TypeError("not expecting type '%s'" % type(s))
29
30
31 try:
32 from six import ensure_str
33 except ImportError:
34
35 def ensure_str(s, encoding="utf-8", errors="strict"):
36 """Coerce *s* to `str`.
37 For Python 2:
38 - `unicode` -> encoded to `str`
39 - `str` -> `str`
40 For Python 3:
41 - `str` -> `str`
42 - `bytes` -> decoded to `str`
43 """
44 if not isinstance(s, (text_type, binary_type)):
45 raise TypeError("not expecting type '%s'" % type(s))
46 if PY2 and isinstance(s, text_type):
47 s = s.encode(encoding, errors)
48 elif PY3 and isinstance(s, binary_type):
49 s = s.decode(encoding, errors)
50 return s