3
3
4
4
from test .support .warnings_helper import ignore_warnings , check_warnings
5
5
6
- import importlib . resources
6
+ import importlib_resources as resources
7
7
8
8
# Since the functional API forwards to Traversable, we only test
9
9
# filesystem resources here -- not zip files, namespace packages etc.
10
10
# We do test for two kinds of Anchor, though.
11
11
12
12
13
13
class StringAnchorMixin :
14
- anchor01 = 'test.test_importlib.resources .data01'
15
- anchor02 = 'test.test_importlib.resources .data02'
14
+ anchor01 = 'importlib_resources.tests .data01'
15
+ anchor02 = 'importlib_resources.tests .data02'
16
16
17
17
18
18
class ModuleAnchorMixin :
19
- from test . test_importlib . resources import data01 as anchor01
20
- from test . test_importlib . resources import data02 as anchor02
19
+ from . import data01 as anchor01
20
+ from . import data02 as anchor02
21
21
22
22
23
23
class FunctionalAPIBase ():
@@ -34,39 +34,39 @@ def _gen_resourcetxt_path_parts(self):
34
34
35
35
def test_read_text (self ):
36
36
self .assertEqual (
37
- importlib . resources .read_text (self .anchor01 , 'utf-8.file' ),
37
+ resources .read_text (self .anchor01 , 'utf-8.file' ),
38
38
'Hello, UTF-8 world!\n ' ,
39
39
)
40
40
self .assertEqual (
41
- importlib . resources .read_text (
41
+ resources .read_text (
42
42
self .anchor02 , 'subdirectory' , 'subsubdir' , 'resource.txt' ,
43
43
encoding = 'utf-8' ,
44
44
),
45
45
'a resource' ,
46
46
)
47
47
for path_parts in self ._gen_resourcetxt_path_parts ():
48
48
self .assertEqual (
49
- importlib . resources .read_text (
49
+ resources .read_text (
50
50
self .anchor02 , * path_parts , encoding = 'utf-8' ,
51
51
),
52
52
'a resource' ,
53
53
)
54
54
# Use generic OSError, since e.g. attempting to read a directory can
55
55
# fail with PermissionError rather than IsADirectoryError
56
56
with self .assertRaises (OSError ):
57
- importlib . resources .read_text (self .anchor01 )
57
+ resources .read_text (self .anchor01 )
58
58
with self .assertRaises (OSError ):
59
- importlib . resources .read_text (self .anchor01 , 'no-such-file' )
59
+ resources .read_text (self .anchor01 , 'no-such-file' )
60
60
with self .assertRaises (UnicodeDecodeError ):
61
- importlib . resources .read_text (self .anchor01 , 'utf-16.file' )
61
+ resources .read_text (self .anchor01 , 'utf-16.file' )
62
62
self .assertEqual (
63
- importlib . resources .read_text (
63
+ resources .read_text (
64
64
self .anchor01 , 'binary.file' , encoding = 'latin1' ,
65
65
),
66
66
'\x00 \x01 \x02 \x03 ' ,
67
67
)
68
68
self .assertEqual (
69
- importlib . resources .read_text (
69
+ resources .read_text (
70
70
self .anchor01 , 'utf-16.file' ,
71
71
errors = 'backslashreplace' ,
72
72
),
@@ -77,38 +77,38 @@ def test_read_text(self):
77
77
78
78
def test_read_binary (self ):
79
79
self .assertEqual (
80
- importlib . resources .read_binary (self .anchor01 , 'utf-8.file' ),
80
+ resources .read_binary (self .anchor01 , 'utf-8.file' ),
81
81
b'Hello, UTF-8 world!\n ' ,
82
82
)
83
83
for path_parts in self ._gen_resourcetxt_path_parts ():
84
84
self .assertEqual (
85
- importlib . resources .read_binary (self .anchor02 , * path_parts ),
85
+ resources .read_binary (self .anchor02 , * path_parts ),
86
86
b'a resource' ,
87
87
)
88
88
89
89
def test_open_text (self ):
90
- with importlib . resources .open_text (self .anchor01 , 'utf-8.file' ) as f :
90
+ with resources .open_text (self .anchor01 , 'utf-8.file' ) as f :
91
91
self .assertEqual (f .read (), 'Hello, UTF-8 world!\n ' )
92
92
for path_parts in self ._gen_resourcetxt_path_parts ():
93
- with importlib . resources .open_text (
93
+ with resources .open_text (
94
94
self .anchor02 , * path_parts ,
95
95
encoding = 'utf-8' ,
96
96
) as f :
97
97
self .assertEqual (f .read (), 'a resource' )
98
98
# Use generic OSError, since e.g. attempting to read a directory can
99
99
# fail with PermissionError rather than IsADirectoryError
100
100
with self .assertRaises (OSError ):
101
- importlib . resources .open_text (self .anchor01 )
101
+ resources .open_text (self .anchor01 )
102
102
with self .assertRaises (OSError ):
103
- importlib . resources .open_text (self .anchor01 , 'no-such-file' )
104
- with importlib . resources .open_text (self .anchor01 , 'utf-16.file' ) as f :
103
+ resources .open_text (self .anchor01 , 'no-such-file' )
104
+ with resources .open_text (self .anchor01 , 'utf-16.file' ) as f :
105
105
with self .assertRaises (UnicodeDecodeError ):
106
106
f .read ()
107
- with importlib . resources .open_text (
107
+ with resources .open_text (
108
108
self .anchor01 , 'binary.file' , encoding = 'latin1' ,
109
109
) as f :
110
110
self .assertEqual (f .read (), '\x00 \x01 \x02 \x03 ' )
111
- with importlib . resources .open_text (
111
+ with resources .open_text (
112
112
self .anchor01 , 'utf-16.file' ,
113
113
errors = 'backslashreplace' ,
114
114
) as f :
@@ -120,24 +120,24 @@ def test_open_text(self):
120
120
)
121
121
122
122
def test_open_binary (self ):
123
- with importlib . resources .open_binary (self .anchor01 , 'utf-8.file' ) as f :
123
+ with resources .open_binary (self .anchor01 , 'utf-8.file' ) as f :
124
124
self .assertEqual (f .read (), b'Hello, UTF-8 world!\n ' )
125
125
for path_parts in self ._gen_resourcetxt_path_parts ():
126
- with importlib . resources .open_binary (
126
+ with resources .open_binary (
127
127
self .anchor02 , * path_parts ,
128
128
) as f :
129
129
self .assertEqual (f .read (), b'a resource' )
130
130
131
131
def test_path (self ):
132
- with importlib . resources .path (self .anchor01 , 'utf-8.file' ) as path :
133
- with open (str (path )) as f :
132
+ with resources .path (self .anchor01 , 'utf-8.file' ) as path :
133
+ with open (str (path ), encoding = 'utf-8' ) as f :
134
134
self .assertEqual (f .read (), 'Hello, UTF-8 world!\n ' )
135
- with importlib . resources .path (self .anchor01 ) as path :
136
- with open (os .path .join (path , 'utf-8.file' )) as f :
135
+ with resources .path (self .anchor01 ) as path :
136
+ with open (os .path .join (path , 'utf-8.file' ), encoding = 'utf-8' ) as f :
137
137
self .assertEqual (f .read (), 'Hello, UTF-8 world!\n ' )
138
138
139
139
def test_is_resource (self ):
140
- is_resource = importlib . resources .is_resource
140
+ is_resource = resources .is_resource
141
141
self .assertTrue (is_resource (self .anchor01 , 'utf-8.file' ))
142
142
self .assertFalse (is_resource (self .anchor01 , 'no_such_file' ))
143
143
self .assertFalse (is_resource (self .anchor01 ))
@@ -146,24 +146,25 @@ def test_is_resource(self):
146
146
self .assertTrue (is_resource (self .anchor02 , * path_parts ))
147
147
148
148
def test_contents (self ):
149
- is_resource = importlib .resources .is_resource
150
149
with check_warnings ((".*contents.*" , DeprecationWarning )):
151
- c = importlib . resources .contents (self .anchor01 )
150
+ c = resources .contents (self .anchor01 )
152
151
self .assertGreaterEqual (
153
152
set (c ),
154
153
{'utf-8.file' , 'utf-16.file' , 'binary.file' , 'subdirectory' },
155
154
)
156
- with (self .assertRaises (OSError ),
155
+ with (
156
+ self .assertRaises (OSError ),
157
157
check_warnings ((".*contents.*" , DeprecationWarning )),
158
158
):
159
- importlib . resources .contents (self .anchor01 , 'utf-8.file' )
159
+ list ( resources .contents (self .anchor01 , 'utf-8.file' ) )
160
160
for path_parts in self ._gen_resourcetxt_path_parts ():
161
- with (self .assertRaises (OSError ),
161
+ with (
162
+ self .assertRaises (OSError ),
162
163
check_warnings ((".*contents.*" , DeprecationWarning )),
163
164
):
164
- importlib . resources .contents (self .anchor01 , * path_parts )
165
+ list ( resources .contents (self .anchor01 , * path_parts ) )
165
166
with check_warnings ((".*contents.*" , DeprecationWarning )):
166
- c = importlib . resources .contents (self .anchor01 , 'subdirectory' )
167
+ c = resources .contents (self .anchor01 , 'subdirectory' )
167
168
self .assertGreaterEqual (
168
169
set (c ),
169
170
{'binary.file' },
@@ -172,13 +173,13 @@ def test_contents(self):
172
173
@ignore_warnings (category = DeprecationWarning )
173
174
def test_common_errors (self ):
174
175
for func in (
175
- importlib . resources .read_text ,
176
- importlib . resources .read_binary ,
177
- importlib . resources .open_text ,
178
- importlib . resources .open_binary ,
179
- importlib . resources .path ,
180
- importlib . resources .is_resource ,
181
- importlib . resources .contents ,
176
+ resources .read_text ,
177
+ resources .read_binary ,
178
+ resources .open_text ,
179
+ resources .open_binary ,
180
+ resources .path ,
181
+ resources .is_resource ,
182
+ resources .contents ,
182
183
):
183
184
with self .subTest (func = func ):
184
185
# Rejecting None anchor
@@ -193,8 +194,8 @@ def test_common_errors(self):
193
194
194
195
def test_text_errors (self ):
195
196
for func in (
196
- importlib . resources .read_text ,
197
- importlib . resources .open_text ,
197
+ resources .read_text ,
198
+ resources .open_text ,
198
199
):
199
200
with self .subTest (func = func ):
200
201
# Multiple path arguments need explicit encoding argument.
0 commit comments