# HG changeset patch # User Anton Shestakov # Date 2023-01-12 15:56:59 # Node ID 330d88217b83f0d030f6958e1cc869cad387335e # Parent 32155ea1e930e8f6a83171ecdb3aaa73e9058279 resourceutil: start using importlib.resources.files() when possible This avoids DeprecationWarnings related to our use of resources.open_binary() on Python 3.11. diff --git a/mercurial/utils/resourceutil.py b/mercurial/utils/resourceutil.py --- a/mercurial/utils/resourceutil.py +++ b/mercurial/utils/resourceutil.py @@ -59,7 +59,10 @@ try: from importlib import resources # pytype: disable=import-error # Force loading of the resources module - resources.open_binary # pytype: disable=module-attr + if pycompat.safehasattr(resources, 'files'): + resources.files # pytype: disable=module-attr + else: + resources.open_binary # pytype: disable=module-attr # py2exe raises an AssertionError if uses importlib.resources if getattr(sys, "frozen", None) in ("console_exe", "windows_exe"): @@ -92,9 +95,18 @@ else: from .. import encoding def open_resource(package, name): - return resources.open_binary( # pytype: disable=module-attr - pycompat.sysstr(package), pycompat.sysstr(name) - ) + if pycompat.safehasattr(resources, 'files'): + return ( + resources.files( # pytype: disable=module-attr + pycompat.sysstr(package) + ) + .joinpath(pycompat.sysstr(name)) + .open('rb') + ) + else: + 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