classification
Title: archive_util.make_archive doesn't restore the cwd if an error is raised
Type: behavior Stage:
Components: Distutils Versions: Python 3.2, Python 3.1, Python 2.7, Python 2.6
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: tarek Nosy List: ezio.melotti, r.david.murray, tarek (3)
Priority: normal Keywords

Created on 2009-10-05 19:40 by ezio.melotti, last changed 2009-10-24 13:45 by tarek.

Messages (4)
msg93618 - (view) Author: Ezio Melotti (ezio.melotti) Date: 2009-10-05 19:40
archive_util.make_archive() changes the value of the cwd, but if an
error is raised in "filename = func(base_name, base_dir, **kwargs)", the
cwd is not restored. This may happen if zlib is not available and causes
other ~60 tests to fail while running regrtest.py.

This trivial fix solve the problem here:

-    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)

The new test_make_archive_owner_group() test added in r75192 also needs
a @unittest.skipUnless(zlib, "Requires zlib"), otherwise dist_distutils
fails.
Since half of this test doesn't require zlib (i.e. when 'tar' is used)
it might be a good idea to split the test in two, in order to test the
'tar' archives even if 'zlib' is not available.

(Thanks to R. David Murray - he spot the right place where the cwd is
changed and not restored.)
msg93622 - (view) Author: Tarek Ziadé (tarek) Date: 2009-10-05 21:25
I see two distinct points (besides test_make_archive_owner_group):

1- having a try..finally when changing the current working dir (I'll do
this thanks for the patch)

2- dealing with calls made on archive_util.make_archive() when zlib is
not available. What error do you get ? CompressionError I guess ?
msg93624 - (view) Author: Ezio Melotti (ezio.melotti) Date: 2009-10-05 21:27
======================================================================
ERROR: test_make_archive_owner_group
(distutils.tests.test_archive_util.ArchiveUtilTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/wolf/dev/trunk/Lib/distutils/tests/test_archive_util.py",
line 224, in test_make_archive_owner_group
    group=group)
  File "/home/wolf/dev/trunk/Lib/distutils/archive_util.py", line 237,
in make_archive
    filename = func(base_name, base_dir, **kwargs)
  File "/home/wolf/dev/trunk/Lib/distutils/archive_util.py", line 163,
in make_zipfile
    compression=zipfile.ZIP_DEFLATED)
  File "/home/wolf/dev/trunk/Lib/zipfile.py", line 675, in __init__
    "Compression requires the (missing) zlib module"
RuntimeError: Compression requires the (missing) zlib module
msg94415 - (view) Author: Tarek Ziadé (tarek) Date: 2009-10-24 13:45
Done in r75659, r75662 and r75663 (merge in 2.6 maint. waiting for 2.6.4
final tag to be done)

Thanks !
History
Date User Action Args
2009-10-24 13:45:32tareksetstatus: open -> closed

messages: + msg94415
versions: + Python 2.6, Python 3.1
2009-10-05 21:27:15ezio.melottisetmessages: + msg93624
2009-10-05 21:25:15tareksetmessages: + msg93622
2009-10-05 20:48:56tareksetresolution: accepted
2009-10-05 19:40:26ezio.melotticreate