gh-150167: Force eager import of _bz2 in bz2.py to fix supported format detection - #154771
Open
DavidCEllis wants to merge 7 commits into
Open
gh-150167: Force eager import of _bz2 in bz2.py to fix supported format detection#154771DavidCEllis wants to merge 7 commits into
_bz2 in bz2.py to fix supported format detection#154771DavidCEllis wants to merge 7 commits into
Conversation
… be removed without forcibly re-importing them.
… undocumented `usefrozen` argument and correct the signature as `deprecated` and `usefrozen` are keyword only arguments.
DavidCEllis
requested review from
FFY00,
brettcannon,
ericsnowcurrently,
giampaolo,
ncoghlan and
warsaw
as code owners
July 27, 2026 12:50
Documentation build overview
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds
import _bz2; _bz2tobz2.pywhich has the effect of making the import eager even under the 'all' lazy imports mode.Fixes the issue where
bz2would always be reported as supported byshutilunderlazy_imports=alleven if it was unavailable. This should also fix this same logic where it's reused in other places (such astest_support).I think it may make sense to add a
SUPPORTED_MODULESattribute tocompressionthat lists which modules are available by checking the internal modules directly, but that can be done on top of this.The 'import' change that added all of the import codeowners as reviewers:
In order to test that a fresh
shutilwould correctly identify 'bz2' as unavailable if_bz2is blocked I needed to add an argument totest.support.import_helpers.import_fresh_moduleto allowbz2to be cleared without requiring it successfully re-import. This is the new optionalclearedargument.This new argument is documented, along with the previously undocumented 'usefrozen' argument and the signature in the documentation now correctly identifies the 'deprecated' argument as keyword-only.
shutil._BZ2_SUPPORTEDis alwaysTrueunderlazy_imports=alleven if it is not supported #150167