Skip to content

importlib.resources regression for custom ResourceReader#127337

@zooba

Description

@zooba

Since Python 3.13 (including 3.14), my custom ResourceReader is now raising errors when used with importlib.resources.contents. This code is otherwise functioning fine from 3.8 onwards.

Traceback (most recent call last): File "D:\cpython\t.py", line 41, in <module> print(contents(FakeModule)) ~~~~~~~~^^^^^^^^^^^^ File "D:\cpython\Lib\importlib\resources\_functional.py", line 60, in contents return (resource.name for resource in _get_resource(anchor, path_names).iterdir()) ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^ File "D:\cpython\Lib\importlib\resources\_functional.py", line 81, in _get_resource return files(anchor).joinpath(*path_names) ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^ TypeError: CompatibilityFiles.SpecPath.joinpath() missing 1 required positional argument: 'other' 

Self-contained repro below (the real one gets compiled into DLLs, so you don't want to try to debug it ;) ):

fromimportlib.abcimportLoaderfromimportlib.machineryimportModuleSpecfromimportlib.resources.abcimportResourceReader_DATA={"FakeModule.NAME": b"hello"} _DATA_NAMES=set(_DATA) classMyReader(ResourceReader): def__init__(self, prefix): self.prefix=prefixdefopen_resource(self, resource): importioreturnio.BytesIO(_DATA[self.prefix+resource]) defresource_path(self, resource): raiseFileNotFoundError() defis_resource(self, resource): returnself.prefix+resourcein_DATA_NAMESdefcontents(self): p=self.prefixlp=len(p) return (n[lp:] fornin_DATA_NAMESifn.startswith(p)) classMyLoader(Loader): create_module= ... exec_module= ... defget_resource_reader(self, fullname): returnMyReader(fullname+".") classFakeModule: __loader__=MyLoader() __name__="FakeModule"__spec__=ModuleSpec(__name__, __loader__) fromimportlib.resourcesimportcontentsprint(contents(FakeModule))

(ping @jaraco)

Metadata

Metadata

Assignees

Labels

3.13bugs and security fixes3.14bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-importlib

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions