[Python-checkins] cpython (merge 3.2 -> 3.2): Merge

antoine.pitrou python-checkins at python.org
Tue Mar 15 21:17:35 CET 2011


http://hg.python.org/cpython/rev/b312aff15312
changeset: 68549:b312aff15312
branch: 3.2
parent: 68542:ccea53bd4f88
parent: 68545:1bf4383f190a
user: Antoine Pitrou <solipsis at pitrou.net>
date: Tue Mar 15 21:11:27 2011 +0100
summary:
 Merge
files:
 Misc/NEWS
diff --git a/Lib/distutils/archive_util.py b/Lib/distutils/archive_util.py
--- a/Lib/distutils/archive_util.py
+++ b/Lib/distutils/archive_util.py
@@ -9,6 +9,12 @@
 from warnings import warn
 import sys
 
+try:
+ import zipfile
+except ImportError:
+ zipfile = None
+
+
 from distutils.errors import DistutilsExecError
 from distutils.spawn import spawn
 from distutils.dir_util import mkpath
@@ -74,11 +80,6 @@
 available, raises DistutilsExecError. Returns the name of the output zip
 file.
 """
- try:
- import zipfile
- except ImportError:
- zipfile = None
-
 zip_filename = base_name + ".zip"
 mkpath(os.path.dirname(zip_filename), dry_run=dry_run)
 
@@ -105,8 +106,12 @@
 zip_filename, base_dir)
 
 if not dry_run:
- zip = zipfile.ZipFile(zip_filename, "w",
- compression=zipfile.ZIP_DEFLATED)
+ try:
+ zip = zipfile.ZipFile(zip_filename, "w",
+ compression=zipfile.ZIP_DEFLATED)
+ except RuntimeError:
+ zip = zipfile.ZipFile(zip_filename, "w",
+ compression=zipfile.ZIP_STORED)
 
 for dirpath, dirnames, filenames in os.walk(base_dir):
 for name in filenames:
diff --git a/Lib/distutils/tests/test_archive_util.py b/Lib/distutils/tests/test_archive_util.py
--- a/Lib/distutils/tests/test_archive_util.py
+++ b/Lib/distutils/tests/test_archive_util.py
@@ -7,12 +7,13 @@
 from os.path import splitdrive
 import warnings
 
+from distutils import archive_util
 from distutils.archive_util import (check_archive_formats, make_tarball,
 make_zipfile, make_archive,
 ARCHIVE_FORMATS)
 from distutils.spawn import find_executable, spawn
 from distutils.tests import support
-from test.support import check_warnings, run_unittest
+from test.support import check_warnings, run_unittest, patch
 
 try:
 import zipfile
@@ -20,10 +21,18 @@
 except ImportError:
 ZIP_SUPPORT = find_executable('zip')
 
+try:
+ import zlib
+ ZLIB_SUPPORT = True
+except ImportError:
+ ZLIB_SUPPORT = False
+
+
 class ArchiveUtilTestCase(support.TempdirManager,
 support.LoggingSilencer,
 unittest.TestCase):
 
+ @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
 def test_make_tarball(self):
 # creating something to tar
 tmpdir = self.mkdtemp()
@@ -84,8 +93,9 @@
 base_name = os.path.join(tmpdir2, 'archive')
 return tmpdir, tmpdir2, base_name
 
- @unittest.skipUnless(find_executable('tar') and find_executable('gzip'),
- 'Need the tar command to run')
+ @unittest.skipUnless(find_executable('tar') and find_executable('gzip')
+ and ZLIB_SUPPORT,
+ 'Need the tar, gzip and zlib command to run')
 def test_tarfile_vs_tar(self):
 tmpdir, tmpdir2, base_name = self._create_files()
 old_dir = os.getcwd()
@@ -169,7 +179,8 @@
 self.assertTrue(not os.path.exists(tarball))
 self.assertEqual(len(w.warnings), 1)
 
- @unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
+ @unittest.skipUnless(ZIP_SUPPORT and ZLIB_SUPPORT,
+ 'Need zip and zlib support to run')
 def test_make_zipfile(self):
 # creating something to tar
 tmpdir = self.mkdtemp()
@@ -182,6 +193,29 @@
 
 # check if the compressed tarball was created
 tarball = base_name + '.zip'
+ self.assertTrue(os.path.exists(tarball))
+
+ @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, tmpdir2, base_name = self._create_files()
+ make_zipfile(base_name, tmpdir)
+
+ tarball = base_name + '.zip'
+ self.assertEqual(called,
+ [((tarball, "w"), {'compression': zipfile.ZIP_STORED})])
+ self.assertTrue(os.path.exists(tarball))
 
 def test_check_archive_formats(self):
 self.assertEqual(check_archive_formats(['gztar', 'xxx', 'zip']),
diff --git a/Lib/distutils/tests/test_bdist_dumb.py b/Lib/distutils/tests/test_bdist_dumb.py
--- a/Lib/distutils/tests/test_bdist_dumb.py
+++ b/Lib/distutils/tests/test_bdist_dumb.py
@@ -18,6 +18,13 @@
 
 """
 
+try:
+ import zlib
+ ZLIB_SUPPORT = True
+except ImportError:
+ ZLIB_SUPPORT = False
+
+
 class BuildDumbTestCase(support.TempdirManager,
 support.LoggingSilencer,
 support.EnvironGuard,
@@ -34,6 +41,7 @@
 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
diff --git a/Lib/distutils/tests/test_sdist.py b/Lib/distutils/tests/test_sdist.py
--- a/Lib/distutils/tests/test_sdist.py
+++ b/Lib/distutils/tests/test_sdist.py
@@ -40,6 +40,13 @@
 somecode%(sep)sdoc.txt
 """
 
+try:
+ import zlib
+ ZLIB_SUPPORT = True
+except ImportError:
+ ZLIB_SUPPORT = False
+
+
 class SDistTestCase(PyPIRCCommandTestCase):
 
 def setUp(self):
@@ -78,6 +85,7 @@
 cmd.warn = _warn
 return dist, cmd
 
+ @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
 def test_prune_file_list(self):
 # this test creates a package with some vcs dirs in it
 # and launch sdist to make sure they get pruned
@@ -119,6 +127,7 @@
 # making sure everything has been pruned correctly
 self.assertEqual(len(content), 4)
 
+ @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
 def test_make_distribution(self):
 
 # check if tar and gzip are installed
@@ -153,6 +162,7 @@
 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
@@ -218,6 +228,7 @@
 finally:
 f.close()
 
+ @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={})
@@ -277,7 +288,7 @@
 cmd.formats = 'supazipa'
 self.assertRaises(DistutilsOptionError, cmd.finalize_options)
 
-
+ @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()
@@ -318,6 +329,7 @@
 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()
@@ -334,6 +346,7 @@
 self.assertEqual(manifest[0],
 '# file GENERATED by distutils, do NOT edit')
 
+ @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()
diff --git a/Lib/test/support.py b/Lib/test/support.py
--- a/Lib/test/support.py
+++ b/Lib/test/support.py
@@ -1467,3 +1467,36 @@
 ok = can_symlink()
 msg = "Requires functional symlink implementation"
 return test if ok else unittest.skip(msg)(test)
+
+def patch(test_instance, object_to_patch, attr_name, new_value):
+ """Override 'object_to_patch'.'attr_name' with 'new_value'.
+
+ Also, add a cleanup procedure to 'test_instance' to restore
+ 'object_to_patch' value for 'attr_name'.
+ The 'attr_name' should be a valid attribute for 'object_to_patch'.
+
+ """
+ # check that 'attr_name' is a real attribute for 'object_to_patch'
+ # will raise AttributeError if it does not exist
+ getattr(object_to_patch, attr_name)
+
+ # keep a copy of the old value
+ attr_is_local = False
+ try:
+ old_value = object_to_patch.__dict__[attr_name]
+ except (AttributeError, KeyError):
+ old_value = getattr(object_to_patch, attr_name, None)
+ else:
+ attr_is_local = True
+
+ # restore the value when the test is done
+ def cleanup():
+ if attr_is_local:
+ setattr(object_to_patch, attr_name, old_value)
+ else:
+ delattr(object_to_patch, attr_name)
+
+ test_instance.addCleanup(cleanup)
+
+ # actually override the attribute
+ setattr(object_to_patch, attr_name, new_value)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -38,10 +38,14 @@
 Library
 -------
 
+- Issue #11501: disutils.archive_utils.make_zipfile no longer fails if zlib is
+ not installed. Instead, the zipfile.ZIP_STORED compression is used to create
+ the ZipFile. Patch by Natalia B. Bidart.
+
 - Issue #11554: Fixed support for Japanese codecs; previously the body output
 encoding was not done if euc-jp or shift-jis was specified as the charset.
 
-- Issue #11500: Fixed a bug in the os x proxy bypass code for fully qualified 
+- Issue #11500: Fixed a bug in the os x proxy bypass code for fully qualified
 IP addresses in the proxy exception list. 
 
 - Issue #11491: dbm.error is no longer raised when dbm.open is called with
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /