Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f134dce

Browse files
committed
Merge
2 parents 709ac7b + e870623 commit f134dce

6 files changed

Lines changed: 110 additions & 13 deletions

File tree

Lib/distutils/archive_util.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
from warnings import warn
1010
import sys
1111

12+
try:
13+
import zipfile
14+
except ImportError:
15+
zipfile = None
16+
17+
1218
from distutils.errors import DistutilsExecError
1319
from distutils.spawn import spawn
1420
from distutils.dir_util import mkpath
@@ -74,11 +80,6 @@ def make_zipfile(base_name, base_dir, verbose=0, dry_run=0):
7480
available, raises DistutilsExecError. Returns the name of the output zip
7581
file.
7682
"""
77-
try:
78-
import zipfile
79-
except ImportError:
80-
zipfile = None
81-
8283
zip_filename = base_name + ".zip"
8384
mkpath(os.path.dirname(zip_filename), dry_run=dry_run)
8485

@@ -105,8 +106,12 @@ def make_zipfile(base_name, base_dir, verbose=0, dry_run=0):
105106
zip_filename, base_dir)
106107

107108
if not dry_run:
108-
zip = zipfile.ZipFile(zip_filename, "w",
109-
compression=zipfile.ZIP_DEFLATED)
109+
try:
110+
zip = zipfile.ZipFile(zip_filename, "w",
111+
compression=zipfile.ZIP_DEFLATED)
112+
except RuntimeError:
113+
zip = zipfile.ZipFile(zip_filename, "w",
114+
compression=zipfile.ZIP_STORED)
110115

111116
for dirpath, dirnames, filenames in os.walk(base_dir):
112117
for name in filenames:

Lib/distutils/tests/test_archive_util.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,32 @@
77
from os.path import splitdrive
88
import warnings
99

10+
from distutils import archive_util
1011
from distutils.archive_util import (check_archive_formats, make_tarball,
1112
make_zipfile, make_archive,
1213
ARCHIVE_FORMATS)
1314
from distutils.spawn import find_executable, spawn
1415
from distutils.tests import support
15-
from test.support import check_warnings, run_unittest
16+
from test.support import check_warnings, run_unittest, patch
1617

1718
try:
1819
import zipfile
1920
ZIP_SUPPORT = True
2021
except ImportError:
2122
ZIP_SUPPORT = find_executable('zip')
2223

24+
try:
25+
import zlib
26+
ZLIB_SUPPORT = True
27+
except ImportError:
28+
ZLIB_SUPPORT = False
29+
30+
2331
class ArchiveUtilTestCase(support.TempdirManager,
2432
support.LoggingSilencer,
2533
unittest.TestCase):
2634

35+
@unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
2736
def test_make_tarball(self):
2837
# creating something to tar
2938
tmpdir = self.mkdtemp()
@@ -84,8 +93,9 @@ def _create_files(self):
8493
base_name = os.path.join(tmpdir2, 'archive')
8594
return tmpdir, tmpdir2, base_name
8695

87-
@unittest.skipUnless(find_executable('tar') and find_executable('gzip'),
88-
'Need the tar command to run')
96+
@unittest.skipUnless(find_executable('tar') and find_executable('gzip')
97+
and ZLIB_SUPPORT,
98+
'Need the tar, gzip and zlib command to run')
8999
def test_tarfile_vs_tar(self):
90100
tmpdir, tmpdir2, base_name = self._create_files()
91101
old_dir = os.getcwd()
@@ -169,7 +179,8 @@ def test_compress_deprecated(self):
169179
self.assertTrue(not os.path.exists(tarball))
170180
self.assertEqual(len(w.warnings), 1)
171181

172-
@unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
182+
@unittest.skipUnless(ZIP_SUPPORT and ZLIB_SUPPORT,
183+
'Need zip and zlib support to run')
173184
def test_make_zipfile(self):
174185
# creating something to tar
175186
tmpdir = self.mkdtemp()
@@ -182,6 +193,29 @@ def test_make_zipfile(self):
182193

183194
# check if the compressed tarball was created
184195
tarball = base_name + '.zip'
196+
self.assertTrue(os.path.exists(tarball))
197+
198+
@unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
199+
def test_make_zipfile_no_zlib(self):
200+
patch(self, archive_util.zipfile, 'zlib', None) # force zlib ImportError
201+
202+
called = []
203+
zipfile_class = zipfile.ZipFile
204+
def fake_zipfile(*a, **kw):
205+
if kw.get('compression', None) == zipfile.ZIP_STORED:
206+
called.append((a, kw))
207+
return zipfile_class(*a, **kw)
208+
209+
patch(self, archive_util.zipfile, 'ZipFile', fake_zipfile)
210+
211+
# create something to tar and compress
212+
tmpdir, tmpdir2, base_name = self._create_files()
213+
make_zipfile(base_name, tmpdir)
214+
215+
tarball = base_name + '.zip'
216+
self.assertEqual(called,
217+
[((tarball, "w"), {'compression': zipfile.ZIP_STORED})])
218+
self.assertTrue(os.path.exists(tarball))
185219

186220
def test_check_archive_formats(self):
187221
self.assertEqual(check_archive_formats(['gztar', 'xxx', 'zip']),

Lib/distutils/tests/test_bdist_dumb.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
1919
"""
2020

21+
try:
22+
import zlib
23+
ZLIB_SUPPORT = True
24+
except ImportError:
25+
ZLIB_SUPPORT = False
26+
27+
2128
class BuildDumbTestCase(support.TempdirManager,
2229
support.LoggingSilencer,
2330
support.EnvironGuard,
@@ -34,6 +41,7 @@ def tearDown(self):
3441
sys.argv[:] = self.old_sys_argv[1]
3542
super(BuildDumbTestCase, self).tearDown()
3643

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

3947
# let's create a simple package

Lib/distutils/tests/test_sdist.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@
4040
somecode%(sep)sdoc.txt
4141
"""
4242

43+
try:
44+
import zlib
45+
ZLIB_SUPPORT = True
46+
except ImportError:
47+
ZLIB_SUPPORT = False
48+
49+
4350
class SDistTestCase(PyPIRCCommandTestCase):
4451

4552
def setUp(self):
@@ -78,6 +85,7 @@ def _warn(*args):
7885
cmd.warn = _warn
7986
return dist, cmd
8087

88+
@unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
8189
def test_prune_file_list(self):
8290
# this test creates a package with some vcs dirs in it
8391
# and launch sdist to make sure they get pruned
@@ -119,6 +127,7 @@ def test_prune_file_list(self):
119127
# making sure everything has been pruned correctly
120128
self.assertEqual(len(content), 4)
121129

130+
@unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
122131
def test_make_distribution(self):
123132

124133
# check if tar and gzip are installed
@@ -153,6 +162,7 @@ def test_make_distribution(self):
153162
result.sort()
154163
self.assertEqual(result, ['fake-1.0.tar', 'fake-1.0.tar.gz'])
155164

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

158168
# http://bugs.python.org/issue2279
@@ -218,6 +228,7 @@ def test_add_defaults(self):
218228
finally:
219229
f.close()
220230

231+
@unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
221232
def test_metadata_check_option(self):
222233
# testing the `medata-check` option
223234
dist, cmd = self.get_cmd(metadata={})
@@ -277,7 +288,7 @@ def test_finalize_options(self):
277288
cmd.formats = 'supazipa'
278289
self.assertRaises(DistutilsOptionError, cmd.finalize_options)
279290

280-
291+
@unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
281292
def test_get_file_list(self):
282293
# make sure MANIFEST is recalculated
283294
dist, cmd = self.get_cmd()
@@ -318,6 +329,7 @@ def test_get_file_list(self):
318329
self.assertEqual(len(manifest2), 6)
319330
self.assertIn('doc2.txt', manifest2[-1])
320331

332+
@unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
321333
def test_manifest_marker(self):
322334
# check that autogenerated MANIFESTs have a marker
323335
dist, cmd = self.get_cmd()
@@ -334,6 +346,7 @@ def test_manifest_marker(self):
334346
self.assertEqual(manifest[0],
335347
'# file GENERATED by distutils, do NOT edit')
336348

349+
@unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
337350
def test_manual_manifest(self):
338351
# check that a MANIFEST without a marker is left alone
339352
dist, cmd = self.get_cmd()

Lib/test/support.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,3 +1467,36 @@ def skip_unless_symlink(test):
14671467
ok = can_symlink()
14681468
msg = "Requires functional symlink implementation"
14691469
return test if ok else unittest.skip(msg)(test)
1470+
1471+
def patch(test_instance, object_to_patch, attr_name, new_value):
1472+
"""Override 'object_to_patch'.'attr_name' with 'new_value'.
1473+
1474+
Also, add a cleanup procedure to 'test_instance' to restore
1475+
'object_to_patch' value for 'attr_name'.
1476+
The 'attr_name' should be a valid attribute for 'object_to_patch'.
1477+
1478+
"""
1479+
# check that 'attr_name' is a real attribute for 'object_to_patch'
1480+
# will raise AttributeError if it does not exist
1481+
getattr(object_to_patch, attr_name)
1482+
1483+
# keep a copy of the old value
1484+
attr_is_local = False
1485+
try:
1486+
old_value = object_to_patch.__dict__[attr_name]
1487+
except (AttributeError, KeyError):
1488+
old_value = getattr(object_to_patch, attr_name, None)
1489+
else:
1490+
attr_is_local = True
1491+
1492+
# restore the value when the test is done
1493+
def cleanup():
1494+
if attr_is_local:
1495+
setattr(object_to_patch, attr_name, old_value)
1496+
else:
1497+
delattr(object_to_patch, attr_name)
1498+
1499+
test_instance.addCleanup(cleanup)
1500+
1501+
# actually override the attribute
1502+
setattr(object_to_patch, attr_name, new_value)

Misc/NEWS

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,14 @@ Core and Builtins
3838
Library
3939
-------
4040

41+
- Issue #11501: disutils.archive_utils.make_zipfile no longer fails if zlib is
42+
not installed. Instead, the zipfile.ZIP_STORED compression is used to create
43+
the ZipFile. Patch by Natalia B. Bidart.
44+
4145
- Issue #11554: Fixed support for Japanese codecs; previously the body output
4246
encoding was not done if euc-jp or shift-jis was specified as the charset.
4347

44-
- Issue #11500: Fixed a bug in the os x proxy bypass code for fully qualified
48+
- Issue #11500: Fixed a bug in the os x proxy bypass code for fully qualified
4549
IP addresses in the proxy exception list.
4650

4751
- Issue #11491: dbm.error is no longer raised when dbm.open is called with

0 commit comments

Comments
 (0)