Mercurial > public > mercurial-scm > hg-stable
diff mercurial/templater.py @ 45341:4aa484efc926
templater: add exception-raising version of open_template()
I'm about to add another caller of `open_template()` (in the template
loader). That caller will want to get exceptions instead of `(None,
None)` if the template doesn't exist. This patch therefore changes
`open_template()` to raise exceptions and adds a new
`try_open_template()` that returns the `(None, None)` value.
Differential Revision: https://phab.mercurial-scm.org/D8905
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Thu, 06 Aug 2020 09:50:10 -0700 |
parents | 6e6fe826ba69 |
children | 735756ecda8c |
line wrap: on
line diff
--- a/mercurial/templater.py Wed Aug 05 22:13:51 2020 -0700 +++ b/mercurial/templater.py Thu Aug 06 09:50:10 2020 -0700 @@ -1095,17 +1095,18 @@ templatepath = templatedir() if templatepath is not None or os.path.isabs(name): f = os.path.join(templatepath, name) - try: - return f, open(f, mode='rb') - except EnvironmentError: - return None, None + return f, open(f, mode='rb') else: name_parts = pycompat.sysstr(name).split('/') package_name = '.'.join(['mercurial', 'templates'] + name_parts[:-1]) - try: - return ( - name, - resourceutil.open_resource(package_name, name_parts[-1]), - ) - except (ImportError, OSError): - return None, None + return ( + name, + resourceutil.open_resource(package_name, name_parts[-1]), + ) + + +def try_open_template(name, templatepath=None): + try: + return open_template(name, templatepath) + except (EnvironmentError, ImportError): + return None, None