summaryrefslogtreecommitdiffstats
path: root/Lib/importlib
diff options
context:
space:
mode:
authorJason R. Coombs <jaraco@jaraco.com>2021-12-19 02:28:49 (GMT)
committerGitHub <noreply@github.com>2021-12-19 02:28:49 (GMT)
commit9b52920173735ac609664c6a3a3021d24a95a092 (patch)
treeffc7dc7dc01cddc6973bd9cf9e4cb649258365d1 /Lib/importlib
parentfe68486197cb26a69ecce9353271d91adf885cb5 (diff)
downloadcpython-9b52920173735ac609664c6a3a3021d24a95a092.zip
cpython-9b52920173735ac609664c6a3a3021d24a95a092.tar.gz
cpython-9b52920173735ac609664c6a3a3021d24a95a092.tar.bz2
bpo-46125: Refactor tests to test traversable API directly. Includes changes from importlib_resources 5.4.0. (GH-30189)
Diffstat (limited to 'Lib/importlib')
-rw-r--r--Lib/importlib/_common.py16
-rw-r--r--Lib/importlib/_legacy.py27
-rw-r--r--Lib/importlib/abc.py2
-rw-r--r--Lib/importlib/resources.py2
4 files changed, 23 insertions, 24 deletions
diff --git a/Lib/importlib/_common.py b/Lib/importlib/_common.py
index 9b126f3..147ea19 100644
--- a/Lib/importlib/_common.py
+++ b/Lib/importlib/_common.py
@@ -6,13 +6,12 @@ import contextlib
import types
import importlib
-from typing import Union, Any, Optional
+from typing import Union, Optional
from .abc import ResourceReader, Traversable
from ._adapters import wrap_spec
Package = Union[types.ModuleType, str]
-Resource = Union[str, os.PathLike]
def files(package):
@@ -23,19 +22,6 @@ def files(package):
return from_package(get_package(package))
-def normalize_path(path):
- # type: (Any) -> str
- """Normalize a path by ensuring it is a string.
-
- If the resulting string contains path separators, an exception is raised.
- """
- str_path = str(path)
- parent, file_name = os.path.split(str_path)
- if parent:
- raise ValueError(f'{path!r} must be only a file name')
- return file_name
-
-
def get_resource_reader(package):
# type: (types.ModuleType) -> Optional[ResourceReader]
"""
diff --git a/Lib/importlib/_legacy.py b/Lib/importlib/_legacy.py
index 477f89e..1d5d3f1 100644
--- a/Lib/importlib/_legacy.py
+++ b/Lib/importlib/_legacy.py
@@ -4,12 +4,12 @@ import pathlib
import types
import warnings
-from typing import Union, Iterable, ContextManager, BinaryIO, TextIO
+from typing import Union, Iterable, ContextManager, BinaryIO, TextIO, Any
from . import _common
Package = Union[types.ModuleType, str]
-Resource = Union[str, os.PathLike]
+Resource = str
def deprecated(func):
@@ -27,16 +27,29 @@ def deprecated(func):
return wrapper
+def normalize_path(path):
+ # type: (Any) -> str
+ """Normalize a path by ensuring it is a string.
+
+ If the resulting string contains path separators, an exception is raised.
+ """
+ str_path = str(path)
+ parent, file_name = os.path.split(str_path)
+ if parent:
+ raise ValueError(f'{path!r} must be only a file name')
+ return file_name
+
+
@deprecated
def open_binary(package: Package, resource: Resource) -> BinaryIO:
"""Return a file-like object opened for binary reading of the resource."""
- return (_common.files(package) / _common.normalize_path(resource)).open('rb')
+ return (_common.files(package) / normalize_path(resource)).open('rb')
@deprecated
def read_binary(package: Package, resource: Resource) -> bytes:
"""Return the binary contents of the resource."""
- return (_common.files(package) / _common.normalize_path(resource)).read_bytes()
+ return (_common.files(package) / normalize_path(resource)).read_bytes()
@deprecated
@@ -47,7 +60,7 @@ def open_text(
errors: str = 'strict',
) -> TextIO:
"""Return a file-like object opened for text reading of the resource."""
- return (_common.files(package) / _common.normalize_path(resource)).open(
+ return (_common.files(package) / normalize_path(resource)).open(
'r', encoding=encoding, errors=errors
)
@@ -85,7 +98,7 @@ def is_resource(package: Package, name: str) -> bool:
Directories are *not* resources.
"""
- resource = _common.normalize_path(name)
+ resource = normalize_path(name)
return any(
traversable.name == resource and traversable.is_file()
for traversable in _common.files(package).iterdir()
@@ -105,4 +118,4 @@ def path(
raised if the file was deleted prior to the context manager
exiting).
"""
- return _common.as_file(_common.files(package) / _common.normalize_path(resource))
+ return _common.as_file(_common.files(package) / normalize_path(resource))
diff --git a/Lib/importlib/abc.py b/Lib/importlib/abc.py
index 1d6843b..f104416 100644
--- a/Lib/importlib/abc.py
+++ b/Lib/importlib/abc.py
@@ -381,7 +381,7 @@ class Traversable(Protocol):
@abc.abstractmethod
def is_dir(self) -> bool:
"""
- Return True if self is a dir
+ Return True if self is a directory
"""
@abc.abstractmethod
diff --git a/Lib/importlib/resources.py b/Lib/importlib/resources.py
index 6cc4628..5b3bc02 100644
--- a/Lib/importlib/resources.py
+++ b/Lib/importlib/resources.py
@@ -4,7 +4,6 @@ from ._common import (
as_file,
files,
Package,
- Resource,
)
from ._legacy import (
@@ -15,6 +14,7 @@ from ._legacy import (
read_text,
is_resource,
path,
+ Resource,
)
from importlib.abc import ResourceReader