summaryrefslogtreecommitdiffstats
path: root/Lib/importlib
diff options
context:
space:
mode:
authorBrett Cannon <brettcannon@users.noreply.github.com>2018-04-30 18:31:45 (GMT)
committerGitHub <noreply@github.com>2018-04-30 18:31:45 (GMT)
commit3ab9365dca8438f89b2060cd3eebe00606133dc4 (patch)
tree7844d510039f747edd6ed5cb2b1e60977c20a30a /Lib/importlib
parent10f715d71218ece737f990fa55027b8e1120cc3a (diff)
downloadcpython-3ab9365dca8438f89b2060cd3eebe00606133dc4.zip
cpython-3ab9365dca8438f89b2060cd3eebe00606133dc4.tar.gz
cpython-3ab9365dca8438f89b2060cd3eebe00606133dc4.tar.bz2
bpo-33254: do not return an empty list when asking for the contents of a namespace package (GH-6467)
Diffstat (limited to 'Lib/importlib')
-rw-r--r--Lib/importlib/abc.py4
-rw-r--r--Lib/importlib/resources.py21
2 files changed, 12 insertions, 13 deletions
diff --git a/Lib/importlib/abc.py b/Lib/importlib/abc.py
index d2f4509..dbdd5bf 100644
--- a/Lib/importlib/abc.py
+++ b/Lib/importlib/abc.py
@@ -381,8 +381,8 @@ class ResourceReader(metaclass=abc.ABCMeta):
@abc.abstractmethod
def contents(self):
- """Return an iterator of strings over the contents of the package."""
- return iter([])
+ """Return an iterable of strings over the contents of the package."""
+ return []
_register(ResourceReader, machinery.SourceFileLoader)
diff --git a/Lib/importlib/resources.py b/Lib/importlib/resources.py
index e565427..4d70186 100644
--- a/Lib/importlib/resources.py
+++ b/Lib/importlib/resources.py
@@ -9,7 +9,7 @@ from importlib.abc import ResourceLoader
from io import BytesIO, TextIOWrapper
from pathlib import Path
from types import ModuleType
-from typing import Iterator, Optional, Set, Union # noqa: F401
+from typing import Iterable, Iterator, Optional, Set, Union # noqa: F401
from typing import cast
from typing.io import BinaryIO, TextIO
from zipimport import ZipImportError
@@ -44,8 +44,7 @@ def _normalize_path(path) -> str:
If the resulting string contains path separators, an exception is raised.
"""
- str_path = str(path)
- parent, file_name = os.path.split(str_path)
+ parent, file_name = os.path.split(path)
if parent:
raise ValueError('{!r} must be only a file name'.format(path))
else:
@@ -228,8 +227,8 @@ def is_resource(package: Package, name: str) -> bool:
return path.is_file()
-def contents(package: Package) -> Iterator[str]:
- """Return the list of entries in 'package'.
+def contents(package: Package) -> Iterable[str]:
+ """Return an iterable of entries in 'package'.
Note that not all entries are resources. Specifically, directories are
not considered resources. Use `is_resource()` on each entry returned here
@@ -238,15 +237,15 @@ def contents(package: Package) -> Iterator[str]:
package = _get_package(package)
reader = _get_resource_reader(package)
if reader is not None:
- yield from reader.contents()
- return
+ return reader.contents()
# Is the package a namespace package? By definition, namespace packages
# cannot have resources. We could use _check_location() and catch the
# exception, but that's extra work, so just inline the check.
- if package.__spec__.origin is None or not package.__spec__.has_location:
- return []
- package_directory = Path(package.__spec__.origin).parent
- yield from os.listdir(str(package_directory))
+ elif package.__spec__.origin is None or not package.__spec__.has_location:
+ return ()
+ else:
+ package_directory = Path(package.__spec__.origin).parent
+ return os.listdir(package_directory)
# Private implementation of ResourceReader and get_resource_reader() for