René's URL Explorer Experiment


Title: gh-154904: Speed up import of shutil by probing compression extensions by maxday · Pull Request #154908 · python/cpython · GitHub

Open Graph Title: gh-154904: Speed up import of shutil by probing compression extensions by maxday · Pull Request #154908 · python/cpython

X Title: gh-154904: Speed up import of shutil by probing compression extensions by maxday · Pull Request #154908 · python/cpython

Description: Speed up import shutil by probing compression extensions instead of importing the wrappers shutil imports bz2, lzma and compression.zstd at module scope only to set the _*_SUPPORTED booleans, then discards them, also dragging incompression._common[._streams] and compression.zstd._zstdfile. Every program importing shutil pays for it even if it never touches an archive. They are pure Python wrappers whose only dependency that can be missing is the extension they wrap, so probe that directly: - import bz2 - del bz2 + import _bz2 + del _bz2 Same for lzma→_lzma, compression.zstd→_zstd. zlib is left alone: it is the extension. tarfile/zipfile still import the wrappers when an archive is created or extracted. Net import cost (median of 40 fresh interpreters, minus -c pass, one core): before after delta import shutil 12.75 ms 10.25 ms −2.50 ms (−19.6%) import urllib.request 36.64 ms 34.30 ms −2.34 ms (−6.4%) urllib.request benefits via tempfile→shutil. Motivation is short-lived processes on constrained hardware (CLI tools, serverless cold starts). No API change: flags, both registries and get_{archive,unpack}_formats() are unchanged, including on builds missing a compression library. The probe is a real import, so ImportError semantics are exact, unlike find_spec('bz2'), which resolves the always-present wrapper and reports success where _bz2 failed to compile (the case on my machine). PEP 810 lazy import also won't do: the flags are read at module scope (shutil.py:1145, :1354), so must be final at import time. Tests Added to TestArchives: test_compression_supported_flags pins the flags to real import semantics, test_compression_wrappers_not_imported_by_shutil prevents the eager imports returning. Existing test_unpack_archive_* coverage unchanged; patchcheck clean. $ ./python -m test test_shutil -v -m "test_compression*" ok, ok $ ./python -m test test_shutil test_zipfile test_tarfile test_importlib \ test_site test_venv test_zipimport test_bz2 test_lzma test_zstd \ test_gzip test_urllib2 test_logging -j6 SUCCESS — 14 OK, run=3,959 skipped=314 (test_bz2 skips on main too) negative controls, each test fails without the half it covers: find_spec variant: ..._supported_flags FAIL: True != False unpatched main: ..._wrappers_not_imported FAIL: "['lzma', 'compression', 'compression.zstd']" != '[]' fixes: #154904 Issue: gh-154904

Open Graph Description: Speed up import shutil by probing compression extensions instead of importing the wrappers shutil imports bz2, lzma and compression.zstd at module scope only to set the _*_SUPPORTED booleans, then ...

X Description: Speed up import shutil by probing compression extensions instead of importing the wrappers shutil imports bz2, lzma and compression.zstd at module scope only to set the _*_SUPPORTED booleans, then ...

Opengraph URL: https://github.com/python/cpython/pull/154908

X: @github

direct link

Domain: github.com

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:4bb2ffd9-2e33-f8a3-e700-c0f98d00c59b
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idCAEE:17FB7:34840A2:4298378:6B021263
html-safe-noncea91072297bf04736d30cd869b702997c3f51d8629cbc81ff68920d6c84788383
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQUVFOjE3RkI3OjM0ODQwQTI6NDI5ODM3ODo2QjAyMTI2MyIsInZpc2l0b3JfaWQiOiIyNjg4MDAxNzQwNjQ4ODc4NjkxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac9b406d78c4c0ff9a7921c13b96f9ebc37b2e0f46d2e26493c3b106e10a3317e0
hovercard-subject-tagpull_request:4165747394
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/python/cpython/pull/154908/files
twitter:imagehttps://avatars.githubusercontent.com/u/864493?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/864493?s=400&v=4
og:image:altSpeed up import shutil by probing compression extensions instead of importing the wrappers shutil imports bz2, lzma and compression.zstd at module scope only to set the _*_SUPPORTED booleans, then ...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release423dbb54c5e7bfecc838eb387c1435eb3d0e760d
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/pull/154908/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F154908%2Ffiles
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F154908%2Ffiles
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/pull/154908/files
Reloadhttps://github.com/python/cpython/pull/154908/files
Reloadhttps://github.com/python/cpython/pull/154908/files
pythonhttps://github.com/python
cpythonhttps://github.com/python/cpython
Codehttps://github.com/python/cpython
Issues5k+ (5k+)https://github.com/python/cpython/issues
Pull requests2.4k (2.4k)https://github.com/python/cpython/pulls
Actionshttps://github.com/python/cpython/actions
Projectshttps://github.com/python/cpython/projects
Security and qualityhttps://github.com/python/cpython/security
Insightshttps://github.com/python/cpython/insights
Sign up for GitHub https://github.com/signup?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose
emmatypinghttps://github.com/emmatyping
python:mainhttps://github.com/python/cpython/tree/main
maxday:maxday/shutil-perfhttps://github.com/maxday/cpython/tree/maxday/shutil-perf
Conversation 13 https://github.com/python/cpython/pull/154908
Commits 2 https://github.com/python/cpython/pull/154908/commits
Checks 56 https://github.com/python/cpython/pull/154908/checks
Files changed 3 https://github.com/python/cpython/pull/154908/files
gh-154904: Speed up import of shutil by probing compression extensions https://github.com/python/cpython/pull/154908/files#top
Show all changes 2 commits https://github.com/python/cpython/pull/154908/files
7cd5807 gh-154904: Speed up import of shutil by probing compression extensions maxday Jul 30, 2026 https://github.com/python/cpython/pull/154908/commits/7cd5807fcd6066eacb59aa4e76bc0070ccb8b1e9
076562a Address review: use ensure_lazy_imports, drop redundant flags test maxday Jul 30, 2026 https://github.com/python/cpython/pull/154908/commits/076562acf3ab246d9b0b2e253bdf5af5b5d3dc7e
Clear filters https://github.com/python/cpython/pull/154908/files
Please reload this pagehttps://github.com/python/cpython/pull/154908/files
Please reload this pagehttps://github.com/python/cpython/pull/154908/files
shutil.py https://github.com/python/cpython/pull/154908/files#diff-db8ac59326160713929e0e1973aef54f0280fe9f154ef24d14244909a0e0689b
test_shutil.py https://github.com/python/cpython/pull/154908/files#diff-ed52359d591fff799c0b81414bdfd9a90541cbc3b97b50a7ebaca38ed98062e7
2026-07-30-02-15-00.gh-issue-154904.Kx9mAq.rst https://github.com/python/cpython/pull/154908/files#diff-1288315f5c0e9e837c5e9ab768add2a4d9890dc95648c61af1ae2aee9325b3bf
https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L582
Lib/shutil.pyhttps://github.com/python/cpython/pull/154908/files#diff-db8ac59326160713929e0e1973aef54f0280fe9f154ef24d14244909a0e0689b
View file https://github.com/maxday/cpython/blob/076562acf3ab246d9b0b2e253bdf5af5b5d3dc7e/Lib/shutil.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/154908/{{ revealButtonHref }}
https://github.com/python/cpython/pull/154908/files#diff-db8ac59326160713929e0e1973aef54f0280fe9f154ef24d14244909a0e0689b
https://github.com/python/cpython/pull/154908/files#diff-db8ac59326160713929e0e1973aef54f0280fe9f154ef24d14244909a0e0689b
https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L583
Lib/test/test_shutil.pyhttps://github.com/python/cpython/pull/154908/files#diff-ed52359d591fff799c0b81414bdfd9a90541cbc3b97b50a7ebaca38ed98062e7
View file https://github.com/maxday/cpython/blob/076562acf3ab246d9b0b2e253bdf5af5b5d3dc7e/Lib/test/test_shutil.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/154908/{{ revealButtonHref }}
https://github.com/python/cpython/pull/154908/files#diff-ed52359d591fff799c0b81414bdfd9a90541cbc3b97b50a7ebaca38ed98062e7
https://github.com/python/cpython/pull/154908/files#diff-ed52359d591fff799c0b81414bdfd9a90541cbc3b97b50a7ebaca38ed98062e7
https://github.com/python/cpython/pull/154908/files#diff-ed52359d591fff799c0b81414bdfd9a90541cbc3b97b50a7ebaca38ed98062e7
https://github.com/python/cpython/pull/154908/files#diff-ed52359d591fff799c0b81414bdfd9a90541cbc3b97b50a7ebaca38ed98062e7
Misc/NEWS.d/next/Library/2026-07-30-02-15-00.gh-issue-154904.Kx9mAq.rsthttps://github.com/python/cpython/pull/154908/files#diff-1288315f5c0e9e837c5e9ab768add2a4d9890dc95648c61af1ae2aee9325b3bf
View file https://github.com/maxday/cpython/blob/076562acf3ab246d9b0b2e253bdf5af5b5d3dc7e/Misc/NEWS.d/next/Library/2026-07-30-02-15-00.gh-issue-154904.Kx9mAq.rst
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/154908/{{ revealButtonHref }}
Please reload this pagehttps://github.com/python/cpython/pull/154908/files
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.