D7701: resourceutil: use `from importlib import resources`

martinvonz (Martin von Zweigbergk) phabricator at mercurial-scm.org
Wed Dec 18 17:30:55 EST 2019


martinvonz created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  Without this patch, we get the following error from when trying to run
  hg with PyOxidizer:
  
    module 'importlib' has no attribute 'resources'
  
  I don't know what why that happens, but `from importlib import
  resources` is the form I would prefer anyway, so let's use that now
  that the impoort-checker has been fixed.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D7701

AFFECTED FILES
  mercurial/utils/resourceutil.py

CHANGE DETAILS

diff --git a/mercurial/utils/resourceutil.py b/mercurial/utils/resourceutil.py
--- a/mercurial/utils/resourceutil.py
+++ b/mercurial/utils/resourceutil.py
@@ -43,25 +43,25 @@
     datapath = os.path.dirname(os.path.dirname(pycompat.fsencode(__file__)))
 
 try:
-    import importlib
+    from importlib import resources
 
     # Force loading of the resources module
-    importlib.resources.open_binary  # pytype: disable=module-attr
+    resources.open_binary  # pytype: disable=module-attr
 
     def open_resource(package, name):
         package = b'mercurial.' + package
-        return importlib.resources.open_binary(  # pytype: disable=module-attr
+        return resources.open_binary(  # pytype: disable=module-attr
             pycompat.sysstr(package), pycompat.sysstr(name)
         )
 
     def list_resources(package):
         package = b'mercurial.' + package
-        for name in importlib.resources.contents(pycompat.sysstr(package)):
-            if importlib.resources.is_resource(pycompat.sysstr(package), name):
+        for name in resources.contents(pycompat.sysstr(package)):
+            if resources.is_resource(pycompat.sysstr(package), name):
                 yield pycompat.sysbytes(name)
 
 
-except AttributeError:
+except (ImportError, AttributeError):
 
     def _package_path(package):
         return os.path.join(datapath, *package.split(b'.'))



To: martinvonz, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list