summaryrefslogtreecommitdiff
path: root/numpy/distutils/ccompiler_opt.py
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/distutils/ccompiler_opt.py')
-rw-r--r--numpy/distutils/ccompiler_opt.py41
1 files changed, 25 insertions, 16 deletions
diff --git a/numpy/distutils/ccompiler_opt.py b/numpy/distutils/ccompiler_opt.py
index aea9835c7..316d3a338 100644
--- a/numpy/distutils/ccompiler_opt.py
+++ b/numpy/distutils/ccompiler_opt.py
@@ -747,12 +747,14 @@ class _Cache:
self.cache_me = {}
self.cache_private = set()
self.cache_infile = False
+ self._cache_path = None
if self.conf_nocache:
self.dist_log("cache is disabled by `Config`")
return
- chash = self.cache_hash(*factors, *self.conf_cache_factors)
+ self._cache_hash = self.cache_hash(*factors, *self.conf_cache_factors)
+ self._cache_path = cache_path
if cache_path:
if os.path.exists(cache_path):
self.dist_log("load cache from file ->", cache_path)
@@ -765,7 +767,7 @@ class _Cache:
elif not hasattr(cache_mod, "hash") or \
not hasattr(cache_mod, "data"):
self.dist_log("invalid cache file", stderr=True)
- elif chash == cache_mod.hash:
+ elif self._cache_hash == cache_mod.hash:
self.dist_log("hit the file cache")
for attr, val in cache_mod.data.items():
setattr(self, attr, val)
@@ -773,10 +775,8 @@ class _Cache:
else:
self.dist_log("miss the file cache")
- atexit.register(self._cache_write, cache_path, chash)
-
if not self.cache_infile:
- other_cache = _share_cache.get(chash)
+ other_cache = _share_cache.get(self._cache_hash)
if other_cache:
self.dist_log("hit the memory cache")
for attr, val in other_cache.__dict__.items():
@@ -785,32 +785,41 @@ class _Cache:
continue
setattr(self, attr, val)
- _share_cache[chash] = self
+ _share_cache[self._cache_hash] = self
+ atexit.register(self.cache_flush)
def __del__(self):
- # TODO: remove the cache form share on del
- pass
+ for h, o in _share_cache.items():
+ if o == self:
+ _share_cache.pop(h)
+ break
- def _cache_write(self, cache_path, cache_hash):
+ def cache_flush(self):
+ """
+ Force update the cache.
+ """
+ if not self._cache_path:
+ return
# TODO: don't write if the cache doesn't change
- self.dist_log("write cache to path ->", cache_path)
- for attr in list(self.__dict__.keys()):
+ self.dist_log("write cache to path ->", self._cache_path)
+ cdict = self.__dict__.copy()
+ for attr in self.__dict__.keys():
if re.match(self._cache_ignore, attr):
- self.__dict__.pop(attr)
+ cdict.pop(attr)
- d = os.path.dirname(cache_path)
+ d = os.path.dirname(self._cache_path)
if not os.path.exists(d):
os.makedirs(d)
- repr_dict = pprint.pformat(self.__dict__, compact=True)
- with open(cache_path, "w") as f:
+ repr_dict = pprint.pformat(cdict, compact=True)
+ with open(self._cache_path, "w") as f:
f.write(textwrap.dedent("""\
# AUTOGENERATED DON'T EDIT
# Please make changes to the code generator \
(distutils/ccompiler_opt.py)
hash = {}
data = \\
- """).format(cache_hash))
+ """).format(self._cache_hash))
f.write(repr_dict)
def cache_hash(self, *factors):