FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

A better error message from importlib.resources.files() when module spec is None by whitphx · Pull Request #331 · python/importlib_resources · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
14 changes: 14 additions & 0 deletions importlib_resources/_common.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ def is_wrapper(frame_info):
return next(callers).frame


def _assert_spec(package: types.ModuleType) -> None:
"""
Provide a nicer error message when package is ``__main__``
and its ``__spec__`` is ``None``
(https://docs.python.org/3/reference/import.html#main-spec).
"""
if package.__spec__ is None:
raise TypeError(
f"Cannot access resources for '{package.__name__}' "
"as it does not appear to correspond to an importable module (its __spec__ is None)."
)


def from_package(package: types.ModuleType):
"""
Return a Traversable object for the given package.
Expand All @@ -79,6 +92,7 @@ def from_package(package: types.ModuleType):
# deferred for performance (python/cpython#109829)
from .future.adapters import wrap_spec

_assert_spec(package)
spec = wrap_spec(package)
reader = spec.loader.get_resource_reader(spec.name)
return reader.files()
Expand Down
20 changes: 20 additions & 0 deletions importlib_resources/tests/test_resource.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import types
import unittest
from importlib import import_module

Expand Down Expand Up @@ -219,5 +220,24 @@ class ResourceFromNamespaceZipTests(
MODULE = 'namespacedata01'


class MainModuleTests(unittest.TestCase):
def test_main_module_with_none_spec(self):
"""
__main__ module with no spec should raise TypeError (for clarity).

See python/cpython#138531 for details.
"""
# construct a __main__ module with no __spec__.
mainmodule = types.ModuleType("__main__")

assert mainmodule.__spec__ is None

with self.assertRaises(
TypeError,
msg="Cannot access resources for '__main__' as it does not appear to correspond to an importable module (its __spec__ is None).",
):
resources.files(mainmodule)


if __name__ == '__main__':
unittest.main()
1 change: 1 addition & 0 deletions newsfragments/331.feature.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``files()`` now provides a nicer error when __main__.__spec__ is None.
Loading

Back | FazBrowse Home | New Git URL