-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpy3compat.py
52 lines (46 loc) · 1.39 KB
/
py3compat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#-*- coding:utf-8 -*-
import sys
if sys.version_info >= (3,0,0):
py3 = True
basestring = str
universal_newline_mode = u'r'
else:
bytes = str
str = unicode
py3 = False
universal_newline_mode = u'rU'
def safe_decode(s, enc='utf-8', errors='strict'):
if isinstance(s, str):
return s
if isinstance(s, bytes):
return s.decode(enc, errors)
# Numeric values are encoded right away
if isinstance(s, int) or isinstance(s, float):
return str(s)
# Some types need to be converted to unicode, but require the encoding
# and errors parameters. Notable examples are Exceptions, which have
# strange characters under some locales, such as French. It even appears
# that, at least in some cases, they have to be encodeed to str first.
# Presumably, there is a better way to do this, but for now this at
# least gives sensible results.
try:
return safe_decode(bytes(s), enc=enc, errors=errors)
except:
pass
# For other types, the unicode representation doesn't require a specific
# encoding. This mostly applies to non-stringy things, such as integers.
return str(s)
def safe_encode(s, enc='utf-8', errors='strict'):
if isinstance(s, bytes):
return s
return s.encode(enc, errors)
if py3:
safe_str = safe_decode
else:
safe_str = safe_encode
__all__ = ['py3', 'safe_decode', 'safe_encode', 'safe_str',
'universal_newline_mode']
if not py3:
__all__ += ['str', 'bytes']
else:
__all__ += ['basestring']