changeset 47911:08f16b3331df

resources: narrow the try:except clause to minimum Otherwise this mind hides other import or attribute errors. Differential Revision: https://phab.mercurial-scm.org/D11312
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Thu, 19 Aug 2021 17:42:55 +0200
parents 155a2ec8a9dc
children 1c797757f5bb
files mercurial/utils/resourceutil.py
diffstat 1 files changed, 20 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/utils/resourceutil.py	Sun Aug 22 16:32:06 2021 -0400
+++ b/mercurial/utils/resourceutil.py	Thu Aug 19 17:42:55 2021 +0200
@@ -59,28 +59,9 @@
     # further down
     from importlib import resources
 
-    from .. import encoding
-
     # Force loading of the resources module
     resources.open_binary  # pytype: disable=module-attr
 
-    def open_resource(package, name):
-        return resources.open_binary(  # pytype: disable=module-attr
-            pycompat.sysstr(package), pycompat.sysstr(name)
-        )
-
-    def is_resource(package, name):
-        return resources.is_resource(  # pytype: disable=module-attr
-            pycompat.sysstr(package), encoding.strfromlocal(name)
-        )
-
-    def contents(package):
-        # pytype: disable=module-attr
-        for r in resources.contents(pycompat.sysstr(package)):
-            # pytype: enable=module-attr
-            yield encoding.strtolocal(r)
-
-
 except (ImportError, AttributeError):
     # importlib.resources was not found (almost definitely because we're on a
     # Python version before 3.7)
@@ -102,3 +83,23 @@
 
         for p in os.listdir(path):
             yield pycompat.fsencode(p)
+
+
+else:
+    from .. import encoding
+
+    def open_resource(package, name):
+        return resources.open_binary(  # pytype: disable=module-attr
+            pycompat.sysstr(package), pycompat.sysstr(name)
+        )
+
+    def is_resource(package, name):
+        return resources.is_resource(  # pytype: disable=module-attr
+            pycompat.sysstr(package), encoding.strfromlocal(name)
+        )
+
+    def contents(package):
+        # pytype: disable=module-attr
+        for r in resources.contents(pycompat.sysstr(package)):
+            # pytype: enable=module-attr
+            yield encoding.strtolocal(r)