Patchwork D7701: resourceutil: use `from importlib import resources`

login
register
mail settings
Submitter phabricator
Date Dec. 18, 2019, 10:30 p.m.
Message ID <differential-rev-PHID-DREV-pmlyybjh2mvkawymf22c-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/43980/
State Superseded
Headers show

Comments

phabricator - Dec. 18, 2019, 10:30 p.m.
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




To: martinvonz, #hg-reviewers
Cc: mercurial-devel
phabricator - Dec. 19, 2019, 2:04 a.m.
This revision is now accepted and ready to land.
indygreg added a comment.
indygreg accepted this revision.


  That's a weird error for PyOxidizer! I'm not sure why it would be happening either.

REPOSITORY
  rHG Mercurial

BRANCH
  default

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7701/new/

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

To: martinvonz, #hg-reviewers, indygreg
Cc: indygreg, mercurial-devel
phabricator - Dec. 19, 2019, 2:06 a.m.
This revision now requires changes to proceed.
indygreg added a comment.
indygreg requested changes to this revision.


  This patch doesn't apply cleanly. Could you please take a look @martinvonz.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7701/new/

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

To: martinvonz, #hg-reviewers, indygreg
Cc: indygreg, mercurial-devel
phabricator - Dec. 19, 2019, 5:44 a.m.
martinvonz added a comment.


  In D7701#113332 <https://phab.mercurial-scm.org/D7701#113332>, @indygreg wrote:
  
  > This patch doesn't apply cleanly. Could you please take a look @martinvonz.
  
  To be clear, it didn't come directly from PyOxidizer. I instrumented `resourceutil.py` to print the `ImportError` that was raised and that is what that error was.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7701/new/

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

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

Patch

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