[PATCH 2 of 5] python3.13: fix resourceutil for removed deprecated importlib.resources

Manuel Jacob me at manueljacob.de
Fri Feb 2 01:10:08 UTC 2024


On 12/01/2024 00.59, Mads Kiilerich wrote:
> # 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.

Which annotation do you mean exactly?

If you mean “# pytype: disable=module-attr”, let’s see whether CI fails 
without it. Otherwise it would probably need to be on the same line as 
“resources.files”.

> 
> 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)

I think you meant “resource” instead of “path”.

> +        else:
> +            for r in resources.contents(pycompat.sysstr(package)):
> +                # pytype: enable=module-attr
> +                yield encoding.strtolocal(r)
> 
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel at lists.mercurial-scm.org
> https://lists.mercurial-scm.org/mailman/listinfo/mercurial-devel

Otherwise it LGTM.


More information about the Mercurial-devel mailing list