-
Notifications
You must be signed in to change notification settings - Fork 4
/
exc_fmt.py
62 lines (51 loc) · 1.46 KB
/
exc_fmt.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
53
54
55
56
57
58
59
60
61
62
#!/usr/local/bin/python
# coding: utf-8
import sys
import warnings
import traceback
try:
unicode()
except NameError:
unicode = None
def str_e(e):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
message = getattr(e, "message", None)
if not (message and unicode):
return str(e)
try:
return unicode(message, "utf8").encode("utf8")
except Exception:
return message.encode("utf8")
def pp_e(e):
"""returns a str in all pythons everywher"""
# py3k has __traceback__
tb = getattr(e, "__traceback__", None)
if tb is not None:
return "\n".join(traceback.format_tb(tb)) + str_e(e)
# in case of sys.exc_clear()
_, _, tb = sys.exc_info()
if tb is not None:
return "\n".join(traceback.format_tb(tb)) + str_e(e)
return str_e(e)
if __name__ == "__main__":
def test(excp):
try:
raise excp
except Exception as e:
stre = str_e(e)
assert isinstance(stre, str)
print(stre)
def test2(excp):
try:
raise excp
except Exception as e:
sys.exc_clear()
stre = str_e(e)
assert isinstance(stre, str)
print(stre)
tests = [Exception("asdf"), Exception(u"aß∂ƒ"), Exception(u"asdf"), Exception(b"asdf1234")]
for t in tests:
test(t)
if getattr(sys, "exc_clear", None):
test2(t)