changeset 49638:330d88217b83 stable

resourceutil: start using importlib.resources.files() when possible This avoids DeprecationWarnings related to our use of resources.open_binary() on Python 3.11.
author Anton Shestakov <av6@dwimlabs.net>
date Thu, 12 Jan 2023 19:56:59 +0400
parents 32155ea1e930
children d00ac86fbd78
files mercurial/utils/resourceutil.py
diffstat 1 files changed, 16 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/utils/resourceutil.py	Tue Jan 10 17:35:53 2023 +0400
+++ b/mercurial/utils/resourceutil.py	Thu Jan 12 19:56:59 2023 +0400
@@ -59,7 +59,10 @@
     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 @@
     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