Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/reusable-check-html-ids.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ jobs:
with:
persist-credentials: false
ref: ${{ github.event.pull_request.head.sha }}
- name: 'Downgrade Git'
# Temporarily downgrade to 2.43 until 2.55 is in the runner image,
# to avoid "fatal: shallow file has changed since we read it" bug.
# See https://github.com/python/cpython/issues/151365.
run: |
sudo apt-get install -y --allow-downgrades 'git=1:2.43.*' 'git-man=1:2.43.*'
git --version
- name: 'Find merge base'
id: merge-base
run: |
Expand Down
9 changes: 9 additions & 0 deletions .github/workflows/reusable-context.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ jobs:
|| ''
}}

- name: 'Downgrade Git'
# Temporarily downgrade to 2.43 until 2.55 is in the runner image,
# to avoid "fatal: shallow file has changed since we read it" bug.
# See https://github.com/python/cpython/issues/151365.
if: github.event_name == 'pull_request'
run: |
sudo apt-get install -y --allow-downgrades 'git=1:2.43.*' 'git-man=1:2.43.*'
git --version

# Adapted from https://github.com/actions/checkout/issues/520#issuecomment-1167205721
- name: Fetch commits to get branch diff
if: github.event_name == 'pull_request'
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/reusable-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ jobs:
&& github.event.pull_request.head.sha
|| ''
}}
- name: 'Downgrade Git'
# Temporarily downgrade to 2.43 until 2.55 is in the runner image,
# to avoid "fatal: shallow file has changed since we read it" bug.
# See https://github.com/python/cpython/issues/151365.
if: github.event_name == 'pull_request'
run: |
sudo apt-get install -y --allow-downgrades 'git=1:2.43.*' 'git-man=1:2.43.*'
git --version
# Adapted from https://github.com/actions/checkout/issues/520#issuecomment-1167205721
- name: 'Fetch commits to get branch diff'
if: github.event_name == 'pull_request'
Expand Down
17 changes: 11 additions & 6 deletions Lib/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,28 @@
except ImportError:
_ZLIB_SUPPORTED = False

# bz2, lzma and compression.zstd are pure Python wrappers whose only
# importable dependency that may be missing is the extension module they
# wrap. Probe those extensions directly instead: it gives the same answer
# without executing the wrappers, which shutil only needs when an archive
# is actually created or extracted.
try:
import bz2
del bz2
import _bz2
del _bz2
_BZ2_SUPPORTED = True
except ImportError:
_BZ2_SUPPORTED = False

try:
import lzma
del lzma
import _lzma
del _lzma
_LZMA_SUPPORTED = True
except ImportError:
_LZMA_SUPPORTED = False

try:
from compression import zstd
del zstd
import _zstd
del _zstd
_ZSTD_SUPPORTED = True
except ImportError:
_ZSTD_SUPPORTED = False
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from test import support
from test.support import os_helper, socket_helper
from test.support.os_helper import TESTFN, FakePath
from test.support.import_helper import ensure_lazy_imports

TESTFN2 = TESTFN + "2"
TESTFN_SRC = TESTFN + "_SRC"
Expand Down Expand Up @@ -2362,6 +2363,14 @@ def _boo(filename, extract_dir, extra):
unregister_unpack_format('Boo2')
self.assertEqual(get_unpack_formats(), formats)

def test_compression_wrappers_not_imported_by_shutil(self):
# gh-154904: Importing shutil must not pull in the compression
# wrappers: they are only needed once an archive is actually created
# or extracted, and importing them measurably slows down every
# process that uses shutil.
ensure_lazy_imports("shutil",
{"bz2", "lzma", "compression", "compression.zstd"})


class TestMisc(BaseTest, unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Speed up :mod:`shutil` import by probing the ``_bz2``, ``_lzma`` and
``_zstd`` extension modules instead of importing the :mod:`bz2`,
:mod:`lzma` and :mod:`compression.zstd` wrappers, which are now only
imported when an archive is actually created or extracted.
Loading