invalidate code cache whenever the xonshe version changes

This commit is contained in:
Anthony Scopatz 2017-02-26 01:07:16 -05:00
parent a4de707aff
commit 3236f3507f
2 changed files with 26 additions and 0 deletions

14
news/ic.rst Normal file
View file

@ -0,0 +1,14 @@
**Added:** None
**Changed:**
* Xonsh's script and code caches will are now invalidated whenever the
xonsh version changes for a given Python version.
**Deprecated:** None
**Removed:** None
**Fixed:** None
**Security:** None

View file

@ -1,9 +1,11 @@
"""Tools for caching xonsh code."""
import os
import sys
import hashlib
import marshal
import builtins
from xonsh import __version__ as XONSH_VERSION
from xonsh.lazyasd import lazyobject
@ -89,9 +91,15 @@ def update_cache(ccode, cache_file_name):
if cache_file_name is not None:
_make_if_not_exists(os.path.dirname(cache_file_name))
with open(cache_file_name, 'wb') as cfile:
cfile.write(XONSH_VERSION.encode() + b'\n')
marshal.dump(ccode, cfile)
def _check_cache_xonsh_version(cfile):
ver = cfile.readline(1024).strip() # version should be < 1 kb
return ver == XONSH_VERSION.encode()
def compile_code(filename, code, execer, glb, loc, mode):
"""
Wrapper for ``execer.compile`` to compile the given code
@ -123,6 +131,8 @@ def script_cache_check(filename, cachefname):
if os.path.isfile(cachefname):
if os.stat(cachefname).st_mtime >= os.stat(filename).st_mtime:
with open(cachefname, 'rb') as cfile:
if not _check_cache_xonsh_version(cfile):
return False, None
ccode = marshal.load(cfile)
run_cached = True
return run_cached, ccode
@ -169,6 +179,8 @@ def code_cache_check(cachefname):
run_cached = False
if os.path.isfile(cachefname):
with open(cachefname, 'rb') as cfile:
if not _check_cache_xonsh_version(cfile):
return False, None
ccode = marshal.load(cfile)
run_cached = True
return run_cached, ccode