Skip to content

Commit b5a94d8

Browse files
committed
Write rewritten code using file.write(marshal.dumps())
This works around the fact that some libraries might monkey patch the file object, so the previous approach of marshal.dump(co, file) breaks because file is not a built-in file object anymore. Fix pytest-dev#3503
1 parent 65bc43d commit b5a94d8

File tree

2 files changed

+2
-4
lines changed

2 files changed

+2
-4
lines changed

_pytest/assertion/rewrite.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,7 @@ def _write_pyc(state, co, source_stat, pyc):
265265
mtime = int(source_stat.mtime)
266266
size = source_stat.size & 0xFFFFFFFF
267267
fp.write(struct.pack("<ll", mtime, size))
268-
if six.PY2:
269-
marshal.dump(co, fp.file)
270-
else:
271-
marshal.dump(co, fp)
268+
fp.write(marshal.dumps(co))
272269
except EnvironmentError as e:
273270
state.trace("error writing pyc file at %s: errno=%s" % (pyc, e.errno))
274271
# we ignore any failure to write the cache file

changelog/3503.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix assertion rewriter compatibility with libraries that monkey patch ``file`` objects.

0 commit comments

Comments
 (0)