# Buggy code
def strformat(self, nlines_up=2):
    try:
        # Try to get a relative path
        # ipython/jupyter input just returns as self.filename
        path = os.path.relpath(self.filename)
    except ValueError:
        # Fallback to absolute path if error occurred in getting the
        # relative path.
        # This may happen on windows if the drive is different
        path = os.path.abspath(self.filename)

    lines = linecache.getlines(path)

    ret = []  # accumulates output
    if lines and self.line:

        def count_spaces(string):
            spaces = 0
            for x in itertools.takewhile(str.isspace, str(string)):
                spaces += 1
            return spaces

        selected = lines[self.line - nlines_up : self.line]
        # see if selected contains a definition
        def_found = False
        for x in selected:
            if "def " in x:
                def_found = True

        # no definition found, try and find one
        if not def_found:
            # try and find a def, go backwards from error line
            fn_name = None
            for x in reversed(lines[: self.line - 1]):
                if "def " in x:
                    fn_name = x
                    break
            if fn_name:
                ret.append(fn_name)
                spaces = count_spaces(x)
                ret.append(" " * (4 + spaces) + "<source elided>\n")

        ret.extend(selected[:-1])
        ret.append(_termcolor.highlight(selected[-1]))

        # point at the problem with a caret
        spaces = count_spaces(selected[-1])
        ret.append(" " * (spaces) + _termcolor.indicate("^"))

    # if in the REPL source may not be available
    if not ret:
        ret = "<source missing, REPL in use?>"

    err = _termcolor.filename('\nFile "%s", line %d:') + "\n%s"
    tmp = err % (path, self.line, _termcolor.code("".join(ret)))
    return tmp


# Traceback (IndexError)
$ conda create -n dsdebug python=3.6
[...]
The following NEW packages will be INSTALLED:

ca-certificates: 2018.03.07-0
certifi:         2018.4.16-py36_0
libcxx:          4.0.1-h579ed51_0
libcxxabi:       4.0.1-hebd6815_0
libedit:         3.1.20170329-hb402a30_2
libffi:          3.2.1-h475c297_4
ncurses:         6.1-h0a44026_0
openssl:         1.0.2o-h26aff7b_0
pip:             10.0.1-py36_0
python:          3.6.6-hc167b69_0
readline:        7.0-hc1231fa_4
setuptools:      39.2.0-py36_0
sqlite:          3.24.0-ha441bb4_0
tk:              8.6.7-h35a86e2_3
wheel:           0.31.1-py36_0
xz:              5.2.4-h1de35cc_4
zlib:            1.2.11-hf3cbc9b_2
[...]

$ conda activate dsdebug
(dsdebug)$ conda install numba numpy
[...]
The following NEW packages will be INSTALLED:

blas:         1.0-mkl
intel-openmp: 2018.0.3-0
libgfortran:  3.0.1-h93005f0_2
llvmlite:     0.24.0-py36hc454e04_0
mkl:          2018.0.3-1
mkl_fft:      1.0.2-py36h6b9c3cc_0
mkl_random:   1.0.1-py36h5d10147_1
numba:        0.39.0-py36h6440ff4_0
numpy:        1.14.5-py36h648b28d_4
numpy-base:   1.14.5-py36ha9ae307_4
[...]

(dsdebug)$ python dstest.py
Traceback (most recent call last):
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/errors.py", line 577, in new_error_context
yield
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/lowering.py", line 254, in lower_block
self.lower_inst(inst)
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/lowering.py", line 303, in lower_inst
val = self.lower_assign(ty, inst)
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/lowering.py", line 449, in lower_assign
return self.lower_expr(ty, value)
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/lowering.py", line 940, in lower_expr
res = self.context.special_ops[expr.op](self, expr)
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/npyufunc/array_exprs.py", line 388, in _lower_array_expr
cres = context.compile_subroutine_no_cache(builder, impl, inner_sig, flags=flags)
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/targets/base.py", line 799, in compile_subroutine_no_cache
locals=locals)
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/compiler.py", line 897, in compile_internal
return pipeline.compile_extra(func)
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/compiler.py", line 367, in compile_extra
return self._compile_bytecode()
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/compiler.py", line 804, in _compile_bytecode
return self._compile_core()
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/compiler.py", line 791, in _compile_core
res = pm.run(self.status)
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/compiler.py", line 253, in run
raise patched_exception
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/compiler.py", line 245, in run
stage()
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/compiler.py", line 480, in stage_generic_rewrites
self, self.func_ir)
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/rewrites/registry.py", line 70, in apply
pipeline.calltypes)
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/rewrites/static_binop.py", line 23, in match
self.static_rhs[expr] = func_ir.infer_constant(expr.rhs)
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/ir.py", line 964, in infer_constant
return self._consts.infer_constant(name)
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/consts.py", line 34, in infer_constant
self._cache[name] = (True, self._do_infer(name))
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/consts.py", line 60, in _do_infer
const = defn.infer_constant()
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/ir.py", line 577, in infer_constant
raise ConstantInferenceError('%s' % self, loc=self.loc)
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/errors.py", line 528, in __init__
super(ConstantInferenceError, self).__init__(msg, loc=loc)
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/errors.py", line 388, in __init__
highlight("%s\n%s\n" % (msg, loc.strformat())))
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/ir.py", line 87, in strformat
ret.append(_termcolor.highlight(selected[-1]))
IndexError: Failed at nopython (nopython rewrites)
list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "dstest.py", line 10, in <module>
print(fn(a))
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/dispatcher.py", line 368, in _compile_for_args
raise e
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/dispatcher.py", line 325, in _compile_for_args
return self.compile(tuple(argtypes))
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/dispatcher.py", line 653, in compile
cres = self._compiler.compile(args, return_type)
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/dispatcher.py", line 83, in compile
pipeline_class=self.pipeline_class)
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/compiler.py", line 873, in compile_extra
return pipeline.compile_extra(func)
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/compiler.py", line 367, in compile_extra
return self._compile_bytecode()
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/compiler.py", line 804, in _compile_bytecode
return self._compile_core()
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/compiler.py", line 791, in _compile_core
res = pm.run(self.status)
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/compiler.py", line 253, in run
raise patched_exception
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/compiler.py", line 245, in run
stage()
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/compiler.py", line 678, in stage_nopython_backend
self._backend(lowerfn, objectmode=False)
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/compiler.py", line 628, in _backend
lowered = lowerfn()
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/compiler.py", line 615, in backend_nopython_mode
self.flags)
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/compiler.py", line 992, in native_lowering_stage
lower.lower()
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/lowering.py", line 173, in lower
self.lower_normal_function(self.fndesc)
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/lowering.py", line 214, in lower_normal_function
entry_block_tail = self.lower_function_body()
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/lowering.py", line 239, in lower_function_body
self.lower_block(block)
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/lowering.py", line 254, in lower_block
self.lower_inst(inst)
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/contextlib.py", line 99, in __exit__
self.gen.throw(type, value, traceback)
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/errors.py", line 585, in new_error_context
six.reraise(type(newerr), newerr, tb)
File "/Users/cball/anaconda3/envs/dsdebug/lib/python3.6/site-packages/numba/six.py", line 659, in reraise
raise value
numba.errors.LoweringError: Failed at nopython (nopython mode backend)
Failed at nopython (nopython rewrites)
list index out of range

File "dstest.py", line 6:
def fn(x):
return 10**x
^
[1] During: lowering "$0.3 = arrayexpr(expr=('**', [const(int, 10), Var(x, dstest.py (6))]), ty=array(float64, 1d, C))" at dstest.py (6)
-------------------------------------------------------------------------------
This should not have happened, a problem has occurred in Numba's internals.

Please report the error message and traceback, along with a minimal reproducer
at: https://github.com/numba/numba/issues/new

If more help is needed please feel free to speak to the Numba core developers
directly at: https://gitter.im/numba/numba

Thanks in advance for your help in improving Numba!

(dsdebug)$ conda install "numba<0.39"
[...]
The following packages will be DOWNGRADED:

llvmlite: 0.24.0-py36hc454e04_0 --> 0.23.2-py36hc454e04_0
numba:    0.39.0-py36h6440ff4_0 --> 0.38.1-py36h6440ff4_0
[...]

$ python dstest.py
[10.]

# Fixed code
def strformat(self, nlines_up=2):
    try:
        # Try to get a relative path
        # ipython/jupyter input just returns as self.filename
        path = os.path.relpath(self.filename)
    except ValueError:
        # Fallback to absolute path if error occurred in getting the
        # relative path.
        # This may happen on windows if the drive is different
        path = os.path.abspath(self.filename)

    lines = linecache.getlines(path)

    ret = []  # accumulates output
    if lines and self.line:

        def count_spaces(string):
            spaces = 0
            for x in itertools.takewhile(str.isspace, str(string)):
                spaces += 1
            return spaces

        # A few places in the code still use no `loc` or default to line 1
        # this is often in places where exceptions are used for the purposes
        # of flow control. As a result max is in use to prevent slice from
        # `[negative: positive]`
        selected = lines[max(0, self.line - nlines_up) : self.line]

        # see if selected contains a definition
        def_found = False
        for x in selected:
            if "def " in x:
                def_found = True

        # no definition found, try and find one
        if not def_found:
            # try and find a def, go backwards from error line
            fn_name = None
            for x in reversed(lines[: self.line - 1]):
                if "def " in x:
                    fn_name = x
                    break
            if fn_name:
                ret.append(fn_name)
                spaces = count_spaces(x)
                ret.append(" " * (4 + spaces) + "<source elided>\n")

        if selected:
            ret.extend(selected[:-1])
            ret.append(_termcolor.highlight(selected[-1]))

            # point at the problem with a caret
            spaces = count_spaces(selected[-1])
            ret.append(" " * (spaces) + _termcolor.indicate("^"))

    # if in the REPL source may not be available
    if not ret:
        ret = "<source missing, REPL in use?>"

    err = _termcolor.filename('\nFile "%s", line %d:') + "\n%s"
    tmp = err % (path, self.line, _termcolor.code("".join(ret)))
    return tmp