24
24
25
25
26
26
def _get_package (package ) -> ModuleType :
27
+ """Take a package name or module object and return the module.
28
+
29
+ If a name, the module is imported. If the passed or imported module
30
+ object is not a package, raise an exception.
31
+ """
27
32
if hasattr (package , '__spec__' ):
28
33
if package .__spec__ .submodule_search_locations is None :
29
34
raise TypeError ('{!r} is not a package' .format (
@@ -39,6 +44,10 @@ def _get_package(package) -> ModuleType:
39
44
40
45
41
46
def _normalize_path (path ) -> str :
47
+ """Normalize a path by ensuring it is a string.
48
+
49
+ If the resulting string contains path separators, an exception is raised.
50
+ """
42
51
str_path = str (path )
43
52
parent , file_name = os .path .split (str_path )
44
53
if parent :
@@ -54,9 +63,11 @@ def _get_resource_reader(
54
63
# hook wants to create a weak reference to the object, but
55
64
# zipimport.zipimporter does not support weak references, resulting in a
56
65
# TypeError. That seems terrible.
57
- if hasattr (package .__spec__ .loader , 'open_resource' ):
58
- return cast (resources_abc .ResourceReader , package .__spec__ .loader )
59
- return None
66
+ spec = package .__spec__
67
+ reader = getattr (spec .loader , 'get_resource_reader' , None )
68
+ if reader is None :
69
+ return None
70
+ return cast (resources_abc .ResourceReader , reader (spec .name ))
60
71
61
72
62
73
def open_binary (package : Package , resource : Resource ) -> BinaryIO :
@@ -73,14 +84,14 @@ def open_binary(package: Package, resource: Resource) -> BinaryIO:
73
84
full_path = os .path .join (package_path , resource )
74
85
try :
75
86
return builtins_open (full_path , mode = 'rb' )
76
- except IOError :
87
+ except OSError :
77
88
# Just assume the loader is a resource loader; all the relevant
78
89
# importlib.machinery loaders are and an AttributeError for
79
90
# get_data() will make it clear what is needed from the loader.
80
91
loader = cast (ResourceLoader , package .__spec__ .loader )
81
92
data = None
82
93
if hasattr (package .__spec__ .loader , 'get_data' ):
83
- with suppress (IOError ):
94
+ with suppress (OSError ):
84
95
data = loader .get_data (full_path )
85
96
if data is None :
86
97
package_name = package .__spec__ .name
@@ -109,14 +120,14 @@ def open_text(package: Package,
109
120
try :
110
121
return builtins_open (
111
122
full_path , mode = 'r' , encoding = encoding , errors = errors )
112
- except IOError :
123
+ except OSError :
113
124
# Just assume the loader is a resource loader; all the relevant
114
125
# importlib.machinery loaders are and an AttributeError for
115
126
# get_data() will make it clear what is needed from the loader.
116
127
loader = cast (ResourceLoader , package .__spec__ .loader )
117
128
data = None
118
129
if hasattr (package .__spec__ .loader , 'get_data' ):
119
- with suppress (IOError ):
130
+ with suppress (OSError ):
120
131
data = loader .get_data (full_path )
121
132
if data is None :
122
133
package_name = package .__spec__ .name
@@ -173,7 +184,7 @@ def path(package: Package, resource: Resource) -> Iterator[Path]:
173
184
# resource_path() raises FileNotFoundError.
174
185
package_directory = Path (package .__spec__ .origin ).parent
175
186
file_path = package_directory / resource
176
- if file_path .exists (): # pragma: FIXME
187
+ if file_path .exists ():
177
188
yield file_path
178
189
else :
179
190
with open_binary (package , resource ) as fp :
@@ -213,10 +224,10 @@ def is_resource(package: Package, name: str) -> bool:
213
224
# contents doesn't necessarily mean it's a resource. Directories are not
214
225
# resources, so let's try to find out if it's a directory or not.
215
226
path = Path (package .__spec__ .origin ).parent / name
216
- if path .is_file (): # pragma: FIXME
227
+ if path .is_file ():
217
228
return True
218
229
if path .is_dir ():
219
- return False # pragma: FIXME
230
+ return False
220
231
# If it's not a file and it's not a directory, what is it? Well, this
221
232
# means the file doesn't exist on the file system, so it probably lives
222
233
# inside a zip file. We have to crack open the zip, look at its table of
0 commit comments