Mercurial > public > mercurial-scm > hg
comparison mercurial/utils/resourceutil.py @ 45452:dd9e28612468
resourceutil: document when we expect to take the importlib.resouces code path
Differential Revision: https://phab.mercurial-scm.org/D9018
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Sat, 12 Sep 2020 11:18:12 -0700 |
parents | 6c8384afbf77 |
children | 7fd369644c68 |
comparison
equal
deleted
inserted
replaced
45451:e53a3d0ef416 | 45452:dd9e28612468 |
---|---|
53 def _package_path(package): | 53 def _package_path(package): |
54 return os.path.join(_rootpath, *package.split(b".")) | 54 return os.path.join(_rootpath, *package.split(b".")) |
55 | 55 |
56 | 56 |
57 try: | 57 try: |
58 # importlib.resources exists from Python 3.7; see fallback in except clause | |
59 # further down | |
58 from importlib import resources | 60 from importlib import resources |
59 | 61 |
60 from .. import encoding | 62 from .. import encoding |
61 | 63 |
62 # Force loading of the resources module | 64 # Force loading of the resources module |
76 for r in resources.contents(pycompat.sysstr(package)): | 78 for r in resources.contents(pycompat.sysstr(package)): |
77 yield encoding.strtolocal(r) | 79 yield encoding.strtolocal(r) |
78 | 80 |
79 | 81 |
80 except (ImportError, AttributeError): | 82 except (ImportError, AttributeError): |
83 # importlib.resources was not found (almost definitely because we're on a | |
84 # Python version before 3.7) | |
81 | 85 |
82 def open_resource(package, name): | 86 def open_resource(package, name): |
83 path = os.path.join(_package_path(package), name) | 87 path = os.path.join(_package_path(package), name) |
84 return open(path, "rb") | 88 return open(path, "rb") |
85 | 89 |