Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable caching functions arising from exec of strings (continued) #9553

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion numba/core/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,47 @@ def from_function(cls, py_func, py_file):
return self


class _StringSrcCacheLocator(_UserProvidedCacheLocator):

def __init__(self, py_func, py_file):
self._identifier = self._hash(py_func)
super().__init__(py_func, py_file)
# FIXME: this is a bit of a hack, but we need to ensure the cache

def get_source_stamp(self):
return self._identifier

def get_disambiguator(self):
# have to be quite fussy about this as there's very little aliasing
# protection with strings (no module or source to check).
return self._identifier

@classmethod
def from_function(cls, py_func, py_file):
if not py_file == "<string>":
return
fname = '<string>-' + str(cls._hash(py_func))
self = cls(py_func, fname)
try:
self.ensure_cache_path()
except OSError:
# Cannot ensure the cache directory exists or is writable
return
return self

@classmethod
def _hash(cls, py_func):
py_code = py_func.__code__
# skip builtin attrs and methods
data = "".join(
repr(getattr(py_code, attr))
for attr in dir(py_code)
if not attr.startswith('__') and \
not callable(getattr(py_code, attr))
)
return hashlib.sha256(data.encode()).hexdigest()


class CacheImpl(metaclass=ABCMeta):
"""
Provides the core machinery for caching.
Expand All @@ -318,7 +359,9 @@ class CacheImpl(metaclass=ABCMeta):
_locator_classes = [_UserProvidedCacheLocator,
_InTreeCacheLocator,
_UserWideCacheLocator,
_IPythonCacheLocator]
_IPythonCacheLocator,
_StringSrcCacheLocator,
]

def __init__(self, py_func):
self._lineno = py_func.__code__.co_firstlineno
Expand Down
Loading