# HG changeset patch # User Mads Kiilerich # Date 1721665203 -7200 # Node ID 747a1370c5987604b345135d8d7fd7b978b674a0 # Parent c5d6a66092e862a0d57d97858ffc657b2b40fa2f utils: fix resourceutil use of deprecated importlib.resources Some importlib functionality was deprecated in 3.11 . The documentation on https://docs.python.org/3.12/library/importlib.resources.html recommends using the new .files() API that was introduced in 3.9. diff -r c5d6a66092e8 -r 747a1370c598 mercurial/utils/resourceutil.py --- a/mercurial/utils/resourceutil.py Tue Jun 27 13:05:03 2023 +0200 +++ b/mercurial/utils/resourceutil.py Mon Jul 22 18:20:03 2024 +0200 @@ -11,6 +11,8 @@ import os import sys +from typing import Iterator + from .. import pycompat @@ -48,6 +50,7 @@ assert dirs[0] == b"mercurial" return os.path.join(_rootpath, *dirs[1:]) + else: datapath = os.path.dirname(os.path.dirname(pycompat.fsencode(__file__))) _rootpath = os.path.dirname(datapath) @@ -62,7 +65,7 @@ from importlib import resources # pytype: disable=import-error # Force loading of the resources module - if hasattr(resources, 'files'): + if hasattr(resources, 'files'): # Introduced in Python 3.9 resources.files # pytype: disable=module-attr else: resources.open_binary # pytype: disable=module-attr @@ -93,6 +96,7 @@ for p in os.listdir(path): yield pycompat.fsencode(p) + else: from .. import encoding @@ -110,13 +114,25 @@ pycompat.sysstr(package), pycompat.sysstr(name) ) - def is_resource(package, name): - return resources.is_resource( # pytype: disable=module-attr - pycompat.sysstr(package), encoding.strfromlocal(name) - ) + def is_resource(package: bytes, name: bytes) -> bool: + if hasattr(resources, 'files'): # Introduced in Python 3.9 + return ( + resources.files(pycompat.sysstr(package)) + .joinpath(encoding.strfromlocal(name)) + .is_file() + ) + else: + return resources.is_resource( # pytype: disable=module-attr + pycompat.sysstr(package), encoding.strfromlocal(name) + ) - def contents(package): - # pytype: disable=module-attr - for r in resources.contents(pycompat.sysstr(package)): - # pytype: enable=module-attr - yield encoding.strtolocal(r) + def contents(package: bytes) -> "Iterator[bytes]": + if hasattr(resources, 'files'): # Introduced in Python 3.9 + for path in resources.files(pycompat.sysstr(package)).iterdir(): + if path.is_file(): + yield encoding.strtolocal(path.name) + else: + # pytype: disable=module-attr + for r in resources.contents(pycompat.sysstr(package)): + # pytype: enable=module-attr + yield encoding.strtolocal(r)