Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 4 additions & 40 deletions Lib/distutils/tests/test_archive_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@
except ImportError:
ZIP_SUPPORT = find_executable('zip')

try:
import zlib
ZLIB_SUPPORT = True
except ImportError:
ZLIB_SUPPORT = False
import zlib

try:
import bz2
Expand Down Expand Up @@ -63,15 +59,13 @@ class ArchiveUtilTestCase(support.TempdirManager,
support.LoggingSilencer,
unittest.TestCase):

@unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
def test_make_tarball(self, name='archive'):
# creating something to tar
tmpdir = self._create_files()
self._make_tarball(tmpdir, name, '.tar.gz')
# trying an uncompressed one
self._make_tarball(tmpdir, name, '.tar', compress=None)

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

@unittest.skipUnless(find_executable('tar') and find_executable('gzip')
and ZLIB_SUPPORT,
'Need the tar, gzip and zlib command to run')
@unittest.skipUnless(find_executable('tar') and find_executable('gzip'),
'Need the tar and gzip commands to run')
def test_tarfile_vs_tar(self):
tmpdir = self._create_files()
tmpdir2 = self.mkdtemp()
Expand Down Expand Up @@ -234,8 +227,7 @@ def test_compress_deprecated(self):
self.assertFalse(os.path.exists(tarball))
self.assertEqual(len(w.warnings), 1)

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

@unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
def test_make_zipfile_no_zlib(self):
patch(self, archive_util.zipfile, 'zlib', None) # force zlib ImportError

called = []
zipfile_class = zipfile.ZipFile
def fake_zipfile(*a, **kw):
if kw.get('compression', None) == zipfile.ZIP_STORED:
called.append((a, kw))
return zipfile_class(*a, **kw)

patch(self, archive_util.zipfile, 'ZipFile', fake_zipfile)

# create something to tar and compress
tmpdir = self._create_files()
base_name = os.path.join(self.mkdtemp(), 'archive')
with change_cwd(tmpdir):
make_zipfile(base_name, 'dist')

tarball = base_name + '.zip'
self.assertEqual(called,
[((tarball, "w"), {'compression': zipfile.ZIP_STORED})])
self.assertTrue(os.path.exists(tarball))
with zipfile.ZipFile(tarball) as zf:
self.assertEqual(sorted(zf.namelist()), self._zip_created_files)

def test_check_archive_formats(self):
self.assertEqual(check_archive_formats(['gztar', 'xxx', 'zip']),
'xxx')
Expand Down Expand Up @@ -308,7 +274,6 @@ def test_make_archive_tar(self):
self.assertEqual(os.path.basename(res), 'archive.tar')
self.assertEqual(self._tarinfo(res), self._created_files)

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

@unittest.skipUnless(ZLIB_SUPPORT, "Requires zlib")
@unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support")
def test_tarfile_root_owner(self):
tmpdir = self._create_files()
Expand Down
9 changes: 2 additions & 7 deletions Lib/distutils/tests/test_bdist_dumb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import os
import sys
import zipfile
import unittest
import zipfile
import zlib
from test.support import run_unittest

from distutils.core import Distribution
Expand All @@ -19,11 +20,6 @@

"""

try:
import zlib
ZLIB_SUPPORT = True
except ImportError:
ZLIB_SUPPORT = False


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

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

# let's create a simple package
Expand Down
4 changes: 1 addition & 3 deletions Lib/distutils/tests/test_bdist_rpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import unittest
import sys
import os
from test.support import run_unittest, requires_zlib
from test.support import run_unittest

from distutils.core import Distribution
from distutils.command.bdist_rpm import bdist_rpm
Expand Down Expand Up @@ -44,7 +44,6 @@ def tearDown(self):
# spurious sdtout/stderr output under Mac OS X
@unittest.skipUnless(sys.platform.startswith('linux'),
'spurious sdtout/stderr output under Mac OS X')
@requires_zlib()
@unittest.skipIf(find_executable('rpm') is None,
'the rpm command is not found')
@unittest.skipIf(find_executable('rpmbuild') is None,
Expand Down Expand Up @@ -87,7 +86,6 @@ def test_quiet(self):
# spurious sdtout/stderr output under Mac OS X
@unittest.skipUnless(sys.platform.startswith('linux'),
'spurious sdtout/stderr output under Mac OS X')
@requires_zlib()
# http://bugs.python.org/issue1533164
@unittest.skipIf(find_executable('rpm') is None,
'the rpm command is not found')
Expand Down
16 changes: 1 addition & 15 deletions Lib/distutils/tests/test_sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@
import unittest
import warnings
import zipfile
import zlib
from os.path import join
from textwrap import dedent
from test.support import captured_stdout, run_unittest
from test.support.warnings_helper import check_warnings

try:
import zlib
ZLIB_SUPPORT = True
except ImportError:
ZLIB_SUPPORT = False

try:
import grp
import pwd
Expand Down Expand Up @@ -88,7 +83,6 @@ def get_cmd(self, metadata=None):
cmd.dist_dir = 'dist'
return dist, cmd

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

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

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

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

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

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

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

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

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

@unittest.skipUnless(ZLIB_SUPPORT, "requires zlib")
@unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support")
@unittest.skipIf(find_executable('tar') is None,
"The tar command is not found")
Expand Down
2 changes: 1 addition & 1 deletion Lib/encodings/zlib_codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""

import codecs
import zlib # this codec needs the optional zlib module !
import zlib

### Codec APIs

Expand Down
22 changes: 6 additions & 16 deletions Lib/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@
import fnmatch
import collections
import errno

try:
import zlib
del zlib
_ZLIB_SUPPORTED = True
except ImportError:
_ZLIB_SUPPORTED = False
import zlib

try:
import bz2
Expand Down Expand Up @@ -911,7 +905,7 @@ def _make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,
"""
if compress is None:
tar_compression = ''
elif _ZLIB_SUPPORTED and compress == 'gzip':
elif compress == 'gzip':
tar_compression = 'gz'
elif _BZ2_SUPPORTED and compress == 'bzip2':
tar_compression = 'bz2'
Expand Down Expand Up @@ -1006,10 +1000,9 @@ def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None):
'tar': (_make_tarball, [('compress', None)], "uncompressed tar file"),
}

if _ZLIB_SUPPORTED:
_ARCHIVE_FORMATS['gztar'] = (_make_tarball, [('compress', 'gzip')],
"gzip'ed tar-file")
_ARCHIVE_FORMATS['zip'] = (_make_zipfile, [], "ZIP file")
_ARCHIVE_FORMATS['gztar'] = (_make_tarball, [('compress', 'gzip')],
"gzip'ed tar-file")
_ARCHIVE_FORMATS['zip'] = (_make_zipfile, [], "ZIP file")

if _BZ2_SUPPORTED:
_ARCHIVE_FORMATS['bztar'] = (_make_tarball, [('compress', 'bzip2')],
Expand Down Expand Up @@ -1217,12 +1210,9 @@ def _unpack_tarfile(filename, extract_dir):
_UNPACK_FORMATS = {
'tar': (['.tar'], _unpack_tarfile, [], "uncompressed tar file"),
'zip': (['.zip'], _unpack_zipfile, [], "ZIP file"),
'gztar': (['.tar.gz', '.tgz'], _unpack_tarfile, [], "gzip'ed tar-file"),
}

if _ZLIB_SUPPORTED:
_UNPACK_FORMATS['gztar'] = (['.tar.gz', '.tgz'], _unpack_tarfile, [],
"gzip'ed tar-file")

if _BZ2_SUPPORTED:
_UNPACK_FORMATS['bztar'] = (['.tar.bz2', '.tbz2'], _unpack_tarfile, [],
"bzip2'ed tar-file")
Expand Down
16 changes: 5 additions & 11 deletions Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,7 @@ def __init__(self, name, mode, comptype, fileobj, bufsize):

try:
if comptype == "gz":
try:
import zlib
except ImportError:
raise CompressionError("zlib module is not available") from None
import zlib
self.zlib = zlib
self.crc = zlib.crc32(b"")
if mode == "r":
Expand Down Expand Up @@ -2359,13 +2356,10 @@ def next(self):
except SubsequentHeaderError as e:
raise ReadError(str(e)) from None
except Exception as e:
try:
import zlib
if isinstance(e, zlib.error):
raise ReadError(f'zlib error: {e}') from None
else:
raise e
except ImportError:
import zlib
if isinstance(e, zlib.error):
raise ReadError(f'zlib error: {e}') from None
else:
raise e
break

Expand Down
7 changes: 1 addition & 6 deletions Lib/test/pythoninfo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
Collect various information about Python to help debugging test failures.
"""
from __future__ import print_function
import errno
import re
import sys
Expand Down Expand Up @@ -558,11 +557,7 @@ def collect_sqlite(info_add):


def collect_zlib(info_add):
try:
import zlib
except ImportError:
return

import zlib
attributes = ('ZLIB_VERSION', 'ZLIB_RUNTIME_VERSION')
copy_attributes(info_add, zlib, 'zlib.%s', attributes)

Expand Down
12 changes: 4 additions & 8 deletions Lib/test/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2700,13 +2700,10 @@ def test_seek0(self):
"rot_13": ["rot13"],
}

try:
import zlib
except ImportError:
zlib = None
else:
bytes_transform_encodings.append("zlib_codec")
transform_aliases["zlib_codec"] = ["zip", "zlib"]
import zlib
bytes_transform_encodings.append("zlib_codec")
transform_aliases["zlib_codec"] = ["zip", "zlib"]

try:
import bz2
except ImportError:
Expand Down Expand Up @@ -2807,7 +2804,6 @@ def test_binary_to_text_denylists_text_transforms(self):
bad_input.decode("rot_13")
self.assertIsNone(failure.exception.__cause__)

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