template: handle missing resource in `_readmapfile` stable
authorPierre-Yves David <pierre-yves.david@octobus.net>
Wed, 18 Aug 2021 01:41:02 +0200
branchstable
changeset 47836 f3b1df44b716
parent 47835 da39b9c1c486
child 47837 6fc9bd898eec
template: handle missing resource in `_readmapfile` This fix the remaining issue in test-template-map.t. Differential Revision: https://phab.mercurial-scm.org/D11288
mercurial/templater.py
mercurial/utils/resourceutil.py
--- a/mercurial/templater.py	Wed Aug 18 01:10:40 2021 +0200
+++ b/mercurial/templater.py	Wed Aug 18 01:41:02 2021 +0200
@@ -852,9 +852,12 @@
         if not subresource:
             if pycompat.ossep not in rel:
                 abs = rel
-                subresource = resourceutil.open_resource(
-                    b'mercurial.templates', rel
-                )
+                try:
+                    subresource = resourceutil.open_resource(
+                        b'mercurial.templates', rel
+                    )
+                except resourceutil.FileNotFoundError:
+                    subresource = None
             else:
                 dir = templatedir()
                 if dir:
--- a/mercurial/utils/resourceutil.py	Wed Aug 18 01:10:40 2021 +0200
+++ b/mercurial/utils/resourceutil.py	Wed Aug 18 01:41:02 2021 +0200
@@ -64,6 +64,11 @@
     # Force loading of the resources module
     resources.open_binary  # pytype: disable=module-attr
 
+    # pytype: disable=import-error
+    from importlib.resources import FileNotFoundError
+
+    # pytype: enable=import-error
+
     def open_resource(package, name):
         return resources.open_binary(  # pytype: disable=module-attr
             pycompat.sysstr(package), pycompat.sysstr(name)
@@ -85,6 +90,9 @@
     # importlib.resources was not found (almost definitely because we're on a
     # Python version before 3.7)
 
+    class FileNotFoundError(RuntimeError):
+        pass
+
     def open_resource(package, name):
         path = os.path.join(_package_path(package), name)
         return open(path, "rb")