From 45c4a8fb3d33698704ac17a376de36ada3cabecf Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 7 Nov 2019 14:41:26 +0100 Subject: [PATCH] Use atomicrewrites only on Windows Fixes https://github.com/pytest-dev/pytest/issues/6147 --- changelog/6148.improvement.rst | 1 + setup.py | 2 +- src/_pytest/assertion/rewrite.py | 69 +++++++++++++++++++++++--------- testing/test_assertrewrite.py | 34 ++++++++++------ 4 files changed, 74 insertions(+), 32 deletions(-) create mode 100644 changelog/6148.improvement.rst diff --git a/changelog/6148.improvement.rst b/changelog/6148.improvement.rst new file mode 100644 index 00000000000..c60d2c92142 --- /dev/null +++ b/changelog/6148.improvement.rst @@ -0,0 +1 @@ +``python-atomicwrites`` is only used on Windows, fixing a performance regression with assertion rewriting on Unix. diff --git a/setup.py b/setup.py index dcf63f6fd23..d7f3d7dbf61 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ "packaging", "attrs>=17.4.0", # should match oldattrs tox env. "more-itertools>=4.0.0", - "atomicwrites>=1.0", + 'atomicwrites>=1.0;sys_platform=="win32"', 'pathlib2>=2.2.0;python_version<"3.6"', 'colorama;sys_platform=="win32"', "pluggy>=0.12,<1.0", diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index 9c9d6135b1b..b8492993643 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -20,8 +20,6 @@ from typing import Set from typing import Tuple -import atomicwrites - from _pytest._io.saferepr import saferepr from _pytest._version import version from _pytest.assertion import util @@ -255,26 +253,59 @@ def get_data(self, pathname): return f.read() -def _write_pyc(state, co, source_stat, pyc): +def _write_pyc_fp(fp, source_stat, co): # Technically, we don't have to have the same pyc format as # (C)Python, since these "pycs" should never be seen by builtin # import. However, there's little reason deviate. - try: - with atomicwrites.atomic_write(fspath(pyc), mode="wb", overwrite=True) as fp: - fp.write(importlib.util.MAGIC_NUMBER) - # as of now, bytecode header expects 32-bit numbers for size and mtime (#4903) - mtime = int(source_stat.st_mtime) & 0xFFFFFFFF - size = source_stat.st_size & 0xFFFFFFFF - # "