diff --git a/CHANGELOG b/CHANGELOG index 888413914..6d22801c9 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -3,6 +3,10 @@ NEXT - No longer show line numbers in the --verbose output, the output is now purely the nodeid. The line number is still shown in failure reports. + Thanks Floris Bruynooghe. + +- fix issue437 where assertion rewriting could cause pytest-xdist slaves + to collect different tests. Thanks Bruno Oliveira. - fix issue547 capsys/capfd also work when output capturing ("-s") is disabled. diff --git a/_pytest/assertion/rewrite.py b/_pytest/assertion/rewrite.py index 95bf4117d..523d2b2dc 100644 --- a/_pytest/assertion/rewrite.py +++ b/_pytest/assertion/rewrite.py @@ -131,7 +131,7 @@ class AssertionRewritingHook(object): pyc = os.path.join(cache_dir, cache_name) # Notice that even if we're in a read-only directory, I'm going # to check for a cached pyc. This may not be optimal... - co = _read_pyc(fn_pypath, pyc) + co = _read_pyc(fn_pypath, pyc, state.trace) if co is None: state.trace("rewriting %r" % (fn,)) co = _rewrite_test(state, fn_pypath) @@ -289,7 +289,7 @@ def _make_rewritten_pyc(state, fn, pyc, co): if _write_pyc(state, co, fn, proc_pyc): os.rename(proc_pyc, pyc) -def _read_pyc(source, pyc): +def _read_pyc(source, pyc, trace=lambda x: None): """Possibly read a pytest pyc containing rewritten code. Return rewritten code if successful or None if not. @@ -298,23 +298,27 @@ def _read_pyc(source, pyc): fp = open(pyc, "rb") except IOError: return None - try: + with fp: try: mtime = int(source.mtime()) data = fp.read(8) - except EnvironmentError: + except EnvironmentError as e: + trace('_read_pyc(%s): EnvironmentError %s' % (source, e)) return None # Check for invalid or out of date pyc file. if (len(data) != 8 or data[:4] != imp.get_magic() or struct.unpack(" strip_bytes + pyc.write(contents[:strip_bytes], mode='wb') + + assert _read_pyc(source, str(pyc)) is None # no error