Skip to content

Commit 8f4bb35

Browse files
committed
Merge branch 'master' into better-lit-errors/149277
2 parents 7fb8fb6 + 6d2b551 commit 8f4bb35

6 files changed

Lines changed: 48 additions & 6 deletions

File tree

.github/workflows/reusable-check-html-ids.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ jobs:
2020
with:
2121
persist-credentials: false
2222
ref: ${{ github.event.pull_request.head.sha }}
23+
- name: 'Downgrade Git'
24+
# Temporarily downgrade to 2.43 until 2.55 is in the runner image,
25+
# to avoid "fatal: shallow file has changed since we read it" bug.
26+
# See https://github.com/python/cpython/issues/151365.
27+
run: |
28+
sudo apt-get install -y --allow-downgrades 'git=1:2.43.*' 'git-man=1:2.43.*'
29+
git --version
2330
- name: 'Find merge base'
2431
id: merge-base
2532
run: |

.github/workflows/reusable-context.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ jobs:
9090
|| ''
9191
}}
9292
93+
- name: 'Downgrade Git'
94+
# Temporarily downgrade to 2.43 until 2.55 is in the runner image,
95+
# to avoid "fatal: shallow file has changed since we read it" bug.
96+
# See https://github.com/python/cpython/issues/151365.
97+
if: github.event_name == 'pull_request'
98+
run: |
99+
sudo apt-get install -y --allow-downgrades 'git=1:2.43.*' 'git-man=1:2.43.*'
100+
git --version
101+
93102
# Adapted from https://github.com/actions/checkout/issues/520#issuecomment-1167205721
94103
- name: Fetch commits to get branch diff
95104
if: github.event_name == 'pull_request'

.github/workflows/reusable-docs.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ jobs:
4747
&& github.event.pull_request.head.sha
4848
|| ''
4949
}}
50+
- name: 'Downgrade Git'
51+
# Temporarily downgrade to 2.43 until 2.55 is in the runner image,
52+
# to avoid "fatal: shallow file has changed since we read it" bug.
53+
# See https://github.com/python/cpython/issues/151365.
54+
if: github.event_name == 'pull_request'
55+
run: |
56+
sudo apt-get install -y --allow-downgrades 'git=1:2.43.*' 'git-man=1:2.43.*'
57+
git --version
5058
# Adapted from https://github.com/actions/checkout/issues/520#issuecomment-1167205721
5159
- name: 'Fetch commits to get branch diff'
5260
if: github.event_name == 'pull_request'

Lib/shutil.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,28 @@
1818
except ImportError:
1919
_ZLIB_SUPPORTED = False
2020

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

2833
try:
29-
import lzma
30-
del lzma
34+
import _lzma
35+
del _lzma
3136
_LZMA_SUPPORTED = True
3237
except ImportError:
3338
_LZMA_SUPPORTED = False
3439

3540
try:
36-
from compression import zstd
37-
del zstd
41+
import _zstd
42+
del _zstd
3843
_ZSTD_SUPPORTED = True
3944
except ImportError:
4045
_ZSTD_SUPPORTED = False

Lib/test/test_shutil.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from test import support
3333
from test.support import os_helper, socket_helper
3434
from test.support.os_helper import TESTFN, FakePath
35+
from test.support.import_helper import ensure_lazy_imports
3536

3637
TESTFN2 = TESTFN + "2"
3738
TESTFN_SRC = TESTFN + "_SRC"
@@ -2362,6 +2363,14 @@ def _boo(filename, extract_dir, extra):
23622363
unregister_unpack_format('Boo2')
23632364
self.assertEqual(get_unpack_formats(), formats)
23642365

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

23662375
class TestMisc(BaseTest, unittest.TestCase):
23672376

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Speed up :mod:`shutil` import by probing the ``_bz2``, ``_lzma`` and
2+
``_zstd`` extension modules instead of importing the :mod:`bz2`,
3+
:mod:`lzma` and :mod:`compression.zstd` wrappers, which are now only
4+
imported when an archive is actually created or extracted.

0 commit comments

Comments
 (0)