Merged revisions 75659 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r75659 | tarek.ziade | 2009-10-24 15:29:44 +0200 (Sat, 24 Oct 2009) | 1 line

  #7066 - Fixed distutils.archive_util.make_archive behavior so it restores the cwd
........
diff --git a/Lib/distutils/archive_util.py b/Lib/distutils/archive_util.py
index d051f91..28e93fe 100644
--- a/Lib/distutils/archive_util.py
+++ b/Lib/distutils/archive_util.py
@@ -232,10 +232,11 @@
         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
diff --git a/Lib/distutils/tests/test_archive_util.py b/Lib/distutils/tests/test_archive_util.py
index 71d32dc..682f19a 100644
--- a/Lib/distutils/tests/test_archive_util.py
+++ b/Lib/distutils/tests/test_archive_util.py
@@ -8,7 +8,8 @@
 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.support import check_warnings
@@ -262,6 +263,20 @@
         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)