Patchwork [1,of,2,stable] demandimport: ignore `lzma` module for demandimport

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

Comments

Manuel Jacob - July 8, 2020, 6:58 a.m.
# HG changeset patch
# User Manuel Jacob <me@manueljacob.de>
# Date 1594189530 -7200
#      Wed Jul 08 08:25:30 2020 +0200
# Branch stable
# Node ID fd73d5b6e982804810e2399b4e16c6f104bf1d3c
# Parent  b1a1702262c93fe5879865dad43bd1016b1397b5
# EXP-Topic lzma
demandimport: ignore `lzma` module for demandimport

This makes importing the module fail if the `_lzma` module is not present.

This makes e.g. tarfile correctly recognize if LZMA support is not present. It
changes the exception

    File "/usr/lib/python3.6/tarfile.py", line 1694, in xzopen
      fileobj = lzma.LZMAFile(fileobj or name, mode, preset=preset)
  AttributeError: module 'lzma' has no attribute 'LZMAFile'

to the more correct exception

    File "/usr/lib/python3.6/tarfile.py", line 1692, in xzopen
      raise CompressionError("lzma module is not available")
  tarfile.CompressionError: lzma module is not available

Also, it prevents that the error "abort: No module named '_lzma'!" is shown when
a development warning is to be shown. The reason why that happened is that for
showing the warning, we get information about the stack frames from the inspect
module, which accesses the `__file__` attribute of all modules in `sys.modules`
to build some cache, causing all modules (including `lzma`) to be imported.
Yuya Nishihara - July 8, 2020, 10:44 a.m.
On Wed, 08 Jul 2020 08:58:29 +0200, Manuel Jacob wrote:
> # HG changeset patch
> # User Manuel Jacob <me@manueljacob.de>
> # Date 1594189530 -7200
> #      Wed Jul 08 08:25:30 2020 +0200
> # Branch stable
> # Node ID fd73d5b6e982804810e2399b4e16c6f104bf1d3c
> # Parent  b1a1702262c93fe5879865dad43bd1016b1397b5
> # EXP-Topic lzma
> demandimport: ignore `lzma` module for demandimport

Queued for stable, thanks.

Patch

diff --git a/hgdemandimport/__init__.py b/hgdemandimport/__init__.py
--- a/hgdemandimport/__init__.py
+++ b/hgdemandimport/__init__.py
@@ -56,6 +56,7 @@ 
     '__builtin__',
     'builtins',
     'urwid.command_map',  # for pudb
+    'lzma',
 }
 
 _pypy = '__pypy__' in sys.builtin_module_names