Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9b52920

Browse files
authored
bpo-46125: Refactor tests to test traversable API directly. Includes changes from importlib_resources 5.4.0. (GH-30189)
1 parent fe68486 commit 9b52920

11 files changed

Lines changed: 154 additions & 213 deletions

File tree

Lib/importlib/_common.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
import types
77
import importlib
88

9-
from typing import Union, Any, Optional
9+
from typing import Union, Optional
1010
from .abc import ResourceReader, Traversable
1111

1212
from ._adapters import wrap_spec
1313

1414
Package = Union[types.ModuleType, str]
15-
Resource = Union[str, os.PathLike]
1615

1716

1817
def files(package):
@@ -23,19 +22,6 @@ def files(package):
2322
return from_package(get_package(package))
2423

2524

26-
def normalize_path(path):
27-
# type: (Any) -> str
28-
"""Normalize a path by ensuring it is a string.
29-
30-
If the resulting string contains path separators, an exception is raised.
31-
"""
32-
str_path = str(path)
33-
parent, file_name = os.path.split(str_path)
34-
if parent:
35-
raise ValueError(f'{path!r} must be only a file name')
36-
return file_name
37-
38-
3925
def get_resource_reader(package):
4026
# type: (types.ModuleType) -> Optional[ResourceReader]
4127
"""

Lib/importlib/_legacy.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import types
55
import warnings
66

7-
from typing import Union, Iterable, ContextManager, BinaryIO, TextIO
7+
from typing import Union, Iterable, ContextManager, BinaryIO, TextIO, Any
88

99
from . import _common
1010

1111
Package = Union[types.ModuleType, str]
12-
Resource = Union[str, os.PathLike]
12+
Resource = str
1313

1414

1515
def deprecated(func):
@@ -27,16 +27,29 @@ def wrapper(*args, **kwargs):
2727
return wrapper
2828

2929

30+
def normalize_path(path):
31+
# type: (Any) -> str
32+
"""Normalize a path by ensuring it is a string.
33+
34+
If the resulting string contains path separators, an exception is raised.
35+
"""
36+
str_path = str(path)
37+
parent, file_name = os.path.split(str_path)
38+
if parent:
39+
raise ValueError(f'{path!r} must be only a file name')
40+
return file_name
41+
42+
3043
@deprecated
3144
def open_binary(package: Package, resource: Resource) -> BinaryIO:
3245
"""Return a file-like object opened for binary reading of the resource."""
33-
return (_common.files(package) / _common.normalize_path(resource)).open('rb')
46+
return (_common.files(package) / normalize_path(resource)).open('rb')
3447

3548

3649
@deprecated
3750
def read_binary(package: Package, resource: Resource) -> bytes:
3851
"""Return the binary contents of the resource."""
39-
return (_common.files(package) / _common.normalize_path(resource)).read_bytes()
52+
return (_common.files(package) / normalize_path(resource)).read_bytes()
4053

4154

4255
@deprecated
@@ -47,7 +60,7 @@ def open_text(
4760
errors: str = 'strict',
4861
) -> TextIO:
4962
"""Return a file-like object opened for text reading of the resource."""
50-
return (_common.files(package) / _common.normalize_path(resource)).open(
63+
return (_common.files(package) / normalize_path(resource)).open(
5164
'r', encoding=encoding, errors=errors
5265
)
5366

@@ -85,7 +98,7 @@ def is_resource(package: Package, name: str) -> bool:
8598
8699
Directories are *not* resources.
87100
"""
88-
resource = _common.normalize_path(name)
101+
resource = normalize_path(name)
89102
return any(
90103
traversable.name == resource and traversable.is_file()
91104
for traversable in _common.files(package).iterdir()
@@ -105,4 +118,4 @@ def path(
105118
raised if the file was deleted prior to the context manager
106119
exiting).
107120
"""
108-
return _common.as_file(_common.files(package) / _common.normalize_path(resource))
121+
return _common.as_file(_common.files(package) / normalize_path(resource))

Lib/importlib/abc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ def read_text(self, encoding=None):
381381
@abc.abstractmethod
382382
def is_dir(self) -> bool:
383383
"""
384-
Return True if self is a dir
384+
Return True if self is a directory
385385
"""
386386

387387
@abc.abstractmethod

Lib/importlib/resources.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
as_file,
55
files,
66
Package,
7-
Resource,
87
)
98

109
from ._legacy import (
@@ -15,6 +14,7 @@
1514
read_text,
1615
is_resource,
1716
path,
17+
Resource,
1818
)
1919

2020
from importlib.abc import ResourceReader

Lib/test/test_importlib/resources/util.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import abc
2-
import contextlib
32
import importlib
43
import io
54
import sys
65
import types
7-
import warnings
86
from pathlib import Path, PurePath
97

108
from .. import data01
@@ -69,13 +67,6 @@ def create_package(file=None, path=None, is_package=True, contents=()):
6967
)
7068

7169

72-
@contextlib.contextmanager
73-
def suppress_known_deprecation():
74-
with warnings.catch_warnings(record=True) as ctx:
75-
warnings.simplefilter('default', category=DeprecationWarning)
76-
yield ctx
77-
78-
7970
class CommonTests(metaclass=abc.ABCMeta):
8071
"""
8172
Tests shared by test_open, test_path, and test_read.
@@ -106,18 +97,6 @@ def test_pathlib_path(self):
10697
path = PurePath('utf-8.file')
10798
self.execute(data01, path)
10899

109-
def test_absolute_path(self):
110-
# An absolute path is a ValueError.
111-
path = Path(__file__)
112-
full_path = path.parent / 'utf-8.file'
113-
with self.assertRaises(ValueError):
114-
self.execute(data01, full_path)
115-
116-
def test_relative_path(self):
117-
# A reative path is a ValueError.
118-
with self.assertRaises(ValueError):
119-
self.execute(data01, '../data01/utf-8.file')
120-
121100
def test_importing_module_as_side_effect(self):
122101
# The anchor package can already be imported.
123102
del sys.modules[data01.__name__]

Lib/test/test_importlib/test_contents.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class ContentsTests:
1515
}
1616

1717
def test_contents(self):
18-
with util.suppress_known_deprecation():
19-
assert self.expected <= set(resources.contents(self.data))
18+
contents = {path.name for path in resources.files(self.data).iterdir()}
19+
assert self.expected <= contents
2020

2121

2222
class ContentsDiskTests(ContentsTests, unittest.TestCase):

Lib/test/test_importlib/test_open.py

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,44 @@
77

88
class CommonBinaryTests(util.CommonTests, unittest.TestCase):
99
def execute(self, package, path):
10-
with util.suppress_known_deprecation():
11-
with resources.open_binary(package, path):
12-
pass
10+
target = resources.files(package).joinpath(path)
11+
with target.open('rb'):
12+
pass
1313

1414

1515
class CommonTextTests(util.CommonTests, unittest.TestCase):
1616
def execute(self, package, path):
17-
with util.suppress_known_deprecation():
18-
with resources.open_text(package, path):
19-
pass
17+
target = resources.files(package).joinpath(path)
18+
with target.open():
19+
pass
2020

2121

2222
class OpenTests:
2323
def test_open_binary(self):
24-
with util.suppress_known_deprecation():
25-
with resources.open_binary(self.data, 'binary.file') as fp:
26-
result = fp.read()
27-
self.assertEqual(result, b'\x00\x01\x02\x03')
24+
target = resources.files(self.data) / 'binary.file'
25+
with target.open('rb') as fp:
26+
result = fp.read()
27+
self.assertEqual(result, b'\x00\x01\x02\x03')
2828

2929
def test_open_text_default_encoding(self):
30-
with util.suppress_known_deprecation():
31-
with resources.open_text(self.data, 'utf-8.file') as fp:
32-
result = fp.read()
30+
target = resources.files(self.data) / 'utf-8.file'
31+
with target.open() as fp:
32+
result = fp.read()
3333
self.assertEqual(result, 'Hello, UTF-8 world!\n')
3434

3535
def test_open_text_given_encoding(self):
36-
with util.suppress_known_deprecation():
37-
with resources.open_text(
38-
self.data, 'utf-16.file', 'utf-16', 'strict'
39-
) as fp:
40-
result = fp.read()
36+
target = resources.files(self.data) / 'utf-16.file'
37+
with target.open(encoding='utf-16', errors='strict') as fp:
38+
result = fp.read()
4139
self.assertEqual(result, 'Hello, UTF-16 world!\n')
4240

4341
def test_open_text_with_errors(self):
4442
# Raises UnicodeError without the 'errors' argument.
45-
with util.suppress_known_deprecation():
46-
with resources.open_text(self.data, 'utf-16.file', 'utf-8', 'strict') as fp:
47-
self.assertRaises(UnicodeError, fp.read)
48-
with util.suppress_known_deprecation():
49-
with resources.open_text(self.data, 'utf-16.file', 'utf-8', 'ignore') as fp:
50-
result = fp.read()
43+
target = resources.files(self.data) / 'utf-16.file'
44+
with target.open(encoding='utf-8', errors='strict') as fp:
45+
self.assertRaises(UnicodeError, fp.read)
46+
with target.open(encoding='utf-8', errors='ignore') as fp:
47+
result = fp.read()
5148
self.assertEqual(
5249
result,
5350
'H\x00e\x00l\x00l\x00o\x00,\x00 '
@@ -56,16 +53,12 @@ def test_open_text_with_errors(self):
5653
)
5754

5855
def test_open_binary_FileNotFoundError(self):
59-
with util.suppress_known_deprecation():
60-
self.assertRaises(
61-
FileNotFoundError, resources.open_binary, self.data, 'does-not-exist'
62-
)
56+
target = resources.files(self.data) / 'does-not-exist'
57+
self.assertRaises(FileNotFoundError, target.open, 'rb')
6358

6459
def test_open_text_FileNotFoundError(self):
65-
with util.suppress_known_deprecation():
66-
self.assertRaises(
67-
FileNotFoundError, resources.open_text, self.data, 'does-not-exist'
68-
)
60+
target = resources.files(self.data) / 'does-not-exist'
61+
self.assertRaises(FileNotFoundError, target.open)
6962

7063

7164
class OpenDiskTests(OpenTests, unittest.TestCase):

Lib/test/test_importlib/test_path.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,22 @@
88

99
class CommonTests(util.CommonTests, unittest.TestCase):
1010
def execute(self, package, path):
11-
with util.suppress_known_deprecation():
12-
with resources.path(package, path):
13-
pass
11+
with resources.as_file(resources.files(package).joinpath(path)):
12+
pass
1413

1514

1615
class PathTests:
1716
def test_reading(self):
1817
# Path should be readable.
1918
# Test also implicitly verifies the returned object is a pathlib.Path
2019
# instance.
21-
with util.suppress_known_deprecation():
22-
with resources.path(self.data, 'utf-8.file') as path:
23-
self.assertTrue(path.name.endswith("utf-8.file"), repr(path))
24-
# pathlib.Path.read_text() was introduced in Python 3.5.
25-
with path.open('r', encoding='utf-8') as file:
26-
text = file.read()
27-
self.assertEqual('Hello, UTF-8 world!\n', text)
20+
target = resources.files(self.data) / 'utf-8.file'
21+
with resources.as_file(target) as path:
22+
self.assertTrue(path.name.endswith("utf-8.file"), repr(path))
23+
# pathlib.Path.read_text() was introduced in Python 3.5.
24+
with path.open('r', encoding='utf-8') as file:
25+
text = file.read()
26+
self.assertEqual('Hello, UTF-8 world!\n', text)
2827

2928

3029
class PathDiskTests(PathTests, unittest.TestCase):
@@ -34,9 +33,9 @@ def test_natural_path(self):
3433
# Guarantee the internal implementation detail that
3534
# file-system-backed resources do not get the tempdir
3635
# treatment.
37-
with util.suppress_known_deprecation():
38-
with resources.path(self.data, 'utf-8.file') as path:
39-
assert 'data' in str(path)
36+
target = resources.files(self.data) / 'utf-8.file'
37+
with resources.as_file(target) as path:
38+
assert 'data' in str(path)
4039

4140

4241
class PathMemoryTests(PathTests, unittest.TestCase):
@@ -54,9 +53,9 @@ class PathZipTests(PathTests, util.ZipSetup, unittest.TestCase):
5453
def test_remove_in_context_manager(self):
5554
# It is not an error if the file that was temporarily stashed on the
5655
# file system is removed inside the `with` stanza.
57-
with util.suppress_known_deprecation():
58-
with resources.path(self.data, 'utf-8.file') as path:
59-
path.unlink()
56+
target = resources.files(self.data) / 'utf-8.file'
57+
with resources.as_file(target) as path:
58+
path.unlink()
6059

6160

6261
if __name__ == '__main__':

0 commit comments

Comments
 (0)