From f0bb568b74c4a5fe2b897286fe05c10a9b929075 Mon Sep 17 00:00:00 2001 From: Sergei Trofimovich Date: Fri, 1 Oct 2021 18:44:47 +0100 Subject: [PATCH] pytest: new hook to delete pytest bytecode In https://github.com/NixOS/nixpkgs/issues/139292 we found out that pytest leaves pytest-generated bytecode in install directory (as tests are ran after install). That bytecode is: - non-deterministic (was copied from cpython before cpython got support for deterministic bytecode) - unneeded after test run The change cleans bytecode up and provides a hook variable to avoid it if needed. Tested on `python39Packages.pytest-xdist` and `python27Packages.flaky` as: $ nix build -f. python39Packages.pytest-xdist $ nix build -f. python39Packages.pytest-xdist --rebuild $ nix build -f. python27Packages.flaky $ nix build -f. python27Packages.flaky --rebuild Closes: https://github.com/NixOS/nixpkgs/issues/139292 --- pkgs/development/python-modules/pytest/4.nix | 13 +++++++++++++ pkgs/development/python-modules/pytest/5.nix | 13 +++++++++++++ pkgs/development/python-modules/pytest/default.nix | 13 +++++++++++++ 3 files changed, 39 insertions(+) diff --git a/pkgs/development/python-modules/pytest/4.nix b/pkgs/development/python-modules/pytest/4.nix index fd6d3507afb8..0a0ae571ba01 100644 --- a/pkgs/development/python-modules/pytest/4.nix +++ b/pkgs/development/python-modules/pytest/4.nix @@ -43,6 +43,19 @@ buildPythonPackage rec { } preDistPhases+=" pytestcachePhase" + + # pytest generates it's own bytecode files to improve assertion messages. + # These files similar to cpython's bytecode files but are never laoded + # by python interpreter directly. We remove them for a few reasons: + # - files are non-deterministic: https://github.com/NixOS/nixpkgs/issues/139292 + # (file headers are generatedt by pytest directly and contain timestamps) + # - files are not needed after tests are finished + pytestRemoveBytecodePhase () { + # suffix is defined at: + # https://github.com/pytest-dev/pytest/blob/4.6.11/src/_pytest/assertion/rewrite.py#L32-L47 + find $out -name "*-PYTEST.py[co]" -delete + } + preDistPhases+=" pytestRemoveBytecodePhase" ''; meta = with lib; { diff --git a/pkgs/development/python-modules/pytest/5.nix b/pkgs/development/python-modules/pytest/5.nix index b38c1a759fe3..8df7c02b1774 100644 --- a/pkgs/development/python-modules/pytest/5.nix +++ b/pkgs/development/python-modules/pytest/5.nix @@ -70,6 +70,19 @@ buildPythonPackage rec { find $out -name .pytest_cache -type d -exec rm -rf {} + } preDistPhases+=" pytestcachePhase" + + # pytest generates it's own bytecode files to improve assertion messages. + # These files similar to cpython's bytecode files but are never laoded + # by python interpreter directly. We remove them for a few reasons: + # - files are non-deterministic: https://github.com/NixOS/nixpkgs/issues/139292 + # (file headers are generatedt by pytest directly and contain timestamps) + # - files are not needed after tests are finished + pytestRemoveBytecodePhase () { + # suffix is defined at: + # https://github.com/pytest-dev/pytest/blob/5.4.3/src/_pytest/assertion/rewrite.py#L42-L45 + find $out -name "*-pytest-*.py[co]" -delete + } + preDistPhases+=" pytestRemoveBytecodePhase" ''; pythonImportsCheck = [ diff --git a/pkgs/development/python-modules/pytest/default.nix b/pkgs/development/python-modules/pytest/default.nix index f2438622f6a6..ac036936b991 100644 --- a/pkgs/development/python-modules/pytest/default.nix +++ b/pkgs/development/python-modules/pytest/default.nix @@ -82,6 +82,19 @@ buildPythonPackage rec { find $out -name .pytest_cache -type d -exec rm -rf {} + } preDistPhases+=" pytestcachePhase" + + # pytest generates it's own bytecode files to improve assertion messages. + # These files similar to cpython's bytecode files but are never laoded + # by python interpreter directly. We remove them for a few reasons: + # - files are non-deterministic: https://github.com/NixOS/nixpkgs/issues/139292 + # (file headers are generatedt by pytest directly and contain timestamps) + # - files are not needed after tests are finished + pytestRemoveBytecodePhase () { + # suffix is defined at: + # https://github.com/pytest-dev/pytest/blob/6.2.5/src/_pytest/assertion/rewrite.py#L51-L53 + find $out -name "*-pytest-*.py[co]" -delete + } + preDistPhases+=" pytestRemoveBytecodePhase" ''; pythonImportsCheck = [