Skip to content

Commit

Permalink
Improve error messages.
Browse files Browse the repository at this point in the history
* Compact format
* Traceback interleaving
* Exception "eating" via disappearance/clobbering is fixed.
  • Loading branch information
stuartarchibald committed May 18, 2020
1 parent 64fbf2b commit cc673c1
Show file tree
Hide file tree
Showing 13 changed files with 381 additions and 82 deletions.
41 changes: 30 additions & 11 deletions numba/core/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ def indicate(self, msg):
def highlight(self, msg):
pass

@abstractmethod
def reset(self, msg):
pass


class _DummyColorScheme(_ColorScheme):

Expand All @@ -123,6 +127,9 @@ def indicate(self, msg):
def highlight(self, msg):
pass

def reset(self, msg):
pass


# holds reference to the instance of the terminal color scheme in use
_termcolor_inst = None
Expand Down Expand Up @@ -172,6 +179,9 @@ def indicate(self, msg):
def highlight(self, msg):
return msg

def reset(self, msg):
return msg

def termcolor():
global _termcolor_inst
if _termcolor_inst is None:
Expand Down Expand Up @@ -215,35 +225,40 @@ def __exit__(self, *exc_detail):
'errmsg': None,
'filename': None,
'indicate': None,
'highlight': None, }
'highlight': None,
'reset': None, }

# suitable for terminals with a dark background
themes['dark_bg'] = {'code': Fore.BLUE,
'errmsg': Fore.YELLOW,
'filename': Fore.WHITE,
'indicate': Fore.GREEN,
'highlight': Fore.RED, }
'highlight': Fore.RED,
'reset': Style.RESET_ALL, }

# suitable for terminals with a light background
themes['light_bg'] = {'code': Fore.BLUE,
'errmsg': Fore.BLACK,
'filename': Fore.MAGENTA,
'indicate': Fore.BLACK,
'highlight': Fore.RED, }
'highlight': Fore.RED,
'reset': Style.RESET_ALL, }

# suitable for terminals with a blue background
themes['blue_bg'] = {'code': Fore.WHITE,
'errmsg': Fore.YELLOW,
'filename': Fore.MAGENTA,
'indicate': Fore.CYAN,
'highlight': Fore.RED, }
'highlight': Fore.RED,
'reset': Style.RESET_ALL, }

# suitable for use in jupyter notebooks
themes['jupyter_nb'] = {'code': Fore.BLACK,
'errmsg': Fore.BLACK,
'filename': Fore.GREEN,
'indicate': Fore.CYAN,
'highlight': Fore.RED, }
'highlight': Fore.RED,
'reset': Style.RESET_ALL, }

default_theme = themes['no_color']

Expand All @@ -254,6 +269,7 @@ def __init__(self, theme=default_theme):
self._filename = theme['filename']
self._indicate = theme['indicate']
self._highlight = theme['highlight']
self._reset = theme['reset']
_DummyColorScheme.__init__(self, theme=theme)

def _markup(self, msg, color=None, style=Style.BRIGHT):
Expand Down Expand Up @@ -283,6 +299,9 @@ def indicate(self, msg):
def highlight(self, msg):
return self._markup(msg, self._highlight)

def reset(self, msg):
return self._markup(msg, self._reset)

def termcolor():
global _termcolor_inst
if _termcolor_inst is None:
Expand Down Expand Up @@ -459,11 +478,12 @@ def __init__(self, msg, loc=None, highlighting=True):
else:
def highlight(x):
return x

if loc:
super(NumbaError, self).__init__(
highlight("%s\n%s\n" % (msg, loc.strformat())))
new_msg = "%s\n%s\n" % (msg, loc.strformat())
else:
super(NumbaError, self).__init__(highlight("%s" % (msg,)))
new_msg = "%s" % (msg,)
super(NumbaError, self).__init__(highlight(new_msg))

@property
def contexts(self):
Expand All @@ -479,9 +499,8 @@ def add_context(self, msg):
contextual information.
"""
self.contexts.append(msg)
f = termcolor().errmsg('{0}\n') + termcolor().filename(
'[{1}] During: {2}')
newmsg = f.format(self, len(self.contexts), msg)
f = termcolor().errmsg('{0}\n') + termcolor().filename('During: {1}')
newmsg = f.format(self, msg)
self.args = (newmsg,)
return self

Expand Down
3 changes: 2 additions & 1 deletion numba/core/extending.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def generic(self):
_overload_default_jit_options = {'no_cpython_wrapper': True}


def overload(func, jit_options={}, strict=True, inline='never'):
def overload(func, jit_options={}, strict=True, inline='never',
signatures=None):
"""
A decorator marking the decorated function as typing and implementing
*func* in nopython mode.
Expand Down

0 comments on commit cc673c1

Please sign in to comment.