-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
67 lines (47 loc) · 1.93 KB
/
__init__.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
63
64
65
66
67
"""Beautiful and helpful exceptions
Just set your `ng_EXCEPTIONS` environment variable. It handles the rest.
Name: ng_exceptions
Author: Krishna Aaseri
Email: krishna18@navgurukul.org
URL: github.com/ng-exceptions
License: Copyright (c) 2017 Josh Junon, licensed under the MIT license
"""
from __future__ import absolute_import
from __future__ import print_function
import logging
import sys
from .formatter import THEME, MAX_LENGTH, PIPE_CHAR, CAP_CHAR, ExceptionFormatter
from .encoding import to_byte
from .context import PY3
from .color import SUPPORTS_COLOR, SHOULD_ENCODE, STREAM
from .log import BetExcLogger, patch as patch_logging
from .repl import interact, get_repl
from .translate import*
__version__ = '0.2.2'
THEME = THEME.copy() # Users customizing the theme should not impact core
def write_stream(data, stream=STREAM):
if SHOULD_ENCODE:
data = to_byte(data)
if PY3:
stream.buffer.write(data)
else:
stream.write(data)
else:
stream.write(data)
def format_exception(exc, value, tb):
# Rebuild each time to take into account any changes made by the user to the global parameters
formatter = ExceptionFormatter(colored=SUPPORTS_COLOR, theme=THEME, max_length=MAX_LENGTH,
pipe_char=PIPE_CHAR, cap_char=CAP_CHAR)
return list(formatter.format_exception(exc, value, tb))
def excepthook(exc, value, tb):
formatted = u''.join(format_exception(exc, value, tb))
formatted = translate(formatted)
write_stream(formatted, STREAM)
def hook():
sys.excepthook = excepthook
logging.setLoggerClass(BetExcLogger)
patch_logging()
if hasattr(sys, 'ps1'):
print('WARNING: ng_exceptions will only inspect code from the command line\n'
' when using: `python -m ng_exceptions\'. Otherwise, only code\n'
' loaded from files will be inspected!', file=sys.stderr)