[Python-checkins] bpo-40275: More lazy imports in test.support (GH-20131)

Hai Shi webhook-mailer at python.org
Mon May 18 18:03:05 EDT 2020


https://github.com/python/cpython/commit/a3ec3ad9e20e7d9ed148d4cfbd22aebec608b42a
commit: a3ec3ad9e20e7d9ed148d4cfbd22aebec608b42a
branch: master
author: Hai Shi <shihai1992 at gmail.com>
committer: GitHub <noreply at github.com>
date: 2020-05-19T00:02:57+02:00
summary:

bpo-40275: More lazy imports in test.support (GH-20131)

Make the the following imports lazy in test.support:

* bz2
* gzip
* lzma
* resource
* zlib

The following test.support decorators now need to be called
with parenthesis:

* @support.requires_bz2
* @support.requires_gzip
* @support.requires_lzma
* @support.requires_zlib

For example, "@requires_zlib" becomes "@requires_zlib()".

files:
M Lib/test/support/__init__.py
M Lib/test/test_importlib/test_zip.py
M Lib/test/test_logging.py
M Lib/test/test_shutil.py
M Lib/test/test_tarfile.py
M Lib/test/test_venv.py
M Lib/test/test_zipapp.py
M Lib/test/test_zipfile.py
M Lib/test/test_zipfile64.py
M Lib/test/test_zipimport.py

diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index a9f9908c7fac9..8dee5b9dcc7ab 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -28,31 +28,6 @@
 
 from .testresult import get_test_runner
 
-try:
-    import zlib
-except ImportError:
-    zlib = None
-
-try:
-    import gzip
-except ImportError:
-    gzip = None
-
-try:
-    import bz2
-except ImportError:
-    bz2 = None
-
-try:
-    import lzma
-except ImportError:
-    lzma = None
-
-try:
-    import resource
-except ImportError:
-    resource = None
-
 __all__ = [
     # globals
     "PIPE_MAX_SIZE", "verbose", "max_memuse", "use_resources", "failfast",
@@ -705,13 +680,33 @@ def dec(*args, **kwargs):
     float.__getformat__("double").startswith("IEEE"),
     "test requires IEEE 754 doubles")
 
-requires_zlib = unittest.skipUnless(zlib, 'requires zlib')
+def requires_zlib(reason='requires zlib'):
+    try:
+        import zlib
+    except ImportError:
+        zlib = None
+    return unittest.skipUnless(zlib, reason)
 
-requires_gzip = unittest.skipUnless(gzip, 'requires gzip')
+def requires_gzip(reason='requires gzip'):
+    try:
+        import gzip
+    except ImportError:
+        gzip = None
+    return unittest.skipUnless(gzip, reason)
 
-requires_bz2 = unittest.skipUnless(bz2, 'requires bz2')
+def requires_bz2(reason='requires bz2'):
+    try:
+        import bz2
+    except ImportError:
+        bz2 = None
+    return unittest.skipUnless(bz2, reason)
 
-requires_lzma = unittest.skipUnless(lzma, 'requires lzma')
+def requires_lzma(reason='requires lzma'):
+    try:
+        import lzma
+    except ImportError:
+        lzma = None
+    return unittest.skipUnless(lzma, reason)
 
 is_jython = sys.platform.startswith('java')
 
@@ -1062,6 +1057,10 @@ def check_syntax_warning(testcase, statement, errtext='', *, lineno=1, offset=No
 
 def open_urlresource(url, *args, **kw):
     import urllib.request, urllib.parse
+    try:
+        import gzip
+    except ImportError:
+        gzip = None
 
     check = kw.pop('check', None)
 
@@ -2579,11 +2578,16 @@ def __enter__(self):
                     self.old_modes[report_type] = old_mode, old_file
 
         else:
-            if resource is not None:
+            try:
+                import resource
+                self.resource = resource
+            except ImportError:
+                self.resource = None
+            if self.resource is not None:
                 try:
-                    self.old_value = resource.getrlimit(resource.RLIMIT_CORE)
-                    resource.setrlimit(resource.RLIMIT_CORE,
-                                       (0, self.old_value[1]))
+                    self.old_value = self.resource.getrlimit(self.resource.RLIMIT_CORE)
+                    self.resource.setrlimit(self.resource.RLIMIT_CORE,
+                                            (0, self.old_value[1]))
                 except (ValueError, OSError):
                     pass
 
@@ -2621,9 +2625,9 @@ def __exit__(self, *ignore_exc):
                     msvcrt.CrtSetReportMode(report_type, old_mode)
                     msvcrt.CrtSetReportFile(report_type, old_file)
         else:
-            if resource is not None:
+            if self.resource is not None:
                 try:
-                    resource.setrlimit(resource.RLIMIT_CORE, self.old_value)
+                    self.resource.setrlimit(self.resource.RLIMIT_CORE, self.old_value)
                 except (ValueError, OSError):
                     pass
 
diff --git a/Lib/test/test_importlib/test_zip.py b/Lib/test/test_importlib/test_zip.py
index 9466ca4a5f424..fa87cd7cb1096 100644
--- a/Lib/test/test_importlib/test_zip.py
+++ b/Lib/test/test_importlib/test_zip.py
@@ -10,7 +10,7 @@
 from test.support import requires_zlib
 
 
- at requires_zlib
+ at requires_zlib()
 class TestZip(unittest.TestCase):
     root = 'test.test_importlib.data'
 
@@ -50,7 +50,7 @@ def test_files(self):
             assert '.whl/' in path, path
 
 
- at requires_zlib
+ at requires_zlib()
 class TestEgg(TestZip):
     def setUp(self):
         # Find the path to the example-*.egg so we can add it to the front of
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py
index 4cc45f7107115..9a114451913e8 100644
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -5076,7 +5076,7 @@ def rotator(self, source, dest):
         self.assertFalse(os.path.exists(rh.namer(self.fn + ".1")))
         rh.close()
 
-    @support.requires_zlib
+    @support.requires_zlib()
     def test_rotator(self):
         def namer(name):
             return name + ".gz"
diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py
index b9fdfd1350a09..e56b337083c8f 100644
--- a/Lib/test/test_shutil.py
+++ b/Lib/test/test_shutil.py
@@ -1246,7 +1246,7 @@ class TestArchives(BaseTest, unittest.TestCase):
 
     ### shutil.make_archive
 
-    @support.requires_zlib
+    @support.requires_zlib()
     def test_make_tarball(self):
         # creating something to tar
         root_dir, base_dir = self._create_files('')
@@ -1302,7 +1302,7 @@ def _create_files(self, base_dir='dist'):
             write_file((root_dir, 'outer'), 'xxx')
         return root_dir, base_dir
 
-    @support.requires_zlib
+    @support.requires_zlib()
     @unittest.skipUnless(shutil.which('tar'),
                          'Need the tar command to run')
     def test_tarfile_vs_tar(self):
@@ -1335,7 +1335,7 @@ def test_tarfile_vs_tar(self):
         self.assertEqual(tarball, base_name + '.tar')
         self.assertTrue(os.path.isfile(tarball))
 
-    @support.requires_zlib
+    @support.requires_zlib()
     def test_make_zipfile(self):
         # creating something to zip
         root_dir, base_dir = self._create_files()
@@ -1372,7 +1372,7 @@ def test_make_zipfile(self):
                     ['dist/', 'dist/sub/', 'dist/sub2/',
                      'dist/file1', 'dist/file2', 'dist/sub/file3'])
 
-    @support.requires_zlib
+    @support.requires_zlib()
     @unittest.skipUnless(shutil.which('zip'),
                          'Need the zip command to run')
     def test_zipfile_vs_zip(self):
@@ -1398,7 +1398,7 @@ def test_zipfile_vs_zip(self):
             names2 = zf.namelist()
         self.assertEqual(sorted(names), sorted(names2))
 
-    @support.requires_zlib
+    @support.requires_zlib()
     @unittest.skipUnless(shutil.which('unzip'),
                          'Need the unzip command to run')
     def test_unzip_zipfile(self):
@@ -1427,7 +1427,7 @@ def test_make_archive(self):
         base_name = os.path.join(tmpdir, 'archive')
         self.assertRaises(ValueError, make_archive, base_name, 'xxx')
 
-    @support.requires_zlib
+    @support.requires_zlib()
     def test_make_archive_owner_group(self):
         # testing make_archive with owner and group, with various combinations
         # this works even if there's not gid/uid support
@@ -1455,7 +1455,7 @@ def test_make_archive_owner_group(self):
         self.assertTrue(os.path.isfile(res))
 
 
-    @support.requires_zlib
+    @support.requires_zlib()
     @unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support")
     def test_tarfile_root_owner(self):
         root_dir, base_dir = self._create_files()
@@ -1500,7 +1500,7 @@ def test_make_tarfile_in_curdir(self):
             self.assertEqual(make_archive('test', 'tar'), 'test.tar')
             self.assertTrue(os.path.isfile('test.tar'))
 
-    @support.requires_zlib
+    @support.requires_zlib()
     def test_make_zipfile_in_curdir(self):
         # Issue #21280
         root_dir = self.mkdtemp()
@@ -1555,20 +1555,20 @@ def check_unpack_archive_with_converter(self, format, converter):
     def test_unpack_archive_tar(self):
         self.check_unpack_archive('tar')
 
-    @support.requires_zlib
+    @support.requires_zlib()
     def test_unpack_archive_gztar(self):
         self.check_unpack_archive('gztar')
 
-    @support.requires_bz2
+    @support.requires_bz2()
     def test_unpack_archive_bztar(self):
         self.check_unpack_archive('bztar')
 
-    @support.requires_lzma
+    @support.requires_lzma()
     @unittest.skipIf(AIX and not _maxdataOK(), "AIX MAXDATA must be 0x20000000 or larger")
     def test_unpack_archive_xztar(self):
         self.check_unpack_archive('xztar')
 
-    @support.requires_zlib
+    @support.requires_zlib()
     def test_unpack_archive_zip(self):
         self.check_unpack_archive('zip')
 
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index 25e9e93604476..be66f1f89e6f3 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -57,21 +57,21 @@ class TarTest:
     def mode(self):
         return self.prefix + self.suffix
 
- at support.requires_gzip
+ at support.requires_gzip()
 class GzipTest:
     tarname = gzipname
     suffix = 'gz'
     open = gzip.GzipFile if gzip else None
     taropen = tarfile.TarFile.gzopen
 
- at support.requires_bz2
+ at support.requires_bz2()
 class Bz2Test:
     tarname = bz2name
     suffix = 'bz2'
     open = bz2.BZ2File if bz2 else None
     taropen = tarfile.TarFile.bz2open
 
- at support.requires_lzma
+ at support.requires_lzma()
 class LzmaTest:
     tarname = xzname
     suffix = 'xz'
diff --git a/Lib/test/test_venv.py b/Lib/test/test_venv.py
index b2794cd992a06..44c62193bf7cc 100644
--- a/Lib/test/test_venv.py
+++ b/Lib/test/test_venv.py
@@ -530,7 +530,7 @@ def do_test_with_pip(self, system_site_packages):
 
     # Issue #26610: pip/pep425tags.py requires ctypes
     @unittest.skipUnless(ctypes, 'pip requires ctypes')
-    @requires_zlib
+    @requires_zlib()
     def test_with_pip(self):
         self.do_test_with_pip(False)
         self.do_test_with_pip(True)
diff --git a/Lib/test/test_zipapp.py b/Lib/test/test_zipapp.py
index 73bddc79c17ab..69f2e55d56384 100644
--- a/Lib/test/test_zipapp.py
+++ b/Lib/test/test_zipapp.py
@@ -101,7 +101,7 @@ def test_create_archive_default_target(self):
         expected_target = self.tmpdir / 'source.pyz'
         self.assertTrue(expected_target.is_file())
 
-    @requires_zlib
+    @requires_zlib()
     def test_create_archive_with_compression(self):
         # Test packing a directory into a compressed archive.
         source = self.tmpdir / 'source'
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
index 29d98c8092d30..c9ca1ddaafe19 100644
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -640,7 +640,7 @@ def test_add_file_after_2107(self):
             self.assertEqual(zinfo.date_time, (2107, 12, 31, 23, 59, 59))
 
 
- at requires_zlib
+ at requires_zlib()
 class DeflateTestsWithSourceFile(AbstractTestsWithSourceFile,
                                  unittest.TestCase):
     compression = zipfile.ZIP_DEFLATED
@@ -656,12 +656,12 @@ def test_per_file_compression(self):
             self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED)
             self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
 
- at requires_bz2
+ at requires_bz2()
 class Bzip2TestsWithSourceFile(AbstractTestsWithSourceFile,
                                unittest.TestCase):
     compression = zipfile.ZIP_BZIP2
 
- at requires_lzma
+ at requires_lzma()
 class LzmaTestsWithSourceFile(AbstractTestsWithSourceFile,
                               unittest.TestCase):
     compression = zipfile.ZIP_LZMA
@@ -1075,17 +1075,17 @@ def test_generated_valid_zip64_extra(self):
                     self.assertEqual(zf.read(zinfo), expected_content)
 
 
- at requires_zlib
+ at requires_zlib()
 class DeflateTestZip64InSmallFiles(AbstractTestZip64InSmallFiles,
                                    unittest.TestCase):
     compression = zipfile.ZIP_DEFLATED
 
- at requires_bz2
+ at requires_bz2()
 class Bzip2TestZip64InSmallFiles(AbstractTestZip64InSmallFiles,
                                  unittest.TestCase):
     compression = zipfile.ZIP_BZIP2
 
- at requires_lzma
+ at requires_lzma()
 class LzmaTestZip64InSmallFiles(AbstractTestZip64InSmallFiles,
                                 unittest.TestCase):
     compression = zipfile.ZIP_LZMA
@@ -1120,15 +1120,15 @@ def test_write_after_close(self):
 class StoredWriterTests(AbstractWriterTests, unittest.TestCase):
     compression = zipfile.ZIP_STORED
 
- at requires_zlib
+ at requires_zlib()
 class DeflateWriterTests(AbstractWriterTests, unittest.TestCase):
     compression = zipfile.ZIP_DEFLATED
 
- at requires_bz2
+ at requires_bz2()
 class Bzip2WriterTests(AbstractWriterTests, unittest.TestCase):
     compression = zipfile.ZIP_BZIP2
 
- at requires_lzma
+ at requires_lzma()
 class LzmaWriterTests(AbstractWriterTests, unittest.TestCase):
     compression = zipfile.ZIP_LZMA
 
@@ -1582,7 +1582,7 @@ def test_unsupported_version(self):
         self.assertRaises(NotImplementedError, zipfile.ZipFile,
                           io.BytesIO(data), 'r')
 
-    @requires_zlib
+    @requires_zlib()
     def test_read_unicode_filenames(self):
         # bug #10801
         fname = findfile('zip_cp437_header.zip')
@@ -2018,7 +2018,7 @@ def test_seek_tell(self):
                 fp.seek(0, os.SEEK_SET)
                 self.assertEqual(fp.tell(), 0)
 
-    @requires_bz2
+    @requires_bz2()
     def test_decompress_without_3rd_party_library(self):
         data = b'PK\x05\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
         zip_file = io.BytesIO(data)
@@ -2076,7 +2076,7 @@ class StoredBadCrcTests(AbstractBadCrcTests, unittest.TestCase):
         b'lePK\005\006\0\0\0\0\001\0\001\0003\000'
         b'\0\0/\0\0\0\0\0')
 
- at requires_zlib
+ at requires_zlib()
 class DeflateBadCrcTests(AbstractBadCrcTests, unittest.TestCase):
     compression = zipfile.ZIP_DEFLATED
     zip_with_bad_crc = (
@@ -2089,7 +2089,7 @@ class DeflateBadCrcTests(AbstractBadCrcTests, unittest.TestCase):
         b'\x00afilePK\x05\x06\x00\x00\x00\x00\x01\x00'
         b'\x01\x003\x00\x00\x003\x00\x00\x00\x00\x00')
 
- at requires_bz2
+ at requires_bz2()
 class Bzip2BadCrcTests(AbstractBadCrcTests, unittest.TestCase):
     compression = zipfile.ZIP_BZIP2
     zip_with_bad_crc = (
@@ -2105,7 +2105,7 @@ class Bzip2BadCrcTests(AbstractBadCrcTests, unittest.TestCase):
         b'\x05\x06\x00\x00\x00\x00\x01\x00\x01\x003\x00\x00\x00[\x00'
         b'\x00\x00\x00\x00')
 
- at requires_lzma
+ at requires_lzma()
 class LzmaBadCrcTests(AbstractBadCrcTests, unittest.TestCase):
     compression = zipfile.ZIP_LZMA
     zip_with_bad_crc = (
@@ -2172,7 +2172,7 @@ def test_bad_password(self):
         self.zip2.setpassword(b"perl")
         self.assertRaises(RuntimeError, self.zip2.read, "zero")
 
-    @requires_zlib
+    @requires_zlib()
     def test_good_password(self):
         self.zip.setpassword(b"python")
         self.assertEqual(self.zip.read("test.txt"), self.plain)
@@ -2318,17 +2318,17 @@ class StoredTestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles,
                                        unittest.TestCase):
     compression = zipfile.ZIP_STORED
 
- at requires_zlib
+ at requires_zlib()
 class DeflateTestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles,
                                         unittest.TestCase):
     compression = zipfile.ZIP_DEFLATED
 
- at requires_bz2
+ at requires_bz2()
 class Bzip2TestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles,
                                       unittest.TestCase):
     compression = zipfile.ZIP_BZIP2
 
- at requires_lzma
+ at requires_lzma()
 class LzmaTestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles,
                                      unittest.TestCase):
     compression = zipfile.ZIP_LZMA
@@ -2416,7 +2416,7 @@ def test_open_write(self):
                     self.assertEqual(zipf.read('twos'), b'222')
 
 
- at requires_zlib
+ at requires_zlib()
 class TestsWithMultipleOpens(unittest.TestCase):
     @classmethod
     def setUpClass(cls):
@@ -2682,7 +2682,7 @@ def test_list_command(self):
                                   PYTHONIOENCODING='ascii:backslashreplace')
             self.assertEqual(out, expected)
 
-    @requires_zlib
+    @requires_zlib()
     def test_create_command(self):
         self.addCleanup(unlink, TESTFN)
         with open(TESTFN, 'w') as f:
diff --git a/Lib/test/test_zipfile64.py b/Lib/test/test_zipfile64.py
index 56746bc08f63d..3a788de221264 100644
--- a/Lib/test/test_zipfile64.py
+++ b/Lib/test/test_zipfile64.py
@@ -73,7 +73,7 @@ def testStored(self):
             self.assertFalse(f.closed)
         self.zipTest(TESTFN2, zipfile.ZIP_STORED)
 
-    @requires_zlib
+    @requires_zlib()
     def testDeflated(self):
         # Try the temp file first.  If we do TESTFN2 first, then it hogs
         # gigabytes of disk space for the duration of the test.
diff --git a/Lib/test/test_zipimport.py b/Lib/test/test_zipimport.py
index 2af8689c1d2cb..560286071c690 100644
--- a/Lib/test/test_zipimport.py
+++ b/Lib/test/test_zipimport.py
@@ -683,7 +683,7 @@ def testLargestPossibleComment(self):
         self.doTest(".py", files, TESTMOD, comment=b"c" * ((1 << 16) - 1))
 
 
- at support.requires_zlib
+ at support.requires_zlib()
 class CompressedZipImportTestCase(UncompressedZipImportTestCase):
     compression = ZIP_DEFLATED
 



More information about the Python-checkins mailing list