Skip to content

Commit a192255

Browse files
committed
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.
1 parent 0423fc6 commit a192255

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

‎Lib/test/test_compileall.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,10 +1158,16 @@ def test_import(self):
11581158
# Change of the module content
11591159
script = self.make_script("print(0)", name="module")
11601160

1161-
# Import the module in Python with -O (optimization level 1)
1162-
script_helper.assert_python_ok(
1163-
"-O", "-c", "import module", __isolated=False, PYTHONPATH=self.path
1164-
)
1161+
# Import the module in Python with -O (optimization level 1).
1162+
# The child must write the opt-1 pyc file, so ignore any ambient
1163+
# PYTHONDONTWRITEBYTECODE or PYTHONOPTIMIZE.
1164+
with os_helper.EnvironmentVarGuard() as env:
1165+
env.unset('PYTHONDONTWRITEBYTECODE')
1166+
env.unset('PYTHONOPTIMIZE')
1167+
script_helper.assert_python_ok(
1168+
"-O", "-c", "import module",
1169+
__isolated=False, PYTHONPATH=self.path
1170+
)
11651171

11661172
# Only opt-1.pyc is changed
11671173
self.assertEqual(inode, os.stat(pycs[0]).st_ino)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix ``test_compileall`` failing when the test suite is run with
2+
:envvar:`PYTHONDONTWRITEBYTECODE` or :envvar:`PYTHONOPTIMIZE` set.

0 commit comments

Comments
 (0)