numbox.core.configurations
Overview
numba caches a compiled function only in a directory
where it can write the cache files, and it picks that directory when the function is decorated. If it finds none, the
decoration raises a RuntimeError, and so does the import of the module that holds the function. numbox decorates
many functions with caching enabled as its modules are imported. Its first import would then fail wherever numba has
no writable location for numbox’s files: in a read-only install whose user cache directory is read-only too, for
instance, or in an import from an .egg archive.
This module prevents that failure. It holds jit_options, the options that numbox passes to numba’s njit
when it compiles its own functions. It also holds the check, the function
uncached_where_no_cache_can_be_written(), which runs when this module is first
imported. The check asks numba whether it could cache a function in each directory of numbox. If the answer is no for
any one directory, numbox disables caching in jit_options and warns once, naming what to change. numbox’s functions
then compile without a cache in every process, with the same results.
A package built on numbox can run the same check on its own files, as Checking a package built on numbox shows.
Setting the options
numbox reads jit_options from the NUMBOX_JIT_OPTIONS environment variable once, when
numbox.core.configurations is first imported. The variable holds a JSON object, which numbox passes to njit as
keyword arguments. If the variable is unset, the object is {"cache": true}, and jit_options is the Python
dictionary {"cache": True}. To disable caching, set the variable before Python starts, or at least before numbox
is first imported:
export NUMBOX_JIT_OPTIONS='{"cache": false}'
numbox passes other keys, as in {"cache": true, "boundscheck": true}, unchanged to njit for the functions that
it compiles with these options. Keep "cache" in any value you set. Without it, most of numbox’s functions compile
without a cache, because njit does not cache by default. numba still caches two kinds of function in that case.
compile_kernel() enables caching for its kernels when no option names
"cache". numbox’s SQLite bindings also compile C callbacks, the functions that SQLite calls for numbox’s virtual
tables and table-valued functions. numbox compiles those callbacks with no option other than "cache". It reads that
option from jit_options and treats caching as enabled when the key is absent.
numba tests the cache option for truth, so the string "false" in {"cache": "false"} would leave caching enabled.
numbox therefore refuses a "cache" value that is not true or false, with a ValueError that names the
variable. It refuses a value that is not a JSON object in the same way.
Functions of your own can compile under the same options, so that one variable controls caching for numbox’s functions
and for yours. The file totals.py below defines one such function:
import numpy as np
from numba import njit
from numbox.core.configurations import jit_options
@njit(**jit_options)
def total(xs):
acc = 0.0
for x in xs:
acc += x
return acc
print(jit_options, total(np.arange(4.0)))
python totals.py prints {'cache': True} 6.0 and caches total; a second run loads total from the cache.
When the check disables caching for numbox, total is not cached either, even where numba could cache totals.py.
Checking a package built on numbox shows how to run the check on totals.py instead, so that numba caches
total wherever it can cache that file.
Where numba writes the cache
numba stores a cached function in an index file, whose name ends in .nbi, and in one data file for each compiled
signature, ending in .nbc. When the function’s source is a file on disk, numba writes them in the first of three
locations that it can write in. numba calls the class that chooses a location a
cache locator, and each location has
its own locator:
a subdirectory of
NUMBA_CACHE_DIR, if that environment variable is set, chosen byUserProvidedCacheLocator;the
__pycache__directory beside the source file, chosen byInTreeCacheLocator;a subdirectory of the user’s cache directory, chosen by
UserWideCacheLocator. The user’s cache directory is~/Library/Caches/numbaon macOS,%LOCALAPPDATA%\numba\Cacheon Windows, and$XDG_CACHE_HOME/numbaelsewhere, or~/.cache/numbaifXDG_CACHE_HOMEis unset.
numba names each subdirectory after the source file’s directory: the directory’s name, an underscore and a SHA-1 hash
of the directory’s path, such as core_df4387379757d214fdc85570af6767de3b7850dc.
numba picks the location when the function is decorated. It skips a location that it cannot create or write in, and it
issues no warning. If NUMBA_CACHE_DIR cannot be written in, numba therefore uses the next location. If no
location is left, the decoration raises RuntimeError: cannot cache function 'f': no locator available for file ....
The variable NUMBA_CACHE_LOCATOR_CLASSES can reorder the
locators or leave some out.
Three other kinds of source have locations of their own:
a function defined in an IPython or Jupyter cell:
numba_cacheunder IPython’s cache directory, chosen byIPythonCacheLocator;a module imported from a
.ziparchive: a subdirectory of the user’s cache directory, chosen byZipCacheLocator;a module of a frozen application, such as one built with PyInstaller: a subdirectory of the user’s cache directory, chosen by
UserWideCacheLocator.
NUMBA_CACHE_DIR does not change the location of any of these, because numba reads it only for a source file on disk.
A source of any other kind has no location at all: a module in an .egg archive, for instance, or one present only as
a .pyc file.
Reading the warning
When numbox.core.configurations is first imported, the check asks numba where it would cache a function defined in
one file from each directory of numbox. If numba could not cache a function defined in one of those files, numbox
compiles all of its functions without a cache and warns once, as here:
RuntimeWarning: numba cannot cache numbox here (cannot cache function '_cache_probe': no locator available for
file '/opt/python/lib/python3.12/site-packages/numbox/core/configurations.py'); it compiles without a cache. Set
NUMBA_CACHE_DIR to a writable directory, or NUMBOX_JIT_OPTIONS='{"cache": false}' to turn caching off and silence
this warning
The warning has three parts. The parentheses contain the error that stopped the check, here numba’s own error: no
locator accepted the file. In that error, _cache_probe is the probe, a stand-in function that the check builds to
ask numba about a file; How numbox checks describes it. The last sentence is the remedy, which says what to change so
that numba can cache. Every remedy ends with a clause that suggests disabling caching instead. Following that advice
silences the warning, because the check does not run when "cache" is false.
Each case below says first what to do, and then quotes its remedy so that you can match it to your warning.
If numbox is installed read-only
Set NUMBA_CACHE_DIR to a writable directory in the environment, before Python starts:
export NUMBA_CACHE_DIR=/tmp/numba-cache
numba then caches numbox’s functions, and yours, under that directory. On a machine that other users share, choose a directory that only you can write in, because numba loads and runs the compiled code that it finds there.
The warning under Reading the warning is this case, and its remedy is the fragment
Set NUMBA_CACHE_DIR to a writable directory. The case arises where the directories that hold numbox are read-only
to you, as in a system Python or a shared environment. numba cannot write in the __pycache__ beside numbox’s
sources. It uses the user’s cache directory instead, and it issues no warning. numbox warns only if that directory
cannot be written in either, as with a read-only home directory on some cluster nodes and in some containers.
If NUMBA_CACHE_DIR is set already, change it to a writable directory with a short path. numba skips a directory that
it cannot write in. It also skips a directory whose path is so long that the subdirectory it would create there exceeds
the path limit. The remedy then names the directory, and it begins:
NUMBA_CACHE_DIR is set to /scratch/numba-cache, which numba could not use: set it to a writable directory at a
short path
If NUMBA_CACHE_LOCATOR_CLASSES lists locators without UserProvidedCacheLocator, numba never reads
NUMBA_CACHE_DIR. Make one of the listed locations writable, or add UserProvidedCacheLocator to the list. The
remedy repeats the value of NUMBA_CACHE_LOCATOR_CLASSES, here InTreeCacheLocator,UserWideCacheLocator, and
begins:
numba looks only where NUMBA_CACHE_LOCATOR_CLASSES, InTreeCacheLocator,UserWideCacheLocator, says: make one of
those locations writable
If numbox is imported from an archive
If numbox comes from an archive whose name does not end in .zip, such as a .whl, an .egg or a .pyz,
install numbox with its source files on disk. Or rename the archive so that its name ends in the lower-case .zip,
with numbox’s source files in it. Python can import modules from a
zip archive of any name, but numba caches a module from an archive only if the archive’s name ends in .zip. The
.egg case arises with Spark, which ships .zip and .egg files to its executors through the --py-files
option of spark-submit. The
remedy for this case begins:
NUMBA_CACHE_DIR has no effect here, because the source is not a file on disk: to cache, install numbox with its
source files on disk, unpacked from any archive, or rename the archive, /jobs/numbox-0.0.0-py3.12.egg, to end in
.zip, lower-case, which numba's .zip locator looks for in the path, with its source files in it, so that numba
caches it in the user's cache directory
For a .zip, numba caches each directory of the archive in a subdirectory of the user’s cache directory, named as
above, and NUMBA_CACHE_DIR has no effect. If your home directory is read-only, make the user’s cache directory
writable, or set its location to a writable directory. XDG_CACHE_HOME or HOME sets its location on Linux, and
HOME sets it on macOS. On Windows, numba queries the system for the location, and no environment variable changes
it. The remedy names both actions regardless of the error. It therefore asks for free space even when, as here, the
error is Permission denied. It begins:
numba caches a .zip, or a frozen application, in the user's cache directory, and NUMBA_CACHE_DIR has no effect
here, because the source is not a file on disk: no file can be written in that directory,
/home/user/.cache/numba/core_1544a931bcaa3f414cb218856f1bf59ef75bcc9f, which cannot be made under /home/user
(Permission denied), so make room there, or make it writable
numba uses a .zip’s location without testing that it can be written in. Without the check, numba’s first save
would raise an OSError. numbox compiles some of its functions as its modules are imported, so the import would
fail; a function that numba compiles lazily fails at its first call instead. The check runs numba’s test on that
location, as numba runs it for every other kind of location.
The check is stricter than numba in one case. numbox warns for a .zip whose location still holds every cache entry
but can no longer be written in, although numba by itself would load those entries. numba’s .zip
locator lacks the test, as numba issue 10888 reports.
If numbox is bundled with PyInstaller
A program bundled with PyInstaller imports numbox from an archive inside the bundle. numba recognizes such a frozen
application by sys.frozen, which PyInstaller sets, and caches its functions in the user’s cache directory. If
numba cannot use that directory, make it writable, or set XDG_CACHE_HOME or HOME so that the directory has a
shorter path. Up to its first colon, the remedy is the same as the remedy for a .zip. After that colon, it reads:
numba could not use that directory, /home/user/.cache/numba: make it writable, or put it at a shorter path,
through XDG_CACHE_HOME or HOME
If numba 0.68 cannot read an archive member
A file inside a .zip is called a member. From version 0.68, numba reads the source of each member when it sets up
the member’s cache. It hashes that source into the stamp, the value that numba stores with a cache entry to
tell whether the source has changed. Three cases fail at that step:
A directory whose name ends in
.zipprecedes the archive in its path, as/jobs.zipdoes in/jobs.zip/numbox.zip. Put the archive elsewhere. numba treats the first part of the path that ends in.zipas the archive, here the directory/jobs.zip, and opening a directory fails. On Linux, the remedy contains the wordsput the archive under a path with no such directory above it.On Windows, numba 0.68.0 builds each member’s name with backslashes and finds no member of that name, so it can cache no module in a
.zip. Upgrade numba, or install numbox with its source files on disk. The bug is numba issue 10889, fixed in numba 0.68.1.A member shipped as a
.pycfile alone has no source in the archive. The section If numbox is installed without its source files describes that case.
In IPython and Jupyter
numbox’s own files are not cells, so numba caches numbox’s functions in a notebook as in any
other process. For functions you define in a cell, numba writes the cache files in numba_cache under IPython’s
cache directory, one directory for all cells. On Linux, and on macOS from IPython 8, IPython’s cache directory is
ipython under XDG_CACHE_HOME, or under ~/.cache, if that base directory exists and can be written in.
Otherwise it is IPython’s own directory: ~/.ipython, or the directory that
IPYTHONDIR names.
numba reads the function’s source when it sets up the cache, so it caches a cell’s function only while IPython holds
the cell’s source.
If IPython can write in neither its cache directory nor its own directory, set IPYTHONDIR, or XDG_CACHE_HOME on
Linux or macOS, to a writable directory before IPython starts. Otherwise the first call of a cached function in a cell
fails with a FileNotFoundError. IPython warns first:
UserWarning: IPython parent '/home/user' is not a writable location, using a temp directory.
IPython then gives numba a new temporary directory each time numba asks for the cache directory. numba asks more than
once, so it creates numba_cache in one temporary directory and then saves in another, where no numba_cache
exists, and the save fails.
numbox itself never runs the check on a cell. A package that runs it on a function that its user defined in a cell, as Checking a package built on numbox shows, gets a remedy for this case, which begins:
numba caches this file in numba_cache under IPython's cache directory, and IPython, able to write neither that
directory nor its own, gives a new temporary directory on every call, so numba makes numba_cache in one and saves
in another: set IPYTHONDIR, or XDG_CACHE_HOME on a POSIX system, to a writable directory
Two other remedies concern cells only. If IPython no longer holds a cell’s source, the remedy says that numba caches a
cell only while IPython holds its source. If NUMBA_CACHE_LOCATOR_CLASSES does not list IPythonCacheLocator, the
remedy asks for it to be listed, because no other locator accepts a cell.
If numbox is installed without its source files
If numbox is installed from .pyc files alone, install it with its .py files, or import it from a .zip that
holds them; NUMBA_CACHE_DIR does not help. A .pyc file records the path of the source it was compiled from, and
numba places the cache by that recorded path. When no file exists at that path, no locator accepts it. A single module
without its source makes the check disable caching for all of numbox. numba’s error in the warning names that module’s
missing .py file. The remedy begins:
NUMBA_CACHE_DIR has no effect here, because the source is not a file on disk: to cache, install numbox with its
source files on disk, unpacked from any archive, or import it from a .zip holding its source files, which numba
caches in the user's cache directory
A .pyc file compiled from a tree that is still on disk elsewhere records a path in that tree, and numba caches the
module as though it ran from there.
In a .zip, put the .py files in the archive, or install numbox with its source files on disk. A .pyc member
can record its own path in the archive, as the
-d option of compileall sets it.
numba 0.68 and later read the member’s source from the archive at that path, and a member shipped as a .pyc file
alone has no source there. The error in the warning is then zipfile’s
There is no item named 'numbox/core/bindings/libm.py' in the archive, and the remedy begins:
numba 0.68 and later read a .zip member's source from the archive, and this one holds none under
numbox/core/bindings/libm.py, the module shipped as .pyc alone: to cache, install numbox with its source files on
disk, unpacked from any archive, or import it from a .zip holding its source files, which numba caches in the
user's cache directory
If a path or a name is too long
If a location lies so close to the path limit, 4096 bytes on Linux, that numba’s cache file names no longer fit, shorten
the location that numba chose. The remedy contains the words path is too long for the file system and names what to
shorten, according to the location that numba chose:
under
NUMBA_CACHE_DIR: a shorterNUMBA_CACHE_DIR;the
__pycache__beside numbox’s sources: numbox installed at a shorter path, orNUMBA_CACHE_DIRset to a short path;under the user’s cache directory: that directory at a shorter path, set through
XDG_CACHE_HOMEorHOME, orNUMBA_CACHE_DIRset to a short path;under the user’s cache directory for a
.zip: that directory at a shorter path, set the same way, whileNUMBA_CACHE_DIRhas no effect.
numba’s own test of a location passes such a location, so without the check numba would fail later with
File name too long, as How numbox checks explains.
A name can be too long regardless of its path. If the file system that holds the location cannot store a file name of
128 bytes, no shorter path helps: set NUMBA_CACHE_DIR to a directory on a file system that allows such names. The
remedy then contains the words so no shorter path cures it.
The 128 bytes bound the cache file names that numba writes for numbox’s own functions, numba’s temporary suffix
included, and a numbox test checks that they stay under it. The names of generated code also remain within the limit.
make_structref() generates a numba StructRef type, called a struct below, from a name
and a list of fields. It shortens long names in the code that it writes. If generated code cannot be cached describes
that code.
If a path is not UTF-8
On Linux, a file name can hold bytes that are not UTF-8. numba names its subdirectory under NUMBA_CACHE_DIR or the
user’s cache directory with a hash of the source directory’s path, encoded as UTF-8. For a path that is not UTF-8,
that encoding raises a UnicodeEncodeError. The __pycache__ beside the source has no hash in its name, so it is
the one location left.
For numbox installed under such a path, unset NUMBA_CACHE_DIR and make sure that numba can write in the
__pycache__ beside numbox’s sources; or install numbox under a UTF-8 path. With the variable set, the remedy says
that NUMBA_CACHE_DIR “is hashed for first”. These words mean that numba tries the variable first and
fails to hash the path for it. That remedy begins:
numba hashes the source's directory as UTF-8 for the directory it caches this file in under NUMBA_CACHE_DIR or the
user's cache directory, and /srv/caf\udce9/numbox/core is not, and NUMBA_CACHE_DIR, which is set to
/scratch/numba-cache, is hashed for first: unset it, and numba caches beside the source, in its __pycache__,
/srv/caf\udce9/numbox/core/__pycache__, hashing no path; or put the file under a UTF-8 path
If the variable is unset and the install is read-only, the remedy begins:
numba hashes the source's directory as UTF-8 for the directory it caches this file in under NUMBA_CACHE_DIR or the
user's cache directory, and /srv/caf\udce9/numbox/core is not: put the file under a UTF-8 path, or make the
__pycache__ beside the source, /srv/caf\udce9/numbox/core/__pycache__, writable, where InTreeCacheLocator caches
it hashing no path
If generated code cannot be cached
numbox also generates Python source at run time and compiles it. A warning that names a generated file, such as
Struct_ce9e24b31bee6866.py, comes from that code:
RuntimeWarning: numba cannot cache Struct_ce9e24b31bee6866.py here ([Errno 20] Not a directory:
'/scratch/numba-cache/numbox-structref'); it compiles without a cache. Set NUMBA_CACHE_DIR to a writable
directory, or compile without a cache to silence this warning: "cache" off in the jit options this code was given,
or in its cache argument where it takes one, or NUMBOX_JIT_OPTIONS='{"cache": false}' where the options are the
package's
numbox could not write the file that holds the generated code, or numba could not cache a function of that file. The
code still works, but it compiles without a cache in every process. To cache it, follow the remedy, here
NUMBA_CACHE_DIR set to a writable directory.
Four kinds of numbox function generate code. make_structref() generates a struct.
compile_kernel() generates a kernel for a graph of formulas.
make_graph() builds a graph of Work nodes, and it compiles the derive function of
each Derived spec: the Python function that computes that node’s value from the
values of the nodes it depends on. The SQLite registrations
register_aggregate(),
register_window() and
register_tvf() generate the callbacks that SQLite calls.
numba caches a function only if it can find the function’s source, and a function compiled from a string has none.
@njit(cache=True) on such a function g raises
RuntimeError: cannot cache function 'g': no locator available for file '<string>'. numbox therefore writes each
piece of generated code to a file, whose name carries a digest, so that each version of the code has a file of its
own. numbox writes the files under NUMBA_CACHE_DIR, or under the user’s cache directory if the variable is unset, in
directories such as numbox-structref (where the generated code is written).
That directory can be unwritable even where numba caches numbox’s own functions, because numba caches those beside their
sources.
The generating functions determine their njit options in different ways. make_structref uses the options you pass
without merging them into numbox’s options. compile_kernel and make_graph merge your options into numbox’s, and
your values take precedence for the keys that you set. NUMBOX_JIT_OPTIONS therefore still controls caching unless
your options name "cache". The cache argument of compile_kernel overrides both your options and
NUMBOX_JIT_OPTIONS. The SQLite registrations always use numbox’s options. The last clause of the warning quoted
above therefore gives three ways to compile without a cache, and what it calls “the package’s” options are numbox’s.
When the file of a derive function cannot be cached, the derive function compiles without a cache too, but numbox gives no warning for it.
For a path that is too long, the remedy contains the words path is too long for the file system and asks for
NUMBA_CACHE_DIR to be set to a shorter path. numbox shortens the names of the generated files, so only the directory
can make the path too long. On Windows, a path component that is too long fails with [WinError 123] instead, and the
remedy is the general one, Set NUMBA_CACHE_DIR to a writable directory. A shorter NUMBA_CACHE_DIR fixes that
case too.
make_graph also generates the graph maker, the function that builds the graph’s nodes, which
the builder overview describes. It compiles the graph maker as though it were defined in numbox’s
builder module, so numba caches it beside that module. Options you pass to make_graph can enable caching for the
graph maker where the check disabled it for numbox. make_graph therefore runs the check on the builder
module’s file under those options. Its warning begins numba cannot cache the code generated at builder.py here and
gives the remedy for numbox’s own files.
The utils page explains why each version of the code needs a file of its own, under Cache-anchor mechanism.
How numbox checks
The check runs once per process, when the module’s first import sets jit_options. It is skipped only if the
options set "cache" to false. The check still runs for a value of NUMBOX_JIT_OPTIONS without "cache",
because numba caches the kernels of compile_kernel and the SQLite callbacks under such options. If the check then
fails, numbox warns once, and jit_options contains "cache": False.
numba picks a location for a function, not for a file, so the check uses the probe to ask numba about a file.
check_cache_location() has Python compile a two-line function named
_cache_probe as though it were defined in the file in question. It passes the probe to numba’s cache set-up, as
numba does for a function decorated with cache=True. numba then picks a locator by its own rules, in its own order
or in the order that NUMBA_CACHE_LOCATOR_CLASSES gives, and raises its own error if no locator accepts the file.
numba never compiles the probe.
The check then repeats the other steps that numba takes at decoration and at the first save:
It reads the source’s stamp, which from numba 0.68 is a hash of the source, so the source must be readable.
It runs numba’s test that the location can be written in. numba’s decoration runs that test for every location except that of a
.zip.It asks numba for the location once more, as numba does at each save.
It creates and removes a file with a name of 128 bytes.
The last step matters for a location near the path limit. numba’s own test creates a temporary file. On Linux, that file
is usually unnamed. It has no entry in the directory, so it has no name that could be too long. Elsewhere its name is
short. The names of the files that numba saves later include the source file’s name, the function’s qualified name and
line, and the Python version. numba writes each of them first under a temporary name 21 bytes longer. The 128 bytes
allow for the longest such name for numbox’s functions, temporary suffix included. Without this step, a location close
to the path limit would pass numba’s test. numba would then fail with File name too long the first time it opened a
function’s index file, which for numbox happens at import.
The check asks first about this module’s own file, and then about one source file in each directory of numbox that
contains a module, because numba’s __pycache__ locations are per directory. It follows symbolic links, so it also
asks about a directory of numbox that is a link into another tree, which may be read-only. When a module is present
only as a .pyc file, the check asks about the file that the .pyc file records. For a .zip, the check lists
the archive’s directories, because an archive has no directories on disk.
The check stops at the first file that numba cannot cache, and it then disables caching for all of numbox. It does so whether or not the modules in that file’s directory define a cached function. Compiling without a cache costs compile time in each process, but it never gives a wrong result.
Empty cache directories are an expected side effect of the check. For each file it asks about, the check creates the
cache directory that numba would use, one for each directory of numbox. numba itself creates such a directory only
when it caches a function there, and for a .zip only at the first save. Some of these directories therefore hold
none of numba’s files until a function in them is compiled. The cache directory for numbox’s top-level directory never
holds any of numba’s files, because the top-level directory holds no compiled code.
The error in a warning’s parentheses is usually numba’s own error, as numba would raise it at decoration. In three cases, the check raises its own error instead. That error names the location that numba chose. The three are a failed test of the location, a 128-byte file that cannot be created, and a temporary directory from IPython that does not exist.
Only numba’s cache errors lead to the warning. They are the errors for which
is_a_cache_error() returns True:
any
OSError;numba’s
RuntimeErrorwhen no locator accepts the file;numba’s
ValueErrorfor a path that contains.zipbut has no part that ends in.zip;the
KeyErrorthat numba 0.68 and later raise for a.zipmember without its source;the
UnicodeEncodeErrorfor a path that is not UTF-8.
The check raises any other error unchanged, so a fault elsewhere in numba appears as an error, not as a warning.
cache_remedy() composes the remedy from the error and the file. It compares the path
that the error names with the location that each locator would give the file. It considers the locators in the order
that numba tries them, and it composes the remedy for the first locator whose location matches. For an error that names
no file, such as one raised for a full disk, it lists the locations that numba could have chosen. The docstring of
cache_remedy, under Modules, describes the cases that it distinguishes.
Checking a package built on numbox
If numba caches the functions of your package, the cases above apply to your package as they do to numbox. Three
functions of this module let you run the same check on your own files.
check_cache_location() asks numba about one file and raises numba’s error if numba
could not cache a function of it. is_a_cache_error() distinguishes those errors from
all other errors. cache_remedy() composes the remedy.
The module below, kernels.py, enables caching for its functions only where numba can cache them. It passes
longest_file_name=128, a bound on the length of the cache file names that numba writes for it; the rule for your own
bound follows the example.
"""Kernels that numba caches only where it can write a cache for this file."""
import inspect
import os
import warnings
from numba import njit
from numbox.core.configurations import cache_remedy, check_cache_location, is_a_cache_error
def _cache_probe():
"""Never compiled: the check asks numba about the file that holds this function's code."""
def _jit_options():
if os.environ.get("KERNELS_CACHE") == "0":
return {"cache": False}
py_file = inspect.getfile(_cache_probe)
try:
check_cache_location(py_file, longest_file_name=128)
except (RuntimeError, OSError, KeyError, ValueError) as error:
if not is_a_cache_error(error):
raise
silence = "KERNELS_CACHE=0 to turn caching off and silence this warning"
remedy = cache_remedy(py_file, error, silence, package="kernels")
warnings.warn(f"numba cannot cache kernels here ({error}); it compiles without a cache. {remedy}",
RuntimeWarning, stacklevel=2)
return {"cache": False}
return {"cache": True}
jit_options = _jit_options()
@njit(**jit_options)
def scale(x, factor):
return x * factor
A second file in the same directory, use_kernels.py, calls scale:
from kernels import scale
print(scale(2.0, 3.0))
python use_kernels.py prints 6.0 and caches scale; a second run loads scale from the cache. Where
numba cannot cache kernels.py, the import of kernels warns once, and scale compiles without a cache:
RuntimeWarning: numba cannot cache kernels here (cannot cache function '_cache_probe': no locator available for
file '/srv/kernels/kernels.py'); it compiles without a cache. Set NUMBA_CACHE_DIR to a writable directory, or
KERNELS_CACHE=0 to turn caching off and silence this warning
The module asks about inspect.getfile(_cache_probe), the file that the function’s code records, because that is
the file numba looks up. A module’s __file__ can name another file: for a module present only as a .pyc file,
it names the .pyc, while numba looks for the .py file the .pyc was compiled from. The except clause
names the four classes that numba’s cache errors belong to; a UnicodeEncodeError is a ValueError, so the
clause catches it too. The package argument names your package in the remedies that ask to install it again. The
silence argument supplies the clause that ends every remedy.
Choose longest_file_name from the names that numba writes. For a function compiled with njit, numba builds
each cache file name from four parts:
stem, the module’s file name without.py;qualname, the function’s qualified name, with any angle brackets removed;line, the line of the function’s first decorator;the Python version, written as
pyand themajorandminorversion numbers, followed by anyabiflags, as inpy312.
The index file adds .nbi, and each data file adds a dot, a counter that starts at 1, and .nbc. numba writes
every file first under a temporary name 21 bytes longer. For up to 999 compiled signatures, the longest name is
therefore len(f"{stem}.{qualname}-{line}.py{major}{minor}{abiflags}.999.nbc".encode()) + 21 bytes. Allow one
byte more for each further digit of the counter.
For kernels.py, the formula gives 51 bytes, which is below the bound of 128. LONGEST_CACHE_FILE_NAME, numbox’s
own bound of 128 bytes, covers numbox’s functions only. A location that fits numbox’s names but not yours passes the
check, and numba fails later with File name too long.
numba’s cache locations are per directory, so if your package has modules in several directories, check one file in
each. Define a _cache_probe in one module of each directory, run the check on the file of each, and stop at the
first error, as numbox does. The same calls can check a function that a user passes to your package, through
check_cache_location(inspect.getfile(func), ...). That file is a cell when the user works in a notebook, and the
remedies under In IPython and Jupyter then apply.
A name in your package can be too long even where numbox’s names are within the limit. numba names its subdirectory
under NUMBA_CACHE_DIR or the user’s cache directory with the source directory’s name, an underscore and the 40
hexadecimal digits of a SHA-1 hash. The subdirectory’s name is 41 bytes longer than the source directory’s name. A
directory whose name is longer than 214 bytes therefore has a subdirectory name longer than 255 bytes, the limit of
nearly every file system. The remedy then names the subdirectory and its length, with the words
261 bytes, which is longer than a name can be on that file system (255). It asks for the file to be put in a
directory with a shorter name. As an alternative, it offers the __pycache__ beside the source, whose name contains
no hash. The limit in parentheses is the one that the file system holding the location reports, or 255 where the file
system does not report a limit. When NUMBA_CACHE_DIR is unset, the remedy has no directory whose file system it
could query, so it measures the name against 255.
A function’s own name can also be too long. numba’s cache file names for it then exceed the file system’s limit, and
the function’s first call fails with File name too long, when numba opens the function’s index file. Rename the
function or its module. Given that error, cache_remedy asks for a shorter name for the function or its file. Its
last clause says that the shorter name “is kernels’s to give”. These words mean that only your package can choose the
new name. If kernels.py held a function whose name had 260 letters, the remedy would read as follows, with the
name shortened here:
numba names the cache files for a function after the source's name and the function's qualified name,
kernels.fff...fff-4.py312.nbi, 280 bytes, which is longer than a name can be on that file system (255), and no
shorter path cures it: a shorter name for the function or its file, which is kernels's to give
The remedy asks for a shorter name only for numba’s own error, which names the long file. If the
longest_file_name you pass is itself longer than the file system allows, the remedy is different. It offers
NUMBA_CACHE_DIR set to a directory on a file system that allows names of that length. It does not ask for a shorter
name.
Modules
numbox.core.configurations
numbox’s njit options, and the check that disables caching where numba cannot cache numbox.
jit_options holds the keyword arguments that numbox passes to numba’s njit for its own functions. When this
module is first imported, it reads them from the NUMBOX_JIT_OPTIONS environment variable. Unless they set
"cache" to false, it then asks numba whether it could cache a function in each directory of numbox. If numba
could not, jit_options has "cache" set to False, and one RuntimeWarning says what to change.
check_cache_location(), is_a_cache_error() and cache_remedy() run the same check for a package built
on numbox. The module also holds MAX_STR_LENGTH, function_struct_size and _strict_cache_mode, which returns
whether NUMBOX_PROXY_CACHE_STRICT selects strict validation of @proxy cache loads. Other numbox modules read
these.
- numbox.core.configurations.LONGEST_CACHE_FILE_NAME = 128
The bound, in bytes, on the length of the cache file names that numba writes for numbox’s own functions, including numba’s temporary suffix. The check creates and removes a file with a name of that length in each location that it asks numba about.
- numbox.core.configurations.cache_remedy(py_file, failure, silence, package='numbox')[source]
Return the remedy for
failure, numba’s cache error for a function whose source file ispy_file.The remedy is one sentence. It names what numba tried and why that failed, says what to change, and ends in
silence, the clause that says how to disable caching.py_filecan be a string, bytes or a path object, and so can the file name thatfailurecarries; both are read as strings.packagenames the package in the remedies that ask for it to be installed again, at a shorter path or with its source files on disk. A package built on numbox passes its own name, as it passes its ownlongest_file_nametocheck_cache_location(); numbox passesLONGEST_CACHE_FILE_NAME.The remedy reads
NUMBA_CACHE_DIRandNUMBA_CACHE_LOCATOR_CLASSESfromnumba.config, the user’s cache directory,sys.frozen, and the archive abovepy_file, if there is one. It asks IPython for its cache directory only if numba reaches a locator of its IPython family that accepts the file. IPython under a home directory that it cannot write warns each time it is asked, and it creates a temporary directory that remains on disk.Most of the remedy depends on the locator that accepted the file. The remedy compares the path that the error names, and that path’s directory, with the location that each locator numba reaches would choose for the file. It compares whole paths, in numba’s order, and attributes the error to the first matching locator. numba’s own errors mean that the locators before that one skipped the file; an error that a caller passes on need not mean that. If the error names a path that no locator would choose for the file, the remedy names that path as a location that none of numba’s locators chose.
If the error names no file, the remedy names every location that numba could have chosen, each once, in numba’s order. Such an error has no file name, an empty one, or a name of another kind, such as an integer file descriptor. numba’s first save raises one when a write fails, as on a full disk. A path object never counts as empty: pathlib reads an empty string as the working directory,
., and the remedy names that directory as a location that none of numba’s locators chose.NUMBA_CACHE_LOCATOR_CLASSESsets the order. Each entry is a class of numba’s caching module; a subclass of one, named by its dotted path, which the remedy treats as its parent; or a class outside numba’s families. The remedy asks a class outside them about the file with a probe, as numba asks it, through the class’sfrom_function. Such a class caches in a place that only the class can name, and the remedy calls that place the class’s own. An error that names a path that none of numba’s locators chooses may name that place. numba reaches no locator listed after one that accepts the file. Its.ziplocator accepts every path that contains.zipwithout trying its location, so numba reaches no locator listed after it for such a path.The remedy distinguishes these cases.
- A source file on disk
numba reads
NUMBA_CACHE_DIRonly for a source file on disk, so the remedies for such a file offer it. The remedy for numba’s error that no locator accepts the file names the variable as one that numba could not use, if it is set, and asks for a writable directory at a short path. If the variable is unset, the remedy asks for it to be set to a writable directory. IfNUMBA_CACHE_LOCATOR_CLASSESomits the user-provided locator, numba never reads the variable, and the remedy asks for the listed locations to be made writable.An
OSErrorthat names the location that numba chose, or a file in it, means that the location refused a file after numba’s own test passed, as on a full disk or with permissions changed since. The remedy names the location and the reason, and asks for free space or a writable directory there. As the alternative, it offersNUMBA_CACHE_DIRif numba tries the variable before that location’s locator, or the variable set to another directory if the location is the variable’s own. The user-wide and.ziplocators choose the same location for a file, so for that location the remedy offers the variable if numba tries it before the last of the two. The remedy offers the variable for a location that none of numba’s locators chose only if numba tries the variable first. In these two cases the remedy does not say that numba could not use the variable.- A path too long for the file system
The remedy names what shortens the location that numba chose. That is a shorter
NUMBA_CACHE_DIRfor the variable’s location, the package installed at a shorter path for the__pycache__beside the source, and IPython’s cache directory at a shorter path for IPython’s location. The remedy for a location under the user’s cache directory is that directory at a shorter path, set throughXDG_CACHE_HOMEorHOME, or throughHOMEalone on macOS; on Windows, no environment variable sets it. For a source file on disk, the remedy also offersNUMBA_CACHE_DIRset to a short path, on the terms on which it offers the variable for a refused file.- A directory name too long for the file system
numba names its directory under
NUMBA_CACHE_DIRor the user’s cache directory after the source’s directory. If that name is longer than the file system holding the location allows, numba cannot create the directory at any path. The remedy then names the directory’s name and its length, and asks for the file to be put in a directory with a shorter name. It does so for an error that names such a location, and for a frozen application’s error. It does so too for numba’s error that no locator accepts a file on disk, if every locator tried that names its directory this way found the name too long. The.ziplocator’s error once the locators before it skipped the file counts as such an error. If the user-provided locator is the only one tried andNUMBA_CACHE_DIRis unset, the remedy also asks for the variable to be set.For a file on disk, the remedy adds the
__pycache__beside the source, whose name holds no hash. It asks for that directory to be made writable if numba reaches the in-tree locator. Otherwise it asks for the in-tree locator to be listed, before the.ziplocator or the outside class that accepted the file if the list puts it after one.- A cache file name too long for the file system
numba names the files that it writes for a function after the source’s name and the function’s qualified name. If numba could not create or open such a file in the location that it chose, because the name is too long for that file system, the remedy names the file and its length. It says that no shorter path avoids the error, and asks for a shorter name for the function or its file, which only the package can give. If some file system in use allows the name, the remedy also offers
NUMBA_CACHE_DIRpointed at a directory on such a file system. It offers the variable as set if the variable’s own locator accepted the file, and otherwise on the terms on which it offers the variable for a path too long.The remedy for the check’s error about a name of
longest_file_namebytes, on a file system that allows no name of that length, names the length. It offersNUMBA_CACHE_DIRon the same terms, regardless of the length. The remedy offers nothing for a source that is not a file on disk, because numba reads the variable only for a file on disk.- A path that is not UTF-8
numba names its directory under
NUMBA_CACHE_DIRor the user’s cache directory with a hash of the source’s directory path, encoded as UTF-8. A locator that names its directory this way raises at decoration for a path whose bytes are not UTF-8, when numba reaches it. For such a path, the remedy names no location under either directory, and where it would offerNUMBA_CACHE_DIRit says that setting the variable does not fix the error. For a.zipunder such a path, the remedy asks for the archive to be put under a UTF-8 path, because the.ziplocator hashes the path the same way. For a frozen application, which the user-wide locator hashes, it asks the same of the application. For a source file on disk, it also offers the__pycache__beside the source, because the in-tree locator hashes no path:If the list omits the in-tree locator, or puts it after a hashing locator other than the variable’s locator, the remedy asks for the in-tree locator to be listed before the first locator that hashes.
If the variable’s locator is the only one that hashes before the in-tree locator, the remedy asks for
NUMBA_CACHE_DIRto be unset.If the in-tree locator comes before every locator that hashes and skipped the file, the remedy asks for the
__pycache__to be made writable.
With either of the first two, the remedy also asks for the
__pycache__to be made writable if numba cannot write in it.- A file that numba’s IPython locator accepts
numba’s IPython locator accepts a cell, named
<ipython-...>, and any file in anipykernel_directory. It caches every such file innumba_cacheunder IPython’s cache directory, the same directory for every file. It skips that location if it cannot create it or write in it. No other locator of numba’s accepts a cell, so numba then raises its error that no locator accepts the file. numba raises the same error for a.zipmember in anipykernel_directory if the list puts no.ziplocator after the IPython one. The remedy asks for that location to be made writable, or for IPython’s cache directory to be put at a shorter path. If IPython raised for its directory, the remedy asks for IPython’s home to be made writable. It names a class outside numba’s families listed with the IPython locator as having skipped the file too. For a path too long, it asks for IPython’s cache directory at a shorter path. That holds for a cell file on disk and for a.zipmember in anipykernel_directory alike, if the IPython locator is listed before the.zipone.If IPython is not importable, numba raises
ImportErrorat decoration for a file that the IPython locator accepts, which is not a cache error. The remedy then treats an error that a caller passes on and that names IPython’s location as an error of a location that none of numba’s locators chose, if another locator that numba reaches has a place for the file. Otherwise, for a file on disk, the remedy names the list, as for numba’s error that no locator accepts the file. If IPython raises anOSErrorfor its directory, numba skips the locator, and the remedy has no location of IPython’s to compare the error with.If IPython can write neither its cache directory nor its own directory, it gives a new temporary directory as its cache directory on every call. numba then creates
numba_cachein one such directory and saves in another. Its first save raisesFileNotFoundErrorfor a file in thenumba_cachethat it could not save in. If the IPython locator accepts the file, the remedy for such an error names that cause, and asks forIPYTHONDIR, orXDG_CACHE_HOMEon a POSIX system, to be set to a writable directory. An error with another errno that names anumba_cacheis one that a caller built, and the remedy treats it as an error of the location that it names.inspect’s error, which has no errno and no file name, means that the IPython locator read no source before it tried its location. For a cell, the remedy says that numba caches a cell only while IPython holds its source, which numba reads from linecache. For a cell file on disk, the remedy asks for the file to be made readable, with the source in it; inspect raises for a file that cannot be read and, before Python 3.13, for an empty one. The remedy for a
.zipmember whose archive holds no source for it, a.pycalone, is the archive’s remedy, which asks for the source files to be shipped. If a member’s source is not UTF-8, the remedy asks for it to be shipped as UTF-8.- A
.zipor a frozen application numba caches both in the user’s cache directory, and
NUMBA_CACHE_DIRhas no effect on them. The remedy asks for free space or a writable directory in the location that numba chose, with the reason that the error gives, such as a full disk or permissions. A.zip’s error names that location, a directory of numba’s under the user’s cache directory, or a file in it, as the first save’s error does; the remedy names the directory. If the directory does not exist, the remedy names the existing directory above it, under which it cannot be created. If a file occupies a part of the location’s path, the remedy asks for that file to be put elsewhere. If the error names a location that none of numba’s locators chose, the remedy names that location as the error gives it, and does not mention the user’s cache directory.A frozen application’s error is numba’s error that no locator accepts the file. It gives no reason, because numba skipped the location after an error of its own, for an unwritable directory and for one too deep alike. The remedy therefore names the user’s cache directory as one that numba could not use, to be made writable or put at a shorter path. numba raises the
.ziplocator’s error instead for a frozen application whose path contains.zipbut has no part that ends in.zip. It does so if the list puts that locator before the user-wide one, or once the user-wide one skipped its location. The remedy then asks for the user-wide locator to be listed before the.zipone, or for the user-wide location to be made writable or put at a shorter path.An error that names the archive that a member’s code names is the stamp’s error. If the archive does not exist, because the member’s
.pycwas compiled to name a path where no archive exists now, the remedy asks for the source files to be shipped. Before numba 0.68, it also offers compiling the.pycmembers to name the archive’s present path. If the archive cannot be read, the remedy asks for it to be made readable.If the error names no file, the remedy names the locations that numba could have chosen, as for a source file on disk. If numba reaches a locator of IPython’s family or a class outside its families for a member, the remedy does not say that numba caches the member in the user’s cache directory. IPython’s
numba_cacheis among the locations whenever IPython gives its directory.- A list without the locator that a source needs
numba caches a
.zipthrough its.ziplocator alone, a cell through its IPython locator alone, and a frozen application through its user-wide locator alone. IfNUMBA_CACHE_LOCATOR_CLASSESomits the locator that the source needs, the remedy asks for that locator to be listed. It does so for numba’s error that no locator accepts the file, and for any error that a caller passes on under such a list, because numba chose no location. A.zipcounts only if no part of the path that ends in.zipis a directory: numba caches nothing under a directory so named.For a source file on disk, a list in which no locator accepts the file is named, with the locators for a file on disk to be listed. The user-provided locator accepts nothing while
NUMBA_CACHE_DIRis unset, so the remedy names it together with the variable. A.ziplocator listed before every locator for a file on disk accepts a path that contains.zip. If no part of that path ends in.zip, the locator finds no archive and raises, and the remedy asks for a locator for a file on disk to be listed before it. A.ziplocator listed after some of them raises the same error once those skipped the file, and numba tries no locator after it. The remedy then asks for one of their locations to be made writable, or for such a locator to be listed before the.zipone.- Another archive, or a
.pycalone A module in an
.egg,.whl,.pyzor other archive that numba does not cache has no cache location, and neither has a module present as.pycalone. The remedy for either asks for the package to be installed with its source files on disk. As the alternative, the remedy offers a.zipthat holds them, which numba caches in the user’s cache directory. If the module is already in an archive whose name does not end in.zip, lower-case, the remedy asks for that archive to be renamed to end in.zip. If a directory above that archive has a name that ends in.zip, numba treats that directory as the archive. The remedy then also asks for the archive to be moved to a path with no such directory above it. Each option names the locator that it needs, ifNUMBA_CACHE_LOCATOR_CLASSESomits that locator. A source that is not in a.zip, that is not a frozen application’s source, and that no locator accepts receives this remedy for any error.- A
.zipmember under numba 0.68 and later From numba 0.68, the stamp of a
.zipmember hashes the member’s source. numba reads the source from the archive by the name that the module’s code gives under the archive’s path, and zipfile raisesKeyErrorfor a name that the archive does not hold. For a module shipped as.pycalone, the remedy names the member and asks for the source files, on disk or in a.zip. A.pyccan be compiled to name another path in the archive (compileall -d) while the archive holds its source beside it. The remedy then asks for the.pycto be compiled to name its own path, or to be omitted from the archive.On Windows, numba 0.68.0 builds the name with the platform’s separator, so every member raises that error (numba issue 10889, fixed in 0.68.1). If the archive holds the member’s source under the name with forward slashes, which numba 0.68.1 reads, the remedy is the install on disk or a numba release that includes the fix. Otherwise it is the remedy for any other member.
numba treats the first part of the path that ends in
.zipas the archive, including a directory so named, and from 0.68 it opens that part to read the source. The remedy for theIsADirectoryErrorthat a directory gives asks for the archive to be put under a path with no such directory above it. If a file on disk lies under such a directory, the.ziplocator accepts it once the locators before it skipped the file. The remedy then asks for one of their locations to be made writable. If the.ziplocator is listed first, the remedy asks instead for a locator for a file on disk to be listed before it. Either remedy also offers putting the file under a path with no such directory.
- numbox.core.configurations.check_cache_location(py_file, longest_file_name=0)[source]
Raise the error that numba would raise if it could not cache a function defined in
py_file; otherwise return.py_filecan be a string, bytes or a path object; it is read as a string. The check repeats the steps that numba runs when it decorates a function withcache=Trueand when it first saves the function. It runs them for a probe whose code namespy_fileas its file:It sets up numba’s cache, which picks a location for the file. numba raises
RuntimeErrorif no locator accepts the file.It reads the source’s stamp, as numba does at decoration. For a
.zipmember, numba reads the archive’s modification time and size, and from numba 0.68 it also reads the member’s source from the archive. zipfile raisesKeyErrorif the archive holds no source under the member’s name. From numba 0.68, numba also reads a source on disk, to hash it.It runs numba’s test that the location can be written in. numba runs that test at decoration for every location except a
.zip’s, and at the first save for every location.It asks numba for the location again, as numba asks at every save, and raises
FileNotFoundErrorif the location does not exist.If
longest_file_nameis not 0, it creates and removes a file with a name of that many bytes in the location.
Nothing is compiled, and no file remains afterward. The directories that numba creates while it tries the locations are not removed. An IPython that can write neither its cache directory nor its own directory also creates a new temporary directory on every call, and the check causes three such calls.
An
OSErrorfrom the check names the location that numba picked, except in three cases:inspect’s error names no file. numba’s IPython locator reads the function’s source before it tries its location, and inspect raises if it finds no source. That happens for a cell that linecache does not hold, for a
.zipmember compiled to.pycalone or whose source is not UTF-8, and for a cell file on disk that cannot be read. An empty cell file raises too before Python 3.13; from 3.13, linecache holds an empty file as one blank line.The stamp’s error names the archive that a
.zipmember’s code names, if that archive has moved or cannot be read. From numba 0.68, it can also name the source, if numba cannot read it, or a directory named.zipabove a file on disk, which numba’s.ziplocator treats as the archive and opens.If IPython gives a new temporary directory on every call, numba creates
numba_cachein one directory and would save in another. TheFileNotFoundErrorthen namesnumba_cacheunder the last directory that IPython gave, which numba never created. numba’s first save would instead name a file innumba_cacheunder the directory that IPython gave numba when it set the cache up.
numba’s own test creates a temporary file, which on Linux is usually unnamed and elsewhere has a short name. The cache files that numba saves later have names of a hundred bytes or more. A location close to the path limit therefore passes numba’s test and fails at the first save. The file with a name of
longest_file_namebytes tests such a name in the location; numbox passesLONGEST_CACHE_FILE_NAME. If a name of that length is longer than the file system holding the location allows, no shorter path avoids the error. TheOSErrorthen carries the length as itslongest_file_nameattribute, andcache_remedy()asks for a file system that allows such a name.
- numbox.core.configurations.function_struct_size = 3
The number of fields in numba’s
FunctionModelstruct:c_addr,py_addrandjit_addr.
- numbox.core.configurations.get_jit_options()[source]
Return the njit options that
NUMBOX_JIT_OPTIONSsets, or{"cache": True}if the variable is unset.The variable holds a JSON object, whose keys numbox passes to
njitas keyword arguments, as in:export NUMBOX_JIT_OPTIONS='{"cache": false}'
Raises
ValueErrorif the value is not valid JSON, is not a JSON object, or holds a"cache"that is nottrueorfalse. The variable is read on every call; this module calls the function once, when it is first imported, to setjit_options.
- numbox.core.configurations.is_a_cache_error(error)[source]
Return whether
erroris one of numba’s errors for a cache that numba cannot set up.Those errors are:
any
OSError;numba’s
RuntimeErrorfor a file that no locator accepts, whose message containsno locator available;numba’s
ValueErrorNo zip file found in path, which numba’s.ziplocator raises for a path that contains.zipbut has no part that ends in.zip;from numba 0.68, zipfile’s
KeyErrorThere is no item named ..., which reaches the caller when the archive holds no source under a.zipmember’s name. numba reads that source for the member’s stamp. The name is missing for a module shipped as.pycalone, and on Windows under numba 0.68.0 for every member, because that release builds the name with the platform’s separator (numba issue 10889, fixed in 0.68.1);the
UnicodeEncodeErrorsurrogates not allowed, which numba raises for a source directory whose path is not UTF-8, when it hashes the path to name its directory underNUMBA_CACHE_DIRor the user’s cache directory.
Any other error, including a
UnicodeEncodeErrorfor another reason, is not a cache error.
- numbox.core.configurations.jit_options = {'cache': True}
The keyword arguments that numbox passes to numba’s
njitfor its own functions. They are the options thatNUMBOX_JIT_OPTIONSsets, or{"cache": True}if it is unset, read when this module is first imported, with"cache"set toFalseif numba cannot cache numbox. A package’s own functions compile under the same options with@njit(**jit_options).
- numbox.core.configurations.uncached_where_no_cache_can_be_written(options)[source]
Return
options, or a copy with"cache"set toFalseafter one warning if numba cannot cache numbox.numba sets a cached function’s cache up when it decorates the function, and raises
RuntimeErrorthere if it finds no location that it can write in. That happens in a read-only install whose user cache directory cannot be written either, and for a module imported from an.egg,.whlor.pyzarchive. Spark ships.eggfiles, as well as.zipand.pyfiles, to its executors through the--py-filesoption ofspark-submit. For a.zip, numba uses the user’s cache directory without testing at decoration that it can write there, and the first save raisesOSErrorinstead. Either error would end numbox’s import at its first decorated function, and no message would say what to change.numbox decorates every function that it caches with the same
jit_options, so this function runs the check once, on a source file in each directory of numbox that holds a module, and applies the result to all of numbox. If numba could not cache a function of one of those files, the function returns the options with"cache"set toFalse. OneRuntimeWarningthen gives numba’s error and the remedy thatcache_remedy()composes for it. The remedy ends withNUMBOX_JIT_OPTIONS='{"cache": false}', which disables caching and silences the warning. An error that is not a cache error, asis_a_cache_error()defines it, is raised unchanged.Options with
"cache"set toFalseare returned as they are, and the check does not run.NUMBOX_JIT_OPTIONScan give options without a"cache"key; such options leavenjitat its default, which is not to cache. The check still runs for them, becausecompile_kernel()caches its kernels under such options, and so do numbox’s SQLite callbacks for virtual tables and table-valued functions. If numba cannot cache numbox, the function returns these options with"cache"set toFalseexplicitly.The check is stricter than numba for a
.zipwhose cache directory still holds every entry but can no longer be written: numbox compiles without a cache there, where numba alone would load the entries. The test that the location can be written in is the rule that numba applies to every other location, and the one that its.ziplocator lacks, as https://github.com/numba/numba/issues/10888 reports.