Skip to content

bpo-32248 - Implement ResourceReader and get_resource_reader() for zipimport #5248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Jan 24, 2018

Conversation

warsaw
Copy link
Member

@warsaw warsaw commented Jan 20, 2018

Here's the implementation of ResourceReader for zipimport. This also removes the fallback hacks for zip files, which we'll no longer need.

Before you complain about the trampoline from zipimport's get_resource_reader() into a private function in importlib.resources, @brettcannon and I discussed this and agreed that it's an acceptable hack. See the comments in the .py files for more details.

https://bugs.python.org/issue32248

@warsaw warsaw requested a review from a team January 20, 2018 00:24
@warsaw warsaw changed the title Implement ResourceReader and get_resource_reader() for zipimport bpo32248 - Implement ResourceReader and get_resource_reader() for zipimport Jan 20, 2018
@warsaw warsaw self-assigned this Jan 20, 2018
@warsaw warsaw changed the title bpo32248 - Implement ResourceReader and get_resource_reader() for zipimport Implement ResourceReader and get_resource_reader() for zipimport Jan 20, 2018
@warsaw warsaw changed the title Implement ResourceReader and get_resource_reader() for zipimport bpo-32248 - Implement ResourceReader and get_resource_reader() for zipimport Jan 20, 2018
@@ -1,7 +1,8 @@
Add :class:`importlib.abc.ResourceReader` as an ABC to provide a
unified API for reading resources contained within packages. Loaders
wishing to support resource reading are expected to implement the
``get_resource_reader(fullname)`` method.
``get_resource_reader(fullname)`` method. File-based and zipimport-based
loaders both implement these APIs.

Also add :mod:`importlib.resources` as the stdlib port of the
``importlib_resources`` PyPI package. The modules provides a high-level
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very long NEWS entry that looks like it should probably be in What's New :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right :). I'll shorten the NEWS entry and expand a bit in the What's New entry.

self.fullname = fullname

def open_resource(self, resource):
path = f'{self.fullname}/{resource}'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does zip importer require the forward slash? Or does it expect os.sep? (You're using pathlib below on what looks like it's probably the same style of path, but without digging further I'm not 100% sure.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure either, but AFAICT, the paths inside the zip all have forward slashes.

I suppose that could pose a problem with using pathlib on Windows, but given Appveyor passing, I think we can trust that it isn't a problem in practice, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is pathlib.PurePath.as_posix() to guarantee the forward slashes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't os.fspath() be called on resource? And is there any worry of people accidentally passing in a subdirectory since there is no argument check?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to do those things since people will be accessing these through the higher level API (I.e. importlib.resources). I think the validation done at that level means we can trust what gets passed to us. I also don't think we need to force forward slashes in the argument passed to self.zipimporter.get_data() since we're using forward slash in the f-string and zips only support forward slashes in their internal file names.

if (function == NULL) {
return NULL;
}
PyObject *retval = PyObject_CallFunction(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PyObject_CallMethod can save you a GetAttrString call :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, trampolining into Python from C is my favourite feature"hack".

@warsaw
Copy link
Member Author

warsaw commented Jan 24, 2018

Thanks for the review @zooba !

Copy link
Member

@brettcannon brettcannon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR LGTM, although I have some questions about potential parameter checking.

self.fullname = fullname

def open_resource(self, resource):
path = f'{self.fullname}/{resource}'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is pathlib.PurePath.as_posix() to guarantee the forward slashes.

self.fullname = fullname

def open_resource(self, resource):
path = f'{self.fullname}/{resource}'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't os.fspath() be called on resource? And is there any worry of people accidentally passing in a subdirectory since there is no argument check?

@warsaw
Copy link
Member Author

warsaw commented Jan 24, 2018

@brettcannon Thanks for the review!

@warsaw warsaw merged commit 6f6eb35 into python:master Jan 24, 2018
@warsaw warsaw deleted the zipresources branch January 24, 2018 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants