[PATCH 2 of 5] python3.13: fix resourceutil for removed deprecated importlib.resources
Mads Kiilerich
mads at kiilerich.com
Thu Jan 11 23:59:53 UTC 2024
# HG changeset patch
# User Mads Kiilerich <mads at kiilerich.com>
# Date 1705001574 -3600
# Thu Jan 11 20:32:54 2024 +0100
# Branch stable
# Node ID a06a7677696d8fa4fc3e33923425ef3fadd6f441
# Parent ab3021e9b0012db64e5bdc70e3f5a36324925d8c
python3.13: fix resourceutil for removed deprecated importlib.resources
The old functionality was deprecated in 3.11 and is on track to be removed in
3.13 . The documentation on
https://docs.python.org/3.12/library/importlib.resources.html recommends using
the new .files() that was introduced in 3.9.
The pytype annotation is probably not preserved correctly.
diff --git a/mercurial/utils/resourceutil.py b/mercurial/utils/resourceutil.py
--- a/mercurial/utils/resourceutil.py
+++ b/mercurial/utils/resourceutil.py
@@ -60,8 +60,10 @@ try:
# Force loading of the resources module
if hasattr(resources, 'files'):
+ # New in Python 3.9
resources.files # pytype: disable=module-attr
else:
+ # Deprecated in Python 3.11
resources.open_binary # pytype: disable=module-attr
# py2exe raises an AssertionError if uses importlib.resources
@@ -109,12 +111,20 @@ else:
)
def is_resource(package, name):
- return resources.is_resource( # pytype: disable=module-attr
- pycompat.sysstr(package), encoding.strfromlocal(name)
- )
+ if hasattr(resources, 'files'):
+ 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)
+ if hasattr(resources, 'files'):
+ for resource in resources.files(pycompat.sysstr(package)).iterdir():
+ if resource.is_file():
+ yield encoding.strtolocal(path.name)
+ else:
+ for r in resources.contents(pycompat.sysstr(package)):
+ # pytype: enable=module-attr
+ yield encoding.strtolocal(r)
More information about the Mercurial-devel
mailing list