Skip to content

Commit 42d107c

Browse files
committed
Make zlib required instead of optional.
This simplifies code by removing a pile of conditionals and alternative logic. Further simplification could be done in `Modules/binascii.c` to simply have binascii.crc32 be a reference to zlib.crc32. Also the conditional logic around zlib inflaceCopy existing and `HAVE_ZLIB_COPY` can likely be removed as that API was added to zlib in 2003. The Windows build already treats zlib as required.
1 parent 3751b6b commit 42d107c

29 files changed

+85
-462
lines changed

Lib/distutils/tests/test_archive_util.py

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@
3030
except ImportError:
3131
ZIP_SUPPORT = find_executable('zip')
3232

33-
try:
34-
import zlib
35-
ZLIB_SUPPORT = True
36-
except ImportError:
37-
ZLIB_SUPPORT = False
33+
import zlib
3834

3935
try:
4036
import bz2
@@ -63,15 +59,13 @@ class ArchiveUtilTestCase(support.TempdirManager,
6359
support.LoggingSilencer,
6460
unittest.TestCase):
6561

66-
@unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
6762
def test_make_tarball(self, name='archive'):
6863
# creating something to tar
6964
tmpdir = self._create_files()
7065
self._make_tarball(tmpdir, name, '.tar.gz')
7166
# trying an uncompressed one
7267
self._make_tarball(tmpdir, name, '.tar', compress=None)
7368

74-
@unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
7569
def test_make_tarball_gzip(self):
7670
tmpdir = self._create_files()
7771
self._make_tarball(tmpdir, 'archive', '.tar.gz', compress='gzip')
@@ -144,9 +138,8 @@ def _create_files(self):
144138
os.mkdir(os.path.join(dist, 'sub2'))
145139
return tmpdir
146140

147-
@unittest.skipUnless(find_executable('tar') and find_executable('gzip')
148-
and ZLIB_SUPPORT,
149-
'Need the tar, gzip and zlib command to run')
141+
@unittest.skipUnless(find_executable('tar') and find_executable('gzip'),
142+
'Need the tar and gzip commands to run')
150143
def test_tarfile_vs_tar(self):
151144
tmpdir = self._create_files()
152145
tmpdir2 = self.mkdtemp()
@@ -234,8 +227,7 @@ def test_compress_deprecated(self):
234227
self.assertFalse(os.path.exists(tarball))
235228
self.assertEqual(len(w.warnings), 1)
236229

237-
@unittest.skipUnless(ZIP_SUPPORT and ZLIB_SUPPORT,
238-
'Need zip and zlib support to run')
230+
@unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
239231
def test_make_zipfile(self):
240232
# creating something to tar
241233
tmpdir = self._create_files()
@@ -249,32 +241,6 @@ def test_make_zipfile(self):
249241
with zipfile.ZipFile(tarball) as zf:
250242
self.assertEqual(sorted(zf.namelist()), self._zip_created_files)
251243

252-
@unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
253-
def test_make_zipfile_no_zlib(self):
254-
patch(self, archive_util.zipfile, 'zlib', None) # force zlib ImportError
255-
256-
called = []
257-
zipfile_class = zipfile.ZipFile
258-
def fake_zipfile(*a, **kw):
259-
if kw.get('compression', None) == zipfile.ZIP_STORED:
260-
called.append((a, kw))
261-
return zipfile_class(*a, **kw)
262-
263-
patch(self, archive_util.zipfile, 'ZipFile', fake_zipfile)
264-
265-
# create something to tar and compress
266-
tmpdir = self._create_files()
267-
base_name = os.path.join(self.mkdtemp(), 'archive')
268-
with change_cwd(tmpdir):
269-
make_zipfile(base_name, 'dist')
270-
271-
tarball = base_name + '.zip'
272-
self.assertEqual(called,
273-
[((tarball, "w"), {'compression': zipfile.ZIP_STORED})])
274-
self.assertTrue(os.path.exists(tarball))
275-
with zipfile.ZipFile(tarball) as zf:
276-
self.assertEqual(sorted(zf.namelist()), self._zip_created_files)
277-
278244
def test_check_archive_formats(self):
279245
self.assertEqual(check_archive_formats(['gztar', 'xxx', 'zip']),
280246
'xxx')
@@ -308,7 +274,6 @@ def test_make_archive_tar(self):
308274
self.assertEqual(os.path.basename(res), 'archive.tar')
309275
self.assertEqual(self._tarinfo(res), self._created_files)
310276

311-
@unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
312277
def test_make_archive_gztar(self):
313278
base_dir = self._create_files()
314279
base_name = os.path.join(self.mkdtemp() , 'archive')
@@ -362,7 +327,6 @@ def test_make_archive_owner_group(self):
362327
owner='kjhkjhkjg', group='oihohoh')
363328
self.assertTrue(os.path.exists(res))
364329

365-
@unittest.skipUnless(ZLIB_SUPPORT, "Requires zlib")
366330
@unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support")
367331
def test_tarfile_root_owner(self):
368332
tmpdir = self._create_files()

Lib/distutils/tests/test_bdist_dumb.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import os
44
import sys
5-
import zipfile
65
import unittest
6+
import zipfile
7+
import zlib
78
from test.support import run_unittest
89

910
from distutils.core import Distribution
@@ -19,11 +20,6 @@
1920
2021
"""
2122

22-
try:
23-
import zlib
24-
ZLIB_SUPPORT = True
25-
except ImportError:
26-
ZLIB_SUPPORT = False
2723

2824

2925
class BuildDumbTestCase(support.TempdirManager,
@@ -42,7 +38,6 @@ def tearDown(self):
4238
sys.argv[:] = self.old_sys_argv[1]
4339
super(BuildDumbTestCase, self).tearDown()
4440

45-
@unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
4641
def test_simple_built(self):
4742

4843
# let's create a simple package

Lib/distutils/tests/test_bdist_rpm.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import unittest
44
import sys
55
import os
6-
from test.support import run_unittest, requires_zlib
6+
from test.support import run_unittest
77

88
from distutils.core import Distribution
99
from distutils.command.bdist_rpm import bdist_rpm
@@ -44,7 +44,6 @@ def tearDown(self):
4444
# spurious sdtout/stderr output under Mac OS X
4545
@unittest.skipUnless(sys.platform.startswith('linux'),
4646
'spurious sdtout/stderr output under Mac OS X')
47-
@requires_zlib()
4847
@unittest.skipIf(find_executable('rpm') is None,
4948
'the rpm command is not found')
5049
@unittest.skipIf(find_executable('rpmbuild') is None,
@@ -87,7 +86,6 @@ def test_quiet(self):
8786
# spurious sdtout/stderr output under Mac OS X
8887
@unittest.skipUnless(sys.platform.startswith('linux'),
8988
'spurious sdtout/stderr output under Mac OS X')
90-
@requires_zlib()
9189
# http://bugs.python.org/issue1533164
9290
@unittest.skipIf(find_executable('rpm') is None,
9391
'the rpm command is not found')

Lib/distutils/tests/test_sdist.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@
44
import unittest
55
import warnings
66
import zipfile
7+
import zlib
78
from os.path import join
89
from textwrap import dedent
910
from test.support import captured_stdout, run_unittest
1011
from test.support.warnings_helper import check_warnings
1112

12-
try:
13-
import zlib
14-
ZLIB_SUPPORT = True
15-
except ImportError:
16-
ZLIB_SUPPORT = False
17-
1813
try:
1914
import grp
2015
import pwd
@@ -88,7 +83,6 @@ def get_cmd(self, metadata=None):
8883
cmd.dist_dir = 'dist'
8984
return dist, cmd
9085

91-
@unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
9286
def test_prune_file_list(self):
9387
# this test creates a project with some VCS dirs and an NFS rename
9488
# file, then launches sdist to check they get pruned on all systems
@@ -133,7 +127,6 @@ def test_prune_file_list(self):
133127
'somecode/', 'somecode/__init__.py']
134128
self.assertEqual(sorted(content), ['fake-1.0/' + x for x in expected])
135129

136-
@unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
137130
@unittest.skipIf(find_executable('tar') is None,
138131
"The tar command is not found")
139132
@unittest.skipIf(find_executable('gzip') is None,
@@ -166,7 +159,6 @@ def test_make_distribution(self):
166159
result.sort()
167160
self.assertEqual(result, ['fake-1.0.tar', 'fake-1.0.tar.gz'])
168161

169-
@unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
170162
def test_add_defaults(self):
171163

172164
# http://bugs.python.org/issue2279
@@ -245,7 +237,6 @@ def test_add_defaults(self):
245237
f.close()
246238
self.assertEqual(manifest, MANIFEST % {'sep': os.sep})
247239

248-
@unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
249240
def test_metadata_check_option(self):
250241
# testing the `medata-check` option
251242
dist, cmd = self.get_cmd(metadata={})
@@ -332,7 +323,6 @@ def test_invalid_template_wrong_path(self):
332323
# this used to crash instead of raising a warning: #8286
333324
self._check_template('include examples/')
334325

335-
@unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
336326
def test_get_file_list(self):
337327
# make sure MANIFEST is recalculated
338328
dist, cmd = self.get_cmd()
@@ -374,7 +364,6 @@ def test_get_file_list(self):
374364
self.assertEqual(len(manifest2), 6)
375365
self.assertIn('doc2.txt', manifest2[-1])
376366

377-
@unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
378367
def test_manifest_marker(self):
379368
# check that autogenerated MANIFESTs have a marker
380369
dist, cmd = self.get_cmd()
@@ -391,7 +380,6 @@ def test_manifest_marker(self):
391380
self.assertEqual(manifest[0],
392381
'# file GENERATED by distutils, do NOT edit')
393382

394-
@unittest.skipUnless(ZLIB_SUPPORT, "Need zlib support to run")
395383
def test_manifest_comments(self):
396384
# make sure comments don't cause exceptions or wrong includes
397385
contents = dedent("""\
@@ -408,7 +396,6 @@ def test_manifest_comments(self):
408396
cmd.run()
409397
self.assertEqual(cmd.filelist.files, ['good.py'])
410398

411-
@unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
412399
def test_manual_manifest(self):
413400
# check that a MANIFEST without a marker is left alone
414401
dist, cmd = self.get_cmd()
@@ -438,7 +425,6 @@ def test_manual_manifest(self):
438425
self.assertEqual(sorted(filenames), ['fake-1.0', 'fake-1.0/PKG-INFO',
439426
'fake-1.0/README.manual'])
440427

441-
@unittest.skipUnless(ZLIB_SUPPORT, "requires zlib")
442428
@unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support")
443429
@unittest.skipIf(find_executable('tar') is None,
444430
"The tar command is not found")

Lib/encodings/zlib_codec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"""
77

88
import codecs
9-
import zlib # this codec needs the optional zlib module !
9+
import zlib
1010

1111
### Codec APIs
1212

Lib/shutil.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,7 @@
1010
import fnmatch
1111
import collections
1212
import errno
13-
14-
try:
15-
import zlib
16-
del zlib
17-
_ZLIB_SUPPORTED = True
18-
except ImportError:
19-
_ZLIB_SUPPORTED = False
13+
import zlib
2014

2115
try:
2216
import bz2
@@ -911,7 +905,7 @@ def _make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,
911905
"""
912906
if compress is None:
913907
tar_compression = ''
914-
elif _ZLIB_SUPPORTED and compress == 'gzip':
908+
elif compress == 'gzip':
915909
tar_compression = 'gz'
916910
elif _BZ2_SUPPORTED and compress == 'bzip2':
917911
tar_compression = 'bz2'
@@ -1006,10 +1000,9 @@ def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None):
10061000
'tar': (_make_tarball, [('compress', None)], "uncompressed tar file"),
10071001
}
10081002

1009-
if _ZLIB_SUPPORTED:
1010-
_ARCHIVE_FORMATS['gztar'] = (_make_tarball, [('compress', 'gzip')],
1011-
"gzip'ed tar-file")
1012-
_ARCHIVE_FORMATS['zip'] = (_make_zipfile, [], "ZIP file")
1003+
_ARCHIVE_FORMATS['gztar'] = (_make_tarball, [('compress', 'gzip')],
1004+
"gzip'ed tar-file")
1005+
_ARCHIVE_FORMATS['zip'] = (_make_zipfile, [], "ZIP file")
10131006

10141007
if _BZ2_SUPPORTED:
10151008
_ARCHIVE_FORMATS['bztar'] = (_make_tarball, [('compress', 'bzip2')],
@@ -1217,12 +1210,9 @@ def _unpack_tarfile(filename, extract_dir):
12171210
_UNPACK_FORMATS = {
12181211
'tar': (['.tar'], _unpack_tarfile, [], "uncompressed tar file"),
12191212
'zip': (['.zip'], _unpack_zipfile, [], "ZIP file"),
1213+
'gztar': (['.tar.gz', '.tgz'], _unpack_tarfile, [], "gzip'ed tar-file"),
12201214
}
12211215

1222-
if _ZLIB_SUPPORTED:
1223-
_UNPACK_FORMATS['gztar'] = (['.tar.gz', '.tgz'], _unpack_tarfile, [],
1224-
"gzip'ed tar-file")
1225-
12261216
if _BZ2_SUPPORTED:
12271217
_UNPACK_FORMATS['bztar'] = (['.tar.bz2', '.tbz2'], _unpack_tarfile, [],
12281218
"bzip2'ed tar-file")

Lib/tarfile.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,7 @@ def __init__(self, name, mode, comptype, fileobj, bufsize):
361361

362362
try:
363363
if comptype == "gz":
364-
try:
365-
import zlib
366-
except ImportError:
367-
raise CompressionError("zlib module is not available") from None
364+
import zlib
368365
self.zlib = zlib
369366
self.crc = zlib.crc32(b"")
370367
if mode == "r":
@@ -2359,13 +2356,10 @@ def next(self):
23592356
except SubsequentHeaderError as e:
23602357
raise ReadError(str(e)) from None
23612358
except Exception as e:
2362-
try:
2363-
import zlib
2364-
if isinstance(e, zlib.error):
2365-
raise ReadError(f'zlib error: {e}') from None
2366-
else:
2367-
raise e
2368-
except ImportError:
2359+
import zlib
2360+
if isinstance(e, zlib.error):
2361+
raise ReadError(f'zlib error: {e}') from None
2362+
else:
23692363
raise e
23702364
break
23712365

Lib/test/pythoninfo.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""
22
Collect various information about Python to help debugging test failures.
33
"""
4-
from __future__ import print_function
54
import errno
65
import re
76
import sys
@@ -558,11 +557,7 @@ def collect_sqlite(info_add):
558557

559558

560559
def collect_zlib(info_add):
561-
try:
562-
import zlib
563-
except ImportError:
564-
return
565-
560+
import zlib
566561
attributes = ('ZLIB_VERSION', 'ZLIB_RUNTIME_VERSION')
567562
copy_attributes(info_add, zlib, 'zlib.%s', attributes)
568563

Lib/test/test_codecs.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2700,13 +2700,10 @@ def test_seek0(self):
27002700
"rot_13": ["rot13"],
27012701
}
27022702

2703-
try:
2704-
import zlib
2705-
except ImportError:
2706-
zlib = None
2707-
else:
2708-
bytes_transform_encodings.append("zlib_codec")
2709-
transform_aliases["zlib_codec"] = ["zip", "zlib"]
2703+
import zlib
2704+
bytes_transform_encodings.append("zlib_codec")
2705+
transform_aliases["zlib_codec"] = ["zip", "zlib"]
2706+
27102707
try:
27112708
import bz2
27122709
except ImportError:
@@ -2807,7 +2804,6 @@ def test_binary_to_text_denylists_text_transforms(self):
28072804
bad_input.decode("rot_13")
28082805
self.assertIsNone(failure.exception.__cause__)
28092806

2810-
@unittest.skipUnless(zlib, "Requires zlib support")
28112807
def test_custom_zlib_error_is_wrapped(self):
28122808
# Check zlib codec gives a good error for malformed input
28132809
msg = "^decoding with 'zlib_codec' codec failed"

0 commit comments

Comments
 (0)