@@ -76,7 +76,8 @@ def open_binary(package: Package, resource: Resource) -> BinaryIO:
76
76
return reader .open_resource (resource )
77
77
# Using pathlib doesn't work well here due to the lack of 'strict'
78
78
# argument for pathlib.Path.resolve() prior to Python 3.6.
79
- absolute_package_path = os .path .abspath (package .__spec__ .origin )
79
+ absolute_package_path = os .path .abspath (
80
+ package .__spec__ .origin or 'non-existent file' )
80
81
package_path = os .path .dirname (absolute_package_path )
81
82
full_path = os .path .join (package_path , resource )
82
83
try :
@@ -184,10 +185,7 @@ def is_resource(package: Package, name: str) -> bool:
184
185
reader = _get_resource_reader (package )
185
186
if reader is not None :
186
187
return reader .is_resource (name )
187
- try :
188
- package_contents = set (contents (package ))
189
- except (NotADirectoryError , FileNotFoundError ):
190
- return False
188
+ package_contents = set (contents (package ))
191
189
if name not in package_contents :
192
190
return False
193
191
# Just because the given file_name lives as an entry in the package's
@@ -241,17 +239,18 @@ def contents(package: Package) -> Iterable[str]:
241
239
return reader .contents ()
242
240
# Is the package a namespace package? By definition, namespace packages
243
241
# cannot have resources.
244
- if (package .__spec__ .origin == 'namespace' and
245
- not package .__spec__ .has_location ):
242
+ namespace = (
243
+ package .__spec__ .origin is None or
244
+ package .__spec__ .origin == 'namespace'
245
+ )
246
+ if namespace or not package .__spec__ .has_location :
246
247
return ()
247
248
package_directory = Path (package .__spec__ .origin ).parent
248
249
try :
249
250
return os .listdir (str (package_directory ))
250
251
except (NotADirectoryError , FileNotFoundError ):
251
252
# The package is probably in a zip file.
252
253
archive_path = getattr (package .__spec__ .loader , 'archive' , None )
253
- if archive_path is None :
254
- raise
255
254
relpath = package_directory .relative_to (archive_path )
256
255
with ZipFile (archive_path ) as zf :
257
256
toc = zf .namelist ()
0 commit comments