forked from wbond/package_control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunicode.py
33 lines (27 loc) · 1.03 KB
/
unicode.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
import os
import locale
def unicode_from_os(e):
"""
This is needed as some exceptions coming from the OS are
already encoded and so just calling unicode(e) will result
in an UnicodeDecodeError as the string isn't in ascii form.
:param e:
The exception to get the value of
:return:
The unicode version of the exception message
"""
try:
# Sublime Text on OS X does not seem to report the correct encoding
# so we hard-code that to UTF-8
encoding = 'UTF-8' if os.name == 'darwin' else locale.getpreferredencoding()
return unicode(str(e), encoding)
# If the "correct" encoding did not work, try some defaults, and then just
# obliterate characters that we can't seen to decode properly
except UnicodeDecodeError:
encodings = ['utf-8', 'cp1252']
for encoding in encodings:
try:
return unicode(str(e), encoding, errors='strict')
except:
pass
return unicode(str(e), errors='replace')