From 8aa0809fbc9a91ec286ca02bd7f0b673e06905de Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 27 Oct 2019 02:28:35 +0100 Subject: [PATCH 1/5] on_rm_rf_error: ignore os.open (no warning) Ref: https://github.com/pytest-dev/pytest/pull/6044/files#r339321752 --- src/_pytest/pathlib.py | 11 ++++++----- testing/test_tmpdir.py | 7 +++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/_pytest/pathlib.py b/src/_pytest/pathlib.py index 543103fb5b0..bd76fac6b31 100644 --- a/src/_pytest/pathlib.py +++ b/src/_pytest/pathlib.py @@ -68,13 +68,14 @@ def on_rm_rf_error(func, path: str, exc, *, start_path: Path) -> bool: return False if func not in (os.rmdir, os.remove, os.unlink): - warnings.warn( - PytestWarning( - "(rm_rf) unknown function {} when removing {}:\n{}: {}".format( - path, func, exctype, excvalue + if func not in (os.open,): + warnings.warn( + PytestWarning( + "(rm_rf) unknown function {} when removing {}:\n{}: {}".format( + path, func, exctype, excvalue + ) ) ) - ) return False # Chmod + retry. diff --git a/testing/test_tmpdir.py b/testing/test_tmpdir.py index 0ebed22ac45..2433d614519 100644 --- a/testing/test_tmpdir.py +++ b/testing/test_tmpdir.py @@ -393,6 +393,13 @@ def test_on_rm_rf_error(self, tmp_path): on_rm_rf_error(None, str(fn), exc_info, start_path=tmp_path) assert fn.is_file() + # ignored function + with pytest.warns(None) as warninfo: + exc_info = (None, PermissionError(), None) + on_rm_rf_error(os.open, str(fn), exc_info, start_path=tmp_path) + assert fn.is_file() + assert not [x.message for x in warninfo] + exc_info = (None, PermissionError(), None) on_rm_rf_error(os.unlink, str(fn), exc_info, start_path=tmp_path) assert not fn.is_file() From 2e5cf1cc789908ab4856c84a99d7d1120f84e694 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 28 Oct 2019 12:34:40 +0100 Subject: [PATCH 2/5] Fix order of format args with warning --- src/_pytest/pathlib.py | 2 +- testing/test_tmpdir.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/_pytest/pathlib.py b/src/_pytest/pathlib.py index bd76fac6b31..8d25b21dd7d 100644 --- a/src/_pytest/pathlib.py +++ b/src/_pytest/pathlib.py @@ -72,7 +72,7 @@ def on_rm_rf_error(func, path: str, exc, *, start_path: Path) -> bool: warnings.warn( PytestWarning( "(rm_rf) unknown function {} when removing {}:\n{}: {}".format( - path, func, exctype, excvalue + func, path, exctype, excvalue ) ) ) diff --git a/testing/test_tmpdir.py b/testing/test_tmpdir.py index 2433d614519..29b6db947bc 100644 --- a/testing/test_tmpdir.py +++ b/testing/test_tmpdir.py @@ -388,7 +388,10 @@ def test_on_rm_rf_error(self, tmp_path): assert not on_rm_rf_error(None, str(fn), exc_info, start_path=tmp_path) # unknown function - with pytest.warns(pytest.PytestWarning): + with pytest.warns( + pytest.PytestWarning, + match=r"^\(rm_rf\) unknown function None when removing .*foo.txt:\nNone: ", + ): exc_info = (None, PermissionError(), None) on_rm_rf_error(None, str(fn), exc_info, start_path=tmp_path) assert fn.is_file() From 2adc84ed6c67116ceb8f1fb22c20d943f0bd0c38 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 7 Nov 2019 12:55:01 +0100 Subject: [PATCH 3/5] changelog --- changelog/6074.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/6074.bugfix.rst diff --git a/changelog/6074.bugfix.rst b/changelog/6074.bugfix.rst new file mode 100644 index 00000000000..624cf5d1c8f --- /dev/null +++ b/changelog/6074.bugfix.rst @@ -0,0 +1 @@ +pytester: fix order of arguments in ``rm_rf`` warning when cleaning up temporary directories, and do not emit warnings for errors with ``os.open``. From 45c4a8fb3d33698704ac17a376de36ada3cabecf Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 7 Nov 2019 14:41:26 +0100 Subject: [PATCH 4/5] 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 - # " Date: Thu, 7 Nov 2019 22:13:03 +0100 Subject: [PATCH 5/5] Update changelog/6148.improvement.rst Co-Authored-By: Bruno Oliveira --- changelog/6148.improvement.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/6148.improvement.rst b/changelog/6148.improvement.rst index c60d2c92142..3d77ab528f9 100644 --- a/changelog/6148.improvement.rst +++ b/changelog/6148.improvement.rst @@ -1 +1 @@ -``python-atomicwrites`` is only used on Windows, fixing a performance regression with assertion rewriting on Unix. +``atomicwrites`` is now only used on Windows, fixing a performance regression with assertion rewriting on Unix.