# Buggy code
def _find_definition(self):
    # try and find a def, go backwards from error line
    fn_name = None
    lines = self.get_lines()
    for x in reversed(lines[: self.line - 1]):
        if "def " in x:
            fn_name = x
            break

    return fn_name


# Traceback (AttributeError)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~/.local/lib/python3.7/site-packages/numba/errors.py in new_error_context(fmt_, *args, **kwargs)
626     try:
--> 627         yield
628     except NumbaError as e:

~/.local/lib/python3.7/site-packages/numba/lowering.py in lower_block(self, block)
257                                    loc=self.loc, errcls_=defaulterrcls):
--> 258                 self.lower_inst(inst)
259

~/.local/lib/python3.7/site-packages/numba/lowering.py in lower_inst(self, inst)
399         elif isinstance(inst, ir.StaticRaise):
--> 400             self.lower_static_raise(inst)
401

~/.local/lib/python3.7/site-packages/numba/lowering.py in lower_static_raise(self, inst)
441         else:
--> 442             self.return_exception(inst.exc_class, inst.exc_args, loc=self.loc)
443

~/.local/lib/python3.7/site-packages/numba/lowering.py in return_exception(self, exc_class, exc_args, loc)
162                                        loc=loc,
--> 163                                        func_name=self.func_ir.func_id.func_name)
164

~/.local/lib/python3.7/site-packages/numba/targets/callconv.py in return_user_exc(self, builder, exc, exc_args, loc, func_name)
364         if loc is not None:
--> 365             fname = loc._raw_function_name()
366             if fname is None:

~/.local/lib/python3.7/site-packages/numba/ir.py in _raw_function_name(self)
62         if defn:
---> 63             return self._defmatcher.match(defn.strip()).groups()[0]
64         else:

AttributeError: 'NoneType' object has no attribute 'groups'

During handling of the above exception, another exception occurred:

LoweringError                             Traceback (most recent call last)
<ipython-input-1-647b0e2d4c3b> in <module>
----> 1 userland stops at function call

~/.local/lib/python3.7/site-packages/numba/dispatcher.py in _compile_for_args(self, *args, **kws)
367                     e.patch_message(''.join(e.args) + help_msg)
368             # ignore the FULL_TRACEBACKS config, this needs reporting!
--> 369             raise e
370
371     def inspect_llvm(self, signature=None):

~/.local/lib/python3.7/site-packages/numba/dispatcher.py in _compile_for_args(self, *args, **kws)
324                 argtypes.append(self.typeof_pyval(a))
325         try:
--> 326             return self.compile(tuple(argtypes))
327         except errors.TypingError as e:
328             # Intercept typing error that may be due to an argument

~/.local/lib/python3.7/site-packages/numba/compiler_lock.py in _acquire_compile_lock(*args, **kwargs)
30         def _acquire_compile_lock(*args, **kwargs):
31             with self:
---> 32                 return func(*args, **kwargs)
33         return _acquire_compile_lock
34

~/.local/lib/python3.7/site-packages/numba/dispatcher.py in compile(self, sig)
656
657             self._cache_misses[sig] += 1
--> 658             cres = self._compiler.compile(args, return_type)
659             self.add_overload(cres)
660             self._cache.save_overload(sig, cres)

~/.local/lib/python3.7/site-packages/numba/dispatcher.py in compile(self, args, return_type)
80                                       args=args, return_type=return_type,
81                                       flags=flags, locals=self.locals,
---> 82                                       pipeline_class=self.pipeline_class)
83         # Check typing error if object mode is used
84         if cres.typing_error is not None and not flags.enable_pyobject:

~/.local/lib/python3.7/site-packages/numba/compiler.py in compile_extra(typingctx, targetctx, func, args, return_type, flags, locals, library, pipeline_class)
939     pipeline = pipeline_class(typingctx, targetctx, library,
940                               args, return_type, flags, locals)
--> 941     return pipeline.compile_extra(func)
942
943

~/.local/lib/python3.7/site-packages/numba/compiler.py in compile_extra(self, func)
370         self.lifted = ()
371         self.lifted_from = None
--> 372         return self._compile_bytecode()
373
374     def compile_ir(self, func_ir, lifted=(), lifted_from=None):

~/.local/lib/python3.7/site-packages/numba/compiler.py in _compile_bytecode(self)
870         """
871         assert self.func_ir is None
--> 872         return self._compile_core()
873
874     def _compile_ir(self):

~/.local/lib/python3.7/site-packages/numba/compiler.py in _compile_core(self)
857         self.define_pipelines(pm)
858         pm.finalize()
--> 859         res = pm.run(self.status)
860         if res is not None:
861             # Early pipeline completion

~/.local/lib/python3.7/site-packages/numba/compiler_lock.py in _acquire_compile_lock(*args, **kwargs)
30         def _acquire_compile_lock(*args, **kwargs):
31             with self:
---> 32                 return func(*args, **kwargs)
33         return _acquire_compile_lock
34

~/.local/lib/python3.7/site-packages/numba/compiler.py in run(self, status)
251                     # No more fallback pipelines?
252                     if is_final_pipeline:
--> 253                         raise patched_exception
254                     # Go to next fallback pipeline
255                     else:

~/.local/lib/python3.7/site-packages/numba/compiler.py in run(self, status)
242                 try:
243                     event(stage_name)
--> 244                     stage()
245                 except _EarlyPipelineCompletion as e:
246                     return e.result

~/.local/lib/python3.7/site-packages/numba/compiler.py in stage_nopython_backend(self)
729         """
730         lowerfn = self.backend_nopython_mode
--> 731         self._backend(lowerfn, objectmode=False)
732
733     def stage_compile_interp_mode(self):

~/.local/lib/python3.7/site-packages/numba/compiler.py in _backend(self, lowerfn, objectmode)
679             self.library.enable_object_caching()
680
--> 681         lowered = lowerfn()
682         signature = typing.signature(self.return_type, *self.args)
683         self.cr = compile_result(

~/.local/lib/python3.7/site-packages/numba/compiler.py in backend_nopython_mode(self)
666                 self.calltypes,
667                 self.flags,
--> 668                 self.metadata)
669
670     def _backend(self, lowerfn, objectmode):

~/.local/lib/python3.7/site-packages/numba/compiler.py in native_lowering_stage(targetctx, library, interp, typemap, restype, calltypes, flags, metadata)
1061         lower = lowering.Lower(targetctx, library, fndesc, interp,
1062                                metadata=metadata)
-> 1063         lower.lower()
1064         if not flags.no_cpython_wrapper:
1065             lower.create_cpython_wrapper(flags.release_gil)

~/.local/lib/python3.7/site-packages/numba/lowering.py in lower(self)
175         if self.generator_info is None:
176             self.genlower = None
--> 177             self.lower_normal_function(self.fndesc)
178         else:
179             self.genlower = self.GeneratorLower(self)

~/.local/lib/python3.7/site-packages/numba/lowering.py in lower_normal_function(self, fndesc)
216         # Init argument values
217         self.extract_function_arguments()
--> 218         entry_block_tail = self.lower_function_body()
219
220         # Close tail of entry block

~/.local/lib/python3.7/site-packages/numba/lowering.py in lower_function_body(self)
241             bb = self.blkmap[offset]
242             self.builder.position_at_end(bb)
--> 243             self.lower_block(block)
244
245         self.post_lower()

~/.local/lib/python3.7/site-packages/numba/lowering.py in lower_block(self, block)
256             with new_error_context('lowering "{inst}" at {loc}', inst=inst,
257                                    loc=self.loc, errcls_=defaulterrcls):
--> 258                 self.lower_inst(inst)
259
260     def create_cpython_wrapper(self, release_gil=False):

~/.local/envs/dylan3/lib/python3.7/contextlib.py in __exit__(self, type, value, traceback)
128                 value = type()
129             try:
--> 130                 self.gen.throw(type, value, traceback)
131             except StopIteration as exc:
132                 # Suppress StopIteration *unless* it's the same exception that

~/.local/lib/python3.7/site-packages/numba/errors.py in new_error_context(fmt_, *args, **kwargs)
633         from numba import config
634         tb = sys.exc_info()[2] if config.FULL_TRACEBACKS else None
--> 635         six.reraise(type(newerr), newerr, tb)
636
637

~/.local/lib/python3.7/site-packages/numba/six.py in reraise(tp, value, tb)
657         if value.__traceback__ is not tb:
658             raise value.with_traceback(tb)
--> 659         raise value
660
661 else:

LoweringError: Failed in nopython mode pipeline (step: nopython mode backend)
'NoneType' object has no attribute 'groups'

File "../../../u/dnelson/python/util/sub.py", line 2566:
#def get_random_number():
<source elided>
if count != num:
raise Exception('Mismatch.')
^

[1] During: lowering "raise <class 'Exception'>('Mismatch.')" at /u/dnelson/python/util/subfind.py (2566)
-------------------------------------------------------------------------------
This should not have happened, a problem has occurred in Numba's internals.

# Fixed code
def _find_definition(self):
    # try and find a def, go backwards from error line
    fn_name = None
    lines = self.get_lines()
    for x in reversed(lines[: self.line - 1]):
        # the strip and startswith is to handle user code with commented out
        # 'def' or use of 'def' in a docstring.
        if x.strip().startswith("def "):
            fn_name = x
            break

    return fn_name