refactor writing the fake pyc into its own function
This commit is contained in:
parent
76cede83c0
commit
7fc2f8786f
|
@ -43,6 +43,23 @@ def pytest_configure(config):
|
||||||
else:
|
else:
|
||||||
rewrite_asserts = None
|
rewrite_asserts = None
|
||||||
|
|
||||||
|
def _write_pyc(co, source_path):
|
||||||
|
if hasattr(imp, "cache_from_source"):
|
||||||
|
# Handle PEP 3147 pycs.
|
||||||
|
pyc = py.path(imp.cache_from_source(source_math))
|
||||||
|
pyc.dirname.ensure(dir=True)
|
||||||
|
else:
|
||||||
|
pyc = source_path + "c"
|
||||||
|
mtime = int(source_path.mtime())
|
||||||
|
fp = pyc.open("wb")
|
||||||
|
try:
|
||||||
|
fp.write(imp.get_magic())
|
||||||
|
fp.write(struct.pack("<l", mtime))
|
||||||
|
marshal.dump(co, fp)
|
||||||
|
finally:
|
||||||
|
fp.close()
|
||||||
|
return pyc
|
||||||
|
|
||||||
def pytest_pycollect_before_module_import(mod):
|
def pytest_pycollect_before_module_import(mod):
|
||||||
if rewrite_asserts is None:
|
if rewrite_asserts is None:
|
||||||
return
|
return
|
||||||
|
@ -61,21 +78,7 @@ def pytest_pycollect_before_module_import(mod):
|
||||||
# It's possible that this error is from some bug in the assertion
|
# It's possible that this error is from some bug in the assertion
|
||||||
# rewriting, but I don't know of a fast way to tell.
|
# rewriting, but I don't know of a fast way to tell.
|
||||||
return
|
return
|
||||||
if hasattr(imp, "cache_from_source"):
|
mod._pyc = _write_pyc(co, mod.fspath)
|
||||||
# Handle PEP 3147 pycs.
|
|
||||||
pyc = py.path(imp.cache_from_source(mod.fspath))
|
|
||||||
pyc.dirname.ensure(dir=True)
|
|
||||||
else:
|
|
||||||
pyc = mod.fspath + "c"
|
|
||||||
mod._pyc = pyc
|
|
||||||
mtime = int(mod.fspath.mtime())
|
|
||||||
fp = pyc.open("wb")
|
|
||||||
try:
|
|
||||||
fp.write(imp.get_magic())
|
|
||||||
fp.write(struct.pack("<l", mtime))
|
|
||||||
marshal.dump(co, fp)
|
|
||||||
finally:
|
|
||||||
fp.close()
|
|
||||||
|
|
||||||
def pytest_pycollect_after_module_import(mod):
|
def pytest_pycollect_after_module_import(mod):
|
||||||
if rewrite_asserts is None or not hasattr(mod, "_pyc"):
|
if rewrite_asserts is None or not hasattr(mod, "_pyc"):
|
||||||
|
|
Loading…
Reference in New Issue