Skip to content

ResourceReader API can't be implemented for zipimporter #49

@jaraco

Description

@jaraco

In GitLab by @warsaw on Jan 2, 2018, 13:25

The ResourceReader APIs only take a single resource argument, but this is incompatible with the current implementation of zipimporter. The problem is that, unlike with file-based modules, all modules imported from a zip file share the same loader:

>>> import test.test_importlib.zipdata02
>>> import sys, os
>>> sys.path.insert(0, os.path.join(os.path.dirname(test.test_importlib.zipdata02.__file__), 'ziptestdata.zip'))
>>> import ziptestdata.two
>>> import ziptestdata.one
>>> ziptestdata.one.__spec__.loader == ziptestdata.two.__spec__.loader
True

So let's say you have a resource called myresource.txt in ziptestdata.two and ziptestdata.one. The implementation of ResourceReader.open_resource() on the zipimporter's loader can't possibly know which myresource.txt file is being requested.

@brettcannon mentioned PEP 302's design decision not to require a unique loader instance per module, and wanted to move away from that, but I think we're stuck with requiring a package argument to the ResourceReader. The only other option would be to change zipimporter to provide a unique loader instance per module, but that seems like a backward compatibility break. Besides, if other 3rd party loaders follow the same implementation of a shared loader, they won't be able to implement ResourceReader either.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions