summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/distutils/archive_util.py11
-rw-r--r--Lib/distutils/tests/test_archive_util.py17
-rw-r--r--Misc/NEWS3
3 files changed, 25 insertions, 6 deletions
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 @@ 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
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 @@ 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.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)
diff --git a/Misc/NEWS b/Misc/NEWS
index 8830b7d..e8e0a4a 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -1143,6 +1143,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 #6516: Added owner/group support when creating tar archives in
Distutils.