Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
6 changes: 3 additions & 3 deletions Lib/importlib/resources/_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class SpecLoaderAdapter:
Adapt a package spec to adapt the underlying loader.
"""

def __init__(self, spec, adapter=lambda spec: spec.loader):
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was the default adapter removed? Isn't this a breaking change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the simplicity sake, because it seems not to be used anywhere.
This default value was defined since this mechanism was introduced in #24670 but it was not used even in that commit.

Copy link
Contributor

Choose a reason for hiding this comment

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

As it does not pertain to the issue that's addressed in this PR I'd recommend reverting this change anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, got it 👍

def __init__(self, spec, adapter):
self.spec = spec
self.loader = adapter(spec)

Expand Down 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