Patchwork D11288: template: handle missing resource in `_readmapfile`

login
register
mail settings
Submitter phabricator
Date Aug. 18, 2021, 1:01 a.m.
Message ID <differential-rev-PHID-DREV-kosp7n5vlv7aunfrcyvf-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/49602/
State Superseded
Headers show

Comments

phabricator - Aug. 18, 2021, 1:01 a.m.
marmoute created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  This should fix tests/test-template-map.t

REPOSITORY
  rHG Mercurial

BRANCH
  stable

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

AFFECTED FILES
  mercurial/templater.py
  mercurial/utils/resourceutil.py

CHANGE DETAILS




To: marmoute, #hg-reviewers
Cc: mercurial-patches, 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
@@ -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")
diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -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: