Skip to content

test_compileall: HardlinkDedupTests test_import fails with PYTHONDONTWRITEBYTECODE or PYTHONOPTIMIZE set #162846

Backlog

Description

@Daranton

Bug report

Bug description:

HardlinkDedupTestsNoSourceEpoch.test_import and
HardlinkDedupTestsWithSourceEpoch.test_import fail when the test suite is
run with PYTHONDONTWRITEBYTECODE set, which is common in container images:

$ PYTHONDONTWRITEBYTECODE=1 ./python -m test test_compileall
...
FAIL: test_import (test.test_compileall.HardlinkDedupTestsNoSourceEpoch.test_import)
----------------------------------------------------------------------
Traceback (most recent call last):
  ...
  File "Lib/test/test_compileall.py", line 1169, in test_import
    self.assertFalse(is_hardlink(pycs[1], pycs[2]))
AssertionError: True is not false

The test changes the source and then imports it in a child process with -O,
expecting the child to rewrite the opt-1 pyc file and so break its hardlink to
the opt-2 file. The child is started with __isolated=False (it needs
PYTHONPATH), so it inherits PYTHONDONTWRITEBYTECODE, never writes the pyc,
and the hardlink stays.

PYTHONOPTIMIZE=2 breaks the test the same way: the child then runs at
optimization level 2 and rewrites the opt-2 file instead of the opt-1 file.

The test was added in gh-84675 (bpo-40495), after the suite was made to pass
with -B/PYTHONDONTWRITEBYTECODE in gh-55629 (bpo-11420). gh-151626 fixed
the same kind of problem for PYTHONPYCACHEPREFIX.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    tests
    type-bug
    on Nov 30, 2026
  2. added 2 commits that reference this issue on Nov 30, 2026
  3. changed the title [-]test_compileall: HardlinkDedupTests test_import fails when PYTHONDONTWRITEBYTECODE is set[/-] [+]test_compileall: HardlinkDedupTests test_import fails with PYTHONDONTWRITEBYTECODE or PYTHONOPTIMIZE set[/+] on Nov 30, 2026
  4. added 2 commits that reference this issue on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    teststype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions