insure moving pyc files around is atomic

This commit is contained in:
Benjamin Peterson 2011-07-05 12:02:53 -05:00
parent d105e75d87
commit 4e8b9fab3c
1 changed files with 8 additions and 2 deletions

View File

@ -87,7 +87,7 @@ class AssertionRewritingHook(object):
cache = py.path.local(fn_pypath.dirname).join("__pycache__", cache_fn) cache = py.path.local(fn_pypath.dirname).join("__pycache__", cache_fn)
if _use_cached_pyc(fn_pypath, cache): if _use_cached_pyc(fn_pypath, cache):
state.trace("found cached rewritten pyc for %r" % (fn,)) state.trace("found cached rewritten pyc for %r" % (fn,))
cache.copy(pyc) _atomic_copy(cache, pyc)
else: else:
state.trace("rewriting %r" % (fn,)) state.trace("rewriting %r" % (fn,))
_make_rewritten_pyc(state, fn_pypath, pyc) _make_rewritten_pyc(state, fn_pypath, pyc)
@ -163,10 +163,16 @@ def _use_cached_pyc(source, cache):
def _cache_pyc(state, pyc, cache): def _cache_pyc(state, pyc, cache):
try: try:
cache.dirpath().ensure(dir=True) cache.dirpath().ensure(dir=True)
pyc.copy(cache) _atomic_copy(pyc, cache)
except EnvironmentError: except EnvironmentError:
state.trace("failed to cache %r as %r" % (pyc, cache)) state.trace("failed to cache %r as %r" % (pyc, cache))
def _atomic_copy(orig, to):
"""An atomic copy (at least on POSIX platforms)"""
temp = py.path.local(orig.strpath + str(os.getpid()))
orig.copy(temp)
temp.rename(to)
def rewrite_asserts(mod): def rewrite_asserts(mod):
"""Rewrite the assert statements in mod.""" """Rewrite the assert statements in mod."""