Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/importlib/resources/_adapters.py
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
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 because the code used to populate '{package.__name__}' "
"does not correspond directly with an importable module."
)

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
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):
resources.files(mainmodule)


if __name__ == '__main__':
unittest.main()
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