bpo-8978: improve tarfile.open error message when lzma / bz2 are missing (GH-24850)
Automerge-Triggered-By: GH:pablogsal
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index 77ad830..817e6f1 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -2283,6 +2283,18 @@ def test__all__(self):
'SubsequentHeaderError', 'ExFileObject', 'main'}
support.check__all__(self, tarfile, not_exported=not_exported)
+ def test_useful_error_message_when_modules_missing(self):
+ fname = os.path.join(os.path.dirname(__file__), 'testtar.tar.xz')
+ with self.assertRaises(tarfile.ReadError) as excinfo:
+ error = tarfile.CompressionError('lzma module is not available'),
+ with unittest.mock.patch.object(tarfile.TarFile, 'xzopen', side_effect=error):
+ tarfile.open(fname)
+
+ self.assertIn(
+ "\n- method xz: CompressionError('lzma module is not available')\n",
+ str(excinfo.exception),
+ )
+
class CommandLineTest(unittest.TestCase):
diff --git a/Lib/test/testtar.tar.xz b/Lib/test/testtar.tar.xz
new file mode 100644
index 0000000..512fa14
--- /dev/null
+++ b/Lib/test/testtar.tar.xz
Binary files differ