Skip to content

gh-152659: Preserve parent package attributes when restoring fresh imports - #152675

Open
zainnadeem786 wants to merge 1 commit into
python:mainfrom
zainnadeem786:fix/import-fresh-module-submodule-restore
Open

zainnadeem786 wants to merge 1 commit into
python:mainfrom
zainnadeem786:fix/import-fresh-module-submodule-restore

Conversation

@zainnadeem786

Copy link
Copy Markdown
Contributor

Summary

Fix test.support.import_helper.import_fresh_module() so that it restores parent package attributes after importing fresh dotted modules.

This resolves the order-dependent failure reported in gh-152659 where test_profiling could fail after test_importlib.test_util.

Root Cause

import_fresh_module() restores sys.modules when exiting, but it did not restore the corresponding parent package attributes.

For example, after importing importlib.util as a fresh module, sys.modules["importlib.util"] was restored, but importlib.util could still reference the temporary module object.

This left the import state inconsistent:

importlib.util is not sys.modules["importlib.util"]

As a result, later tests patching importlib.util.find_spec could patch a different module object from the one actually used by the code under test, producing order-dependent failures.

Changes

  • Save parent package attributes before removing modules.
  • Restore parent package attributes when leaving import_fresh_module().
  • Correctly handle attributes that did not previously exist.
  • Add regression tests covering both restoration and attribute removal.

Tests

Validated with:

PCbuild\amd64\python_d.exe -m unittest -v ^
    test.test_support.TestSupport.test_import_fresh_module ^
    test.test_support.TestSupport.test_import_fresh_module_restores_parent_attr ^
    test.test_support.TestSupport.test_import_fresh_module_removes_added_parent_attr

PCbuild\amd64\python_d.exe -m unittest -v ^
    test.test_importlib.test_util ^
    test.test_profiling.test_sampling_profiler.test_cli.TestSampleProfilerCLI

PCbuild\amd64\python.exe -m unittest -v ^
    test.test_importlib.test_util ^
    test.test_profiling.test_sampling_profiler.test_cli.TestSampleProfilerCLI

PCbuild\amd64\python_d.exe -m test ^
    test_support ^
    test_importlib ^
    test_profiling

git diff --check

All validation passed successfully.

Closes gh-152659

Comment thread Lib/test/support/import_helper.py
Comment thread Lib/test/support/import_helper.py Outdated
Comment thread Lib/test/support/import_helper.py
Comment thread Lib/test/test_support.py Outdated
Comment thread Lib/test/test_support.py
Comment thread Lib/test/test_support.py Outdated
Comment thread Lib/test/test_support.py
Comment thread Lib/test/test_support.py Outdated
Comment thread Lib/test/test_support.py Outdated
@bedevere-app

bedevere-app Bot commented Sep 25, 2026

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@zainnadeem786
zainnadeem786 force-pushed the fix/import-fresh-module-submodule-restore branch from 3d20a01 to df8f089 Compare September 26, 2026 07:38
@zainnadeem786

Copy link
Copy Markdown
Contributor Author

Hi @brettcannon I have made the requested changes; please review again.

@bedevere-app

bedevere-app Bot commented Sep 26, 2026

Copy link
Copy Markdown

Thanks for making the requested changes!

@brettcannon: please review the changes made to this pull request.

@bedevere-app
bedevere-app Bot requested a review from brettcannon September 26, 2026 07:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Order-dependent failure: test_profiling CLI test fails after test_importlib.test_util

2 participants