|
10 | 10 | import os.path |
11 | 11 | import errno |
12 | 12 | import functools |
13 | | -import importlib |
14 | 13 | import socket |
15 | 14 | import subprocess |
16 | 15 | import random |
|
33 | 32 | from test import support |
34 | 33 | from test.support import os_helper, socket_helper |
35 | 34 | 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 |
37 | 36 |
|
38 | 37 | TESTFN2 = TESTFN + "2" |
39 | 38 | TESTFN_SRC = TESTFN + "_SRC" |
@@ -2323,39 +2322,13 @@ def _boo(filename, extract_dir, extra): |
2323 | 2322 | unregister_unpack_format('Boo2') |
2324 | 2323 | self.assertEqual(get_unpack_formats(), formats) |
2325 | 2324 |
|
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 | | - |
2346 | 2325 | 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"}) |
2359 | 2332 |
|
2360 | 2333 |
|
2361 | 2334 | class TestMisc(BaseTest, unittest.TestCase): |
|
0 commit comments