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

gh-121190: A better error message from `importlib.resources.files()` when module spec is None by whitphx · Pull Request #138531 · python/cpython · GitHub

Repository navigation

4 changes: 2 additions & 2 deletions Lib/importlib/resources/_adapters.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 @@ -160,9 +160,9 @@ def files(self):
return CompatibilityFiles.SpecPath(self.spec, self._reader)


def wrap_spec(package):
def wrap_spec(spec):
"""
Construct a package spec with traversable compatibility
on the spec/loader/reader.
"""
return SpecLoaderAdapter(package.__spec__, TraversableResourcesLoader)
return SpecLoaderAdapter(spec, TraversableResourcesLoader)
8 changes: 7 additions & 1 deletion Lib/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 @@ -113,7 +113,13 @@ def from_package(package: types.ModuleType):
# deferred for performance (python/cpython#109829)
from ._adapters import wrap_spec

spec = wrap_spec(package)
if package.__spec__ is None:
raise TypeError(
f"Cannot access resources for '{package.__name__ or package!r}' "
"as it does not appear to correspond to an importable module (its __spec__ is None)."
)

spec = wrap_spec(package.__spec__)
reader = spec.loader.get_resource_reader(spec.name)
return reader.files()

Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_importlib/resources/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,4 +1,5 @@
import unittest
import types

from . import util
from importlib import resources, import_module
Expand Down Expand Up @@ -232,5 +233,17 @@ class ResourceFromNamespaceZipTests(
MODULE = 'namespacedata01'


class ResourceFromMainModuleWithNoneSpecTests(unittest.TestCase):
# `__main__.__spec__` can be `None` depending on how it is populated.
# https://docs.python.org/3/reference/import.html#main-spec
def test_main_module_with_none_spec(self):
mainmodule = types.ModuleType("__main__")

self.assertIsNone(mainmodule.__spec__)

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()
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 @@
``importlib.resources.files(package)`` raises an error with a clearer message when ``package.__spec__`` is ``None``
Loading

Back | FazBrowse Home | New Git URL