]> granicus.if.org Git - python/commitdiff
#7066 - Fixed distutils.archive_util.make_archive behavior so it restores the cwd
authorTarek Ziadé <ziade.tarek@gmail.com>
Sat, 24 Oct 2009 13:29:44 +0000 (13:29 +0000)
committerTarek Ziadé <ziade.tarek@gmail.com>
Sat, 24 Oct 2009 13:29:44 +0000 (13:29 +0000)
Lib/distutils/archive_util.py
Lib/distutils/tests/test_archive_util.py
Misc/NEWS

index bc5edfd8648907baaf99bf423f5d6077bac6e3ee..c741cc017482edbd9b420358940ee4a1363636dc 100644 (file)
@@ -233,9 +233,11 @@ def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,
         kwargs['owner'] = owner
         kwargs['group'] = group
 
-    filename = func(base_name, base_dir, **kwargs)
-    if root_dir is not None:
-        log.debug("changing back to '%s'", save_cwd)
-        os.chdir(save_cwd)
+    try:
+        filename = func(base_name, base_dir, **kwargs)
+    finally:
+        if root_dir is not None:
+            log.debug("changing back to '%s'", save_cwd)
+            os.chdir(save_cwd)
 
     return filename
index b91986ba9573a31614e3f67de226b7249ca70084..a9b46d8e22fbd7b35904ae8a68820b7095610591 100644 (file)
@@ -8,7 +8,8 @@ from os.path import splitdrive
 import warnings
 
 from distutils.archive_util import (check_archive_formats, make_tarball,
-                                    make_zipfile, make_archive)
+                                    make_zipfile, make_archive,
+                                    ARCHIVE_FORMATS)
 from distutils.spawn import find_executable, spawn
 from distutils.tests import support
 from test.test_support import check_warnings
@@ -262,6 +263,20 @@ class ArchiveUtilTestCase(support.TempdirManager,
         finally:
             archive.close()
 
+    def test_make_archive_cwd(self):
+        current_dir = os.getcwd()
+        def _breaks(*args, **kw):
+            raise RuntimeError()
+        ARCHIVE_FORMATS['xxx'] = (_breaks, [], 'xxx file')
+        try:
+            try:
+                make_archive('xxx', 'xxx', root_dir=self.mkdtemp())
+            except:
+                pass
+            self.assertEquals(os.getcwd(), current_dir)
+        finally:
+            del ARCHIVE_FORMATS['xxx']
+
 def test_suite():
     return unittest.makeSuite(ArchiveUtilTestCase)
 
index 7f3825914f52b05b86703432b402653fdfb61bbf..052ec4843d8f622ba96234ac129541aeeebf7acd 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -418,6 +418,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #7066: archive_util.make_archive now restores the cwd if an error is 
+  raised. Initial patch by Ezio Melotti.
+
 - Issue #6218: io.StringIO and io.BytesIO instances are now picklable with
   protocol 2.