Skip to content

Commit 076562a

Browse files
committed
Address review: use ensure_lazy_imports, drop redundant flags test
Per review feedback on GH-154908: - Rewrite test_compression_wrappers_not_imported_by_shutil on top of test.support.import_helper.ensure_lazy_imports instead of hand-rolling an assert_python_ok subprocess. - Remove test_compression_supported_flags: since each wrapper imports its extension module at the top level, the assertion is near-tautological, and the zlib subtest compared importing zlib with itself.
1 parent 7cd5807 commit 076562a

1 file changed

Lines changed: 7 additions & 34 deletions

File tree

Lib/test/test_shutil.py

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import os.path
1111
import errno
1212
import functools
13-
import importlib
1413
import socket
1514
import subprocess
1615
import random
@@ -33,7 +32,7 @@
3332
from test import support
3433
from test.support import os_helper, socket_helper
3534
from test.support.os_helper import TESTFN, FakePath
36-
from test.support.script_helper import assert_python_ok
35+
from test.support.import_helper import ensure_lazy_imports
3736

3837
TESTFN2 = TESTFN + "2"
3938
TESTFN_SRC = TESTFN + "_SRC"
@@ -2323,39 +2322,13 @@ def _boo(filename, extract_dir, extra):
23232322
unregister_unpack_format('Boo2')
23242323
self.assertEqual(get_unpack_formats(), formats)
23252324

2326-
def test_compression_supported_flags(self):
2327-
# shutil determines compression support by probing the extension
2328-
# modules (_bz2, _lzma, _zstd) rather than importing the pure Python
2329-
# wrappers. The answer must match what importing the wrapper does,
2330-
# including on builds where the extension is missing or fails to load.
2331-
for wrapper, supported in (
2332-
('zlib', shutil._ZLIB_SUPPORTED),
2333-
('bz2', shutil._BZ2_SUPPORTED),
2334-
('lzma', shutil._LZMA_SUPPORTED),
2335-
('compression.zstd', shutil._ZSTD_SUPPORTED),
2336-
):
2337-
with self.subTest(wrapper=wrapper):
2338-
try:
2339-
importlib.import_module(wrapper)
2340-
except ImportError:
2341-
importable = False
2342-
else:
2343-
importable = True
2344-
self.assertEqual(supported, importable)
2345-
23462325
def test_compression_wrappers_not_imported_by_shutil(self):
2347-
# Importing shutil must not pull in the compression wrappers: they are
2348-
# only needed once an archive is actually created or extracted, and
2349-
# importing them measurably slows down every process that uses shutil.
2350-
wrappers = ('bz2', 'lzma', 'compression', 'compression.zstd')
2351-
script = (
2352-
'import sys, shutil; '
2353-
f'print([m for m in {wrappers!r} if m in sys.modules])'
2354-
)
2355-
# -I so that a sitecustomize/usercustomize importing one of these
2356-
# cannot make the test fail spuriously.
2357-
rc, stdout, stderr = assert_python_ok('-I', '-c', script)
2358-
self.assertEqual(stdout.decode().strip(), '[]', stderr)
2326+
# gh-154904: Importing shutil must not pull in the compression
2327+
# wrappers: they are only needed once an archive is actually created
2328+
# or extracted, and importing them measurably slows down every
2329+
# process that uses shutil.
2330+
ensure_lazy_imports("shutil",
2331+
{"bz2", "lzma", "compression", "compression.zstd"})
23592332

23602333

23612334
class TestMisc(BaseTest, unittest.TestCase):

0 commit comments

Comments
 (0)