From 2779e806b8846c40ab32f91cea068e6aec0211b0 Mon Sep 17 00:00:00 2001 From: pikammmmm <124257101+pikammmmm@users.noreply.github.com> Date: Thu, 30 Jul 2026 13:41:01 +0200 Subject: [PATCH 1/3] gh-127337: Fix importlib.resources for legacy ResourceReader loaders CompatibilityFiles.SpecPath, .ChildPath and .OrphanPath each defined joinpath() with a single required argument, but importlib.resources.abc.Traversable declares joinpath(*descendants) and documents that passing no descendants returns self. Since 3.13 the functional API is implemented on top of files(anchor).joinpath(*path_names), so any call that passes no path names -- contents(anchor), is_resource(anchor) -- raised TypeError for a package whose loader provides only a legacy ResourceReader. On 3.12, contents(anchor) returned the resource names. Passing several descendants at once now gives the same result as applying them one at a time, which is the equivalence the Traversable documentation relies on for implementations predating the multi-argument protocol. --- Lib/importlib/resources/_adapters.py | 22 +++++++--- .../resources/test_compatibilty_files.py | 44 +++++++++++++++++++ ...-07-30-13-36-06.gh-issue-127337.ahcvtH.rst | 9 ++++ 3 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-07-30-13-36-06.gh-issue-127337.ahcvtH.rst diff --git a/Lib/importlib/resources/_adapters.py b/Lib/importlib/resources/_adapters.py index 50688fbb666658..e2ad6879be6744 100644 --- a/Lib/importlib/resources/_adapters.py +++ b/Lib/importlib/resources/_adapters.py @@ -66,10 +66,14 @@ def is_file(self): is_dir = is_file - def joinpath(self, other): + def joinpath(self, *descendants): + if not descendants: + return self if not self._reader: - return CompatibilityFiles.OrphanPath(other) - return CompatibilityFiles.ChildPath(self._reader, other) + return CompatibilityFiles.OrphanPath(*descendants) + first, *rest = descendants + child = CompatibilityFiles.ChildPath(self._reader, first) + return child.joinpath(*rest) @property def name(self): @@ -97,8 +101,10 @@ def is_file(self): def is_dir(self): return not self.is_file() - def joinpath(self, other): - return CompatibilityFiles.OrphanPath(self.name, other) + def joinpath(self, *descendants): + if not descendants: + return self + return CompatibilityFiles.OrphanPath(self.name, *descendants) @property def name(self): @@ -128,8 +134,10 @@ def is_file(self): is_dir = is_file - def joinpath(self, other): - return CompatibilityFiles.OrphanPath(*self._path, other) + def joinpath(self, *descendants): + if not descendants: + return self + return CompatibilityFiles.OrphanPath(*self._path, *descendants) @property def name(self): diff --git a/Lib/test/test_importlib/resources/test_compatibilty_files.py b/Lib/test/test_importlib/resources/test_compatibilty_files.py index 2fd39bedf258d1..e70e3ad3718601 100644 --- a/Lib/test/test_importlib/resources/test_compatibilty_files.py +++ b/Lib/test/test_importlib/resources/test_compatibilty_files.py @@ -6,6 +6,8 @@ wrap_spec, ) +from test.support import warnings_helper + from . import util @@ -79,6 +81,37 @@ def test_orphan_path_invalid(self): with self.assertRaises(ValueError): CompatibilityFiles.OrphanPath() + def test_spec_path_joinpath_no_descendants(self): + files = self.files + assert files.joinpath() is files + + def test_child_path_joinpath_no_descendants(self): + child = self.files / 'a' + assert child.joinpath() is child + + def test_orphan_path_joinpath_no_descendants(self): + orphan = self.files / 'a' / 'b' + assert orphan.joinpath() is orphan + + def test_joinpath_multiple_descendants(self): + # Several descendants at once give the same result as applying them + # one at a time, as documented for Traversable.joinpath(). + files = self.files + for path, expected in ( + (files.joinpath('a', 'b'), files / 'a' / 'b'), + ((files / 'a').joinpath('b', 'c'), files / 'a' / 'b' / 'c'), + ((files / 'a' / 'b').joinpath('c', 'd'), files / 'a' / 'b' / 'c' / 'd'), + ): + assert isinstance(path, CompatibilityFiles.OrphanPath) + assert path._path == expected._path + + def test_functional_api_without_path_names(self): + # gh-127337: these call joinpath() with no descendants at all. + package = self.package + with warnings_helper.check_warnings((".*contents.*", DeprecationWarning)): + assert sorted(resources.contents(package)) == ['a', 'b', 'c'] + assert not resources.is_resource(package) + def test_wrap_spec(self): spec = wrap_spec(self.package) assert isinstance(spec.loader.get_resource_reader(None), CompatibilityFiles) @@ -95,3 +128,14 @@ def files(self): def test_spec_path_joinpath(self): assert isinstance(self.files / 'a', CompatibilityFiles.OrphanPath) + + def test_spec_path_joinpath_no_descendants(self): + # Returns self rather than an OrphanPath with no parts, which would + # be rejected by OrphanPath.__init__(). + files = self.files + assert files.joinpath() is files + + def test_spec_path_joinpath_multiple_descendants(self): + path = self.files.joinpath('a', 'b') + assert isinstance(path, CompatibilityFiles.OrphanPath) + assert path._path == (self.files / 'a' / 'b')._path diff --git a/Misc/NEWS.d/next/Library/2026-07-30-13-36-06.gh-issue-127337.ahcvtH.rst b/Misc/NEWS.d/next/Library/2026-07-30-13-36-06.gh-issue-127337.ahcvtH.rst new file mode 100644 index 00000000000000..0883c8f497fafe --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-30-13-36-06.gh-issue-127337.ahcvtH.rst @@ -0,0 +1,9 @@ +Fix a regression in :mod:`importlib.resources` where functions such as +:func:`~importlib.resources.is_resource` and +:func:`~importlib.resources.contents` raised :exc:`TypeError` for a package +whose loader provides only a legacy +:class:`~importlib.resources.abc.ResourceReader`. The internal +``CompatibilityFiles`` traversables wrapping such a reader now accept the +zero-or-more argument ``joinpath()`` signature specified by +:meth:`importlib.resources.abc.Traversable.joinpath`. +Patch by pikammmmm. From c37c21f1228ebd9c0e3750421e82afa62e24b1b6 Mon Sep 17 00:00:00 2001 From: pikammmmm <124257101+pikammmmm@users.noreply.github.com> Date: Wed, 11 Nov 2026 21:50:06 +0100 Subject: [PATCH 2/3] Split multi-level descendants in CompatibilityFiles joinpath() Traversable.joinpath() documents that each descendant may contain several levels separated by '/'. joinpath('a/b') now gives the same result as joinpath('a', 'b') on SpecPath, ChildPath and OrphanPath. --- Lib/importlib/resources/_adapters.py | 31 ++++++++++++++----- .../resources/test_compatibilty_files.py | 19 ++++++++++++ ...-07-30-13-36-06.gh-issue-127337.ahcvtH.rst | 4 ++- 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/Lib/importlib/resources/_adapters.py b/Lib/importlib/resources/_adapters.py index e2ad6879be6744..88d03c62d67766 100644 --- a/Lib/importlib/resources/_adapters.py +++ b/Lib/importlib/resources/_adapters.py @@ -1,3 +1,5 @@ +import itertools +import pathlib from contextlib import suppress from io import TextIOWrapper @@ -37,6 +39,18 @@ def _io_wrapper(file, mode='r', *args, **kwargs): raise ValueError(f"Invalid mode value '{mode}', only 'r' and 'rb' are supported") +def _names(descendants): + """ + Split descendants into single path names, as each one + may contain multiple levels separated by ``posixpath.sep``. + """ + return tuple( + itertools.chain.from_iterable( + path.parts for path in map(pathlib.PurePosixPath, descendants) + ) + ) + + class CompatibilityFiles: """ Adapter for an existing or non-existent resource reader @@ -67,11 +81,12 @@ def is_file(self): is_dir = is_file def joinpath(self, *descendants): - if not descendants: + names = _names(descendants) + if not names: return self if not self._reader: - return CompatibilityFiles.OrphanPath(*descendants) - first, *rest = descendants + return CompatibilityFiles.OrphanPath(*names) + first, *rest = names child = CompatibilityFiles.ChildPath(self._reader, first) return child.joinpath(*rest) @@ -102,9 +117,10 @@ def is_dir(self): return not self.is_file() def joinpath(self, *descendants): - if not descendants: + names = _names(descendants) + if not names: return self - return CompatibilityFiles.OrphanPath(self.name, *descendants) + return CompatibilityFiles.OrphanPath(self.name, *names) @property def name(self): @@ -135,9 +151,10 @@ def is_file(self): is_dir = is_file def joinpath(self, *descendants): - if not descendants: + names = _names(descendants) + if not names: return self - return CompatibilityFiles.OrphanPath(*self._path, *descendants) + return CompatibilityFiles.OrphanPath(*self._path, *names) @property def name(self): diff --git a/Lib/test/test_importlib/resources/test_compatibilty_files.py b/Lib/test/test_importlib/resources/test_compatibilty_files.py index e70e3ad3718601..5b9e2dc644a009 100644 --- a/Lib/test/test_importlib/resources/test_compatibilty_files.py +++ b/Lib/test/test_importlib/resources/test_compatibilty_files.py @@ -105,6 +105,20 @@ def test_joinpath_multiple_descendants(self): assert isinstance(path, CompatibilityFiles.OrphanPath) assert path._path == expected._path + def test_joinpath_multiple_levels(self): + # A descendant may contain several levels separated by '/', which + # gives the same result as passing the levels separately. + files = self.files + for path, expected in ( + (files.joinpath('a/b'), files / 'a' / 'b'), + (files / 'a/b', files / 'a' / 'b'), + (files.joinpath('a/b', 'c'), files / 'a' / 'b' / 'c'), + ((files / 'a').joinpath('b/c'), files / 'a' / 'b' / 'c'), + ((files / 'a' / 'b').joinpath('c/d'), files / 'a' / 'b' / 'c' / 'd'), + ): + assert isinstance(path, CompatibilityFiles.OrphanPath) + assert path._path == expected._path + def test_functional_api_without_path_names(self): # gh-127337: these call joinpath() with no descendants at all. package = self.package @@ -139,3 +153,8 @@ def test_spec_path_joinpath_multiple_descendants(self): path = self.files.joinpath('a', 'b') assert isinstance(path, CompatibilityFiles.OrphanPath) assert path._path == (self.files / 'a' / 'b')._path + + def test_spec_path_joinpath_multiple_levels(self): + path = self.files.joinpath('a/b') + assert isinstance(path, CompatibilityFiles.OrphanPath) + assert path._path == (self.files / 'a' / 'b')._path diff --git a/Misc/NEWS.d/next/Library/2026-07-30-13-36-06.gh-issue-127337.ahcvtH.rst b/Misc/NEWS.d/next/Library/2026-07-30-13-36-06.gh-issue-127337.ahcvtH.rst index 0883c8f497fafe..71da5f530635b7 100644 --- a/Misc/NEWS.d/next/Library/2026-07-30-13-36-06.gh-issue-127337.ahcvtH.rst +++ b/Misc/NEWS.d/next/Library/2026-07-30-13-36-06.gh-issue-127337.ahcvtH.rst @@ -5,5 +5,7 @@ whose loader provides only a legacy :class:`~importlib.resources.abc.ResourceReader`. The internal ``CompatibilityFiles`` traversables wrapping such a reader now accept the zero-or-more argument ``joinpath()`` signature specified by -:meth:`importlib.resources.abc.Traversable.joinpath`. +:meth:`importlib.resources.abc.Traversable.joinpath`. They also split a +descendant containing ``/`` into its levels, so ``joinpath('a/b')`` and +``joinpath('a', 'b')`` give the same result. Patch by pikammmmm. From 9043f9fe210d621756bebf34d9231ac69ccd670a Mon Sep 17 00:00:00 2001 From: pikammmmm <124257101+pikammmmm@users.noreply.github.com> Date: Wed, 11 Nov 2026 22:46:16 +0100 Subject: [PATCH 3/3] Revert "Split multi-level descendants in CompatibilityFiles joinpath()" This reverts commit c37c21f122. Splitting on '/' turned a ChildPath into an OrphanPath for readers that serve names containing '/', which is a behaviour change this fix should not carry. --- Lib/importlib/resources/_adapters.py | 31 +++++-------------- .../resources/test_compatibilty_files.py | 19 ------------ ...-07-30-13-36-06.gh-issue-127337.ahcvtH.rst | 4 +-- 3 files changed, 8 insertions(+), 46 deletions(-) diff --git a/Lib/importlib/resources/_adapters.py b/Lib/importlib/resources/_adapters.py index 88d03c62d67766..e2ad6879be6744 100644 --- a/Lib/importlib/resources/_adapters.py +++ b/Lib/importlib/resources/_adapters.py @@ -1,5 +1,3 @@ -import itertools -import pathlib from contextlib import suppress from io import TextIOWrapper @@ -39,18 +37,6 @@ def _io_wrapper(file, mode='r', *args, **kwargs): raise ValueError(f"Invalid mode value '{mode}', only 'r' and 'rb' are supported") -def _names(descendants): - """ - Split descendants into single path names, as each one - may contain multiple levels separated by ``posixpath.sep``. - """ - return tuple( - itertools.chain.from_iterable( - path.parts for path in map(pathlib.PurePosixPath, descendants) - ) - ) - - class CompatibilityFiles: """ Adapter for an existing or non-existent resource reader @@ -81,12 +67,11 @@ def is_file(self): is_dir = is_file def joinpath(self, *descendants): - names = _names(descendants) - if not names: + if not descendants: return self if not self._reader: - return CompatibilityFiles.OrphanPath(*names) - first, *rest = names + return CompatibilityFiles.OrphanPath(*descendants) + first, *rest = descendants child = CompatibilityFiles.ChildPath(self._reader, first) return child.joinpath(*rest) @@ -117,10 +102,9 @@ def is_dir(self): return not self.is_file() def joinpath(self, *descendants): - names = _names(descendants) - if not names: + if not descendants: return self - return CompatibilityFiles.OrphanPath(self.name, *names) + return CompatibilityFiles.OrphanPath(self.name, *descendants) @property def name(self): @@ -151,10 +135,9 @@ def is_file(self): is_dir = is_file def joinpath(self, *descendants): - names = _names(descendants) - if not names: + if not descendants: return self - return CompatibilityFiles.OrphanPath(*self._path, *names) + return CompatibilityFiles.OrphanPath(*self._path, *descendants) @property def name(self): diff --git a/Lib/test/test_importlib/resources/test_compatibilty_files.py b/Lib/test/test_importlib/resources/test_compatibilty_files.py index 5b9e2dc644a009..e70e3ad3718601 100644 --- a/Lib/test/test_importlib/resources/test_compatibilty_files.py +++ b/Lib/test/test_importlib/resources/test_compatibilty_files.py @@ -105,20 +105,6 @@ def test_joinpath_multiple_descendants(self): assert isinstance(path, CompatibilityFiles.OrphanPath) assert path._path == expected._path - def test_joinpath_multiple_levels(self): - # A descendant may contain several levels separated by '/', which - # gives the same result as passing the levels separately. - files = self.files - for path, expected in ( - (files.joinpath('a/b'), files / 'a' / 'b'), - (files / 'a/b', files / 'a' / 'b'), - (files.joinpath('a/b', 'c'), files / 'a' / 'b' / 'c'), - ((files / 'a').joinpath('b/c'), files / 'a' / 'b' / 'c'), - ((files / 'a' / 'b').joinpath('c/d'), files / 'a' / 'b' / 'c' / 'd'), - ): - assert isinstance(path, CompatibilityFiles.OrphanPath) - assert path._path == expected._path - def test_functional_api_without_path_names(self): # gh-127337: these call joinpath() with no descendants at all. package = self.package @@ -153,8 +139,3 @@ def test_spec_path_joinpath_multiple_descendants(self): path = self.files.joinpath('a', 'b') assert isinstance(path, CompatibilityFiles.OrphanPath) assert path._path == (self.files / 'a' / 'b')._path - - def test_spec_path_joinpath_multiple_levels(self): - path = self.files.joinpath('a/b') - assert isinstance(path, CompatibilityFiles.OrphanPath) - assert path._path == (self.files / 'a' / 'b')._path diff --git a/Misc/NEWS.d/next/Library/2026-07-30-13-36-06.gh-issue-127337.ahcvtH.rst b/Misc/NEWS.d/next/Library/2026-07-30-13-36-06.gh-issue-127337.ahcvtH.rst index 71da5f530635b7..0883c8f497fafe 100644 --- a/Misc/NEWS.d/next/Library/2026-07-30-13-36-06.gh-issue-127337.ahcvtH.rst +++ b/Misc/NEWS.d/next/Library/2026-07-30-13-36-06.gh-issue-127337.ahcvtH.rst @@ -5,7 +5,5 @@ whose loader provides only a legacy :class:`~importlib.resources.abc.ResourceReader`. The internal ``CompatibilityFiles`` traversables wrapping such a reader now accept the zero-or-more argument ``joinpath()`` signature specified by -:meth:`importlib.resources.abc.Traversable.joinpath`. They also split a -descendant containing ``/`` into its levels, so ``joinpath('a/b')`` and -``joinpath('a', 'b')`` give the same result. +:meth:`importlib.resources.abc.Traversable.joinpath`. Patch by pikammmmm.