resourceutil: start using importlib.resources.files() when possible stable
authorAnton Shestakov <av6@dwimlabs.net>
Thu, 12 Jan 2023 19:56:59 +0400
branchstable
changeset 49943 330d88217b83
parent 49939 32155ea1e930
child 49945 d00ac86fbd78
resourceutil: start using importlib.resources.files() when possible This avoids DeprecationWarnings related to our use of resources.open_binary() on Python 3.11.
mercurial/utils/resourceutil.py
--- 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