From bd6a0cc5c9ce540400b88f474022b938d91fdd66 Mon Sep 17 00:00:00 2001 From: Daranton Date: Mon, 30 Nov 2026 01:08:59 +0000 Subject: [PATCH 1/2] gh-162846: Fix test_compileall when PYTHONDONTWRITEBYTECODE is set HardlinkDedupTestsBase.test_import expects a child interpreter to rewrite the opt-1 pyc file. The child inherits the environment, so with PYTHONDONTWRITEBYTECODE set it never writes the file, and with PYTHONOPTIMIZE=2 it writes the opt-2 file instead. Unset both variables for the child. --- Lib/test/test_compileall.py | 14 ++++++++++---- .../2026-11-30-00-59-15.gh-issue-162846.rCETAA.rst | 2 ++ 2 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Tests/2026-11-30-00-59-15.gh-issue-162846.rCETAA.rst diff --git a/Lib/test/test_compileall.py b/Lib/test/test_compileall.py index 9a6ca2be1624c6c..cfc38944be4b59d 100644 --- a/Lib/test/test_compileall.py +++ b/Lib/test/test_compileall.py @@ -1158,10 +1158,16 @@ def test_import(self): # Change of the module content script = self.make_script("print(0)", name="module") - # Import the module in Python with -O (optimization level 1) - script_helper.assert_python_ok( - "-O", "-c", "import module", __isolated=False, PYTHONPATH=self.path - ) + # Import the module in Python with -O (optimization level 1). + # The child must write the opt-1 pyc file, so ignore any ambient + # PYTHONDONTWRITEBYTECODE or PYTHONOPTIMIZE. + with os_helper.EnvironmentVarGuard() as env: + env.unset('PYTHONDONTWRITEBYTECODE') + env.unset('PYTHONOPTIMIZE') + script_helper.assert_python_ok( + "-O", "-c", "import module", + __isolated=False, PYTHONPATH=self.path + ) # Only opt-1.pyc is changed self.assertEqual(inode, os.stat(pycs[0]).st_ino) diff --git a/Misc/NEWS.d/next/Tests/2026-11-30-00-59-15.gh-issue-162846.rCETAA.rst b/Misc/NEWS.d/next/Tests/2026-11-30-00-59-15.gh-issue-162846.rCETAA.rst new file mode 100644 index 000000000000000..db33a8244222957 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2026-11-30-00-59-15.gh-issue-162846.rCETAA.rst @@ -0,0 +1,2 @@ +Fix ``test_compileall`` failing when the test suite is run with +:envvar:`PYTHONDONTWRITEBYTECODE` or :envvar:`PYTHONOPTIMIZE` set. From 0daa0b0011bb0cb868d11ad7b92aa2840b29afcd Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Mon, 30 Nov 2026 20:02:44 +0000 Subject: [PATCH 2/2] Delete Misc/NEWS.d/next/Tests/2026-11-30-00-59-15.gh-issue-162846.rCETAA.rst --- .../next/Tests/2026-11-30-00-59-15.gh-issue-162846.rCETAA.rst | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 Misc/NEWS.d/next/Tests/2026-11-30-00-59-15.gh-issue-162846.rCETAA.rst diff --git a/Misc/NEWS.d/next/Tests/2026-11-30-00-59-15.gh-issue-162846.rCETAA.rst b/Misc/NEWS.d/next/Tests/2026-11-30-00-59-15.gh-issue-162846.rCETAA.rst deleted file mode 100644 index db33a8244222957..000000000000000 --- a/Misc/NEWS.d/next/Tests/2026-11-30-00-59-15.gh-issue-162846.rCETAA.rst +++ /dev/null @@ -1,2 +0,0 @@ -Fix ``test_compileall`` failing when the test suite is run with -:envvar:`PYTHONDONTWRITEBYTECODE` or :envvar:`PYTHONOPTIMIZE` set.