# Buggy code
def _store_object(self, obj):
    self._wr = weakref.ref(obj)


# Traceback (KeyError)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
c:\users\wiese\repos\ga\numba\numba\core\caching.py in IndexDataCacheFile.save(self, key, data)
482 try:
483     # If key already exists, we will overwrite the file
--> 484     data_name = overloads[key]
485 except KeyError:
486     # Find an available name for the data file

KeyError: ((type(CPUDispatcher(<function mul_self at 0x0000025918224C10>)), int64), ('x86_64-pc-windows-msvc', 'broadwell', '+64bit,+adx,-aes,+avx,+avx2,-avx512bf16,-avx512bitalg,-avx512bw,-avx512cd,-avx512dq,-avx512er,-avx512f,-avx512ifma,-avx512pf,-avx512vbmi,-avx512vbmi2,-avx512vl,-avx512vnni,-avx512vpopcntdq,+bmi,+bmi2,-cldemote,-clflushopt,-clwb,-clzero,+cmov,+cx16,+cx8,-enqcmd,+f16c,+fma,-fma4,+fsgsbase,+fxsr,-gfni,+invpcid,-lwp,+lzcnt,+mmx,+movbe,-movdir64b,-movdiri,-mpx,-mwaitx,+pclmul,-pconfig,-pku,+popcnt,-prefetchwt1,+prfchw,-ptwrite,-rdpid,+rdrnd,+rdseed,-rtm,+sahf,-sgx,-sha,-shstk,+sse,+sse2,+sse3,+sse4.1,+sse4.2,-sse4a,+ssse3,-tbm,-vaes,-vpclmulqdq,-waitpkg,-wbnoinvd,-xop,+xsave,-xsavec,+xsaveopt,-xsaves'), ('95d9e4857cb9755b29e175e557e615d8467821eb42c66a521b4270d86f6b8c06', 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'))

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-7-74c10e900da8> in <module>
7 @numba.njit(cache=True)
8 def compose_self_apply(f, x):
9         return f(f(x))
---> 11 compose_self_apply(mul_self, 2)

c:\users\wiese\repos\ga\numba\numba\core\dispatcher.py in _DispatcherBase._compile_for_args(self, *args, **kws)
431         e.patch_message('\n'.join((str(e).rstrip(), help_msg)))
432 # ignore the FULL_TRACEBACKS config, this needs reporting!
--> 433 raise e

c:\users\wiese\repos\ga\numba\numba\core\dispatcher.py in _DispatcherBase._compile_for_args(self, *args, **kws)
364         argtypes.append(self.typeof_pyval(a))
365 try:
--> 366     return self.compile(tuple(argtypes))
367 except errors.ForceLiteralArg as e:
368     # Received request for compiler re-entry with the list of arguments
369     # indicated by e.requested_args.
370     # First, check if any of these args are already Literal-ized
371     already_lit_pos = [i for i in e.requested_args
372                        if isinstance(args[i], types.Literal)]

c:\users\wiese\repos\ga\numba\numba\core\compiler_lock.py in _CompilerLock.__call__.<locals>._acquire_compile_lock(*args, **kwargs)
29 @functools.wraps(func)
30 def _acquire_compile_lock(*args, **kwargs):
31     with self:
---> 32         return func(*args, **kwargs)

c:\users\wiese\repos\ga\numba\numba\core\dispatcher.py in Dispatcher.compile(self, sig)
822     raise e.bind_fold_arguments(folded)
823 self.add_overload(cres)
--> 824 self._cache.save_overload(sig, cres)
825 return cres.entry_point

c:\users\wiese\repos\ga\numba\numba\core\caching.py in Cache.save_overload(self, sig, data)
664 """
665 Save the data for the given signature in the cache.
666 """
667 with self._guard_against_spurious_io_errors():
--> 668     self._save_overload(sig, data)

c:\users\wiese\repos\ga\numba\numba\core\caching.py in Cache._save_overload(self, sig, data)
676 key = self._index_key(sig, _get_codegen(data))
677 data = self._impl.reduce(data)
--> 678 self._cache_file.save(key, data)

c:\users\wiese\repos\ga\numba\numba\core\caching.py in IndexDataCacheFile.save(self, key, data)
491             break
492     overloads[key] = data_name
--> 493     self._save_index(overloads)
494 self._save_data(data_name, data)

c:\users\wiese\repos\ga\numba\numba\core\caching.py in IndexDataCacheFile._save_index(self, overloads)
537 def _save_index(self, overloads):
538     data = self._source_stamp, overloads
--> 539     data = self._dump(data)
540     with self._open_for_write(self._index_path) as f:
541         pickle.dump(self._version, f, protocol=-1)

c:\users\wiese\repos\ga\numba\numba\core\caching.py in IndexDataCacheFile._dump(self, obj)
566 def _dump(self, obj):
--> 567     return pickle.dumps(obj, protocol=-1)

TypeError: cannot pickle 'weakref' object

# Fixed code
def _store_object(self, obj):
    self._wr = _PickleableWeakRef(obj)