Skip to content
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

importlib: improve bytes handling #9070

Merged
merged 4 commits into from
Nov 3, 2022
Merged

Conversation

JelleZijlstra
Copy link
Member

No description provided.

@@ -38,7 +45,7 @@ class Loader(metaclass=ABCMeta):

class ResourceLoader(Loader):
@abstractmethod
def get_data(self, path: _Path) -> bytes: ...
def get_data(self, path: str) -> bytes: ...
Copy link
Member Author

Choose a reason for hiding this comment

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


class ExecutionLoader(InspectLoader):
@abstractmethod
def get_filename(self, fullname: str) -> _Path: ...
def get_filename(self, fullname: str) -> str: ...
Copy link
Member Author

Choose a reason for hiding this comment

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

Docstring says "Abstract method which should return the value that file is to be set to." __file__ is always a str, not bytes. (Note that _Path = str | bytes.)


class SourceLoader(ResourceLoader, ExecutionLoader, metaclass=ABCMeta):
def path_mtime(self, path: _Path) -> float: ...
def set_data(self, path: _Path, data: bytes) -> None: ...
def path_mtime(self, path: str) -> float: ...
Copy link
Member Author

Choose a reason for hiding this comment

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

Docstrings say the path must be str for all three methods.

@@ -70,17 +77,17 @@ class MetaPathFinder(Finder):

class PathEntryFinder(Finder):
def find_module(self, fullname: str) -> Loader | None: ...
def find_loader(self, fullname: str) -> tuple[Loader | None, Sequence[_Path]]: ...
def find_loader(self, fullname: str) -> tuple[Loader | None, Sequence[str]]: ...
Copy link
Member Author

Choose a reason for hiding this comment

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

The second element is ModuleSpec.submodule_search_locations which is a list[str]. https://github.com/python/cpython/blob/2cfcaf5af602b297fc90086de4d8ac980c7891e2/Lib/importlib/abc.py#L155

def __init__(self, fullname: str, path: _Path) -> None: ...
def get_data(self, path: _Path) -> bytes: ...
def get_filename(self, name: str | None = ...) -> _Path: ...
path: str
Copy link
Member Author

Choose a reason for hiding this comment

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

This class has no implementation, but from _bootstrap_external.FileLoader it seems clear the path is not intended to be bytes: https://github.com/python/cpython/blob/2cfcaf5af602b297fc90086de4d8ac980c7891e2/Lib/importlib/_bootstrap_external.py#L1195

@@ -113,10 +114,10 @@ class PathFinder:

@classmethod
def find_spec(
cls, fullname: str, path: Sequence[bytes | str] | None = ..., target: types.ModuleType | None = ...
cls, fullname: str, path: Sequence[str] | None = ..., target: types.ModuleType | None = ...
Copy link
Member Author

Choose a reason for hiding this comment

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

The path gets passed to _get_spec, which ignores any non-str entries in the path: https://github.com/python/cpython/blob/2cfcaf5af602b297fc90086de4d8ac980c7891e2/Lib/importlib/_bootstrap_external.py#L1525

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@JelleZijlstra JelleZijlstra marked this pull request as ready for review November 3, 2022 04:46
@github-actions
Copy link
Contributor

github-actions bot commented Nov 3, 2022

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@@ -47,40 +52,40 @@ class InspectLoader(Loader):
def get_source(self, fullname: str) -> str | None: ...
def exec_module(self, module: types.ModuleType) -> None: ...
@staticmethod
def source_to_code(data: bytes | str, path: str = ...) -> types.CodeType: ...
def source_to_code(data: ReadableBuffer | str, path: str = ...) -> types.CodeType: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could also be AST, but I don't think that's too important.

@srittau srittau merged commit 4363107 into python:main Nov 3, 2022
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.

2 participants