Patchwork [2,of,2,stable] archival: abort if compression method is unavailable

login
register
mail settings
Submitter Manuel Jacob
Date July 8, 2020, 6:58 a.m.
Message ID <30f95ba5fd923a0a26d7.1594191510@tmp>
Download mbox | patch
Permalink /patch/46655/
State Accepted
Headers show

Comments

Manuel Jacob - July 8, 2020, 6:58 a.m.
# HG changeset patch
# User Manuel Jacob <me@manueljacob.de>
# Date 1594191441 -7200
#      Wed Jul 08 08:57:21 2020 +0200
# Branch stable
# Node ID 30f95ba5fd923a0a26d75c42f49e016a27da2816
# Parent  fd73d5b6e982804810e2399b4e16c6f104bf1d3c
# EXP-Topic lzma
archival: abort if compression method is unavailable

`tarfile.CompressionError` is documented to be the "exception for unavailable
compression methods".

Also, make tests conditional on whether the lzma module is available or not.

Patch

diff --git a/mercurial/archival.py b/mercurial/archival.py
--- a/mercurial/archival.py
+++ b/mercurial/archival.py
@@ -189,7 +189,12 @@ 
                     name, pycompat.sysstr(mode), gzfileobj
                 )
             else:
-                return tarfile.open(name, pycompat.sysstr(mode + kind), fileobj)
+                try:
+                    return tarfile.open(
+                        name, pycompat.sysstr(mode + kind), fileobj
+                    )
+                except tarfile.CompressionError as e:
+                    raise error.Abort(pycompat.bytestr(e))
 
         if isinstance(dest, bytes):
             self.z = taropen(b'w:', name=dest)
diff --git a/tests/hghave.py b/tests/hghave.py
--- a/tests/hghave.py
+++ b/tests/hghave.py
@@ -1074,3 +1074,14 @@ 
     return matchoutput(
         '`rustup which --toolchain nightly rustfmt` --version', b'rustfmt'
     )
+
+
+@check("lzma", "python lzma module")
+def has_lzma():
+    try:
+        import _lzma
+
+        _lzma.FORMAT_XZ
+        return True
+    except ImportError:
+        return False
diff --git a/tests/test-archive.t b/tests/test-archive.t
--- a/tests/test-archive.t
+++ b/tests/test-archive.t
@@ -576,12 +576,18 @@ 
 
 test xz support only available in Python 3.4
 
-#if py3
+#if lzma
   $ hg archive ../archive.txz
   $ which xz >/dev/null && xz -l ../archive.txz | head -n1 || true
   Strms  Blocks   Compressed Uncompressed  Ratio  Check   Filename (xz !)
   $ rm -f ../archive.txz
-#else
+#endif
+#if py3 no-lzma
+  $ hg archive ../archive.txz
+  abort: lzma module is not available
+  [255]
+#endif
+#if no-py3
   $ hg archive ../archive.txz
   abort: xz compression is only available in Python 3
   [255]