@@ -76,7 +76,8 @@ def open_binary(package: Package, resource: Resource) -> BinaryIO:
7676 return reader .open_resource (resource )
7777 # Using pathlib doesn't work well here due to the lack of 'strict'
7878 # 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' )
8081 package_path = os .path .dirname (absolute_package_path )
8182 full_path = os .path .join (package_path , resource )
8283 try :
@@ -184,10 +185,7 @@ def is_resource(package: Package, name: str) -> bool:
184185 reader = _get_resource_reader (package )
185186 if reader is not None :
186187 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 ))
191189 if name not in package_contents :
192190 return False
193191 # Just because the given file_name lives as an entry in the package's
@@ -241,17 +239,18 @@ def contents(package: Package) -> Iterable[str]:
241239 return reader .contents ()
242240 # Is the package a namespace package? By definition, namespace packages
243241 # 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 :
246247 return ()
247248 package_directory = Path (package .__spec__ .origin ).parent
248249 try :
249250 return os .listdir (str (package_directory ))
250251 except (NotADirectoryError , FileNotFoundError ):
251252 # The package is probably in a zip file.
252253 archive_path = getattr (package .__spec__ .loader , 'archive' , None )
253- if archive_path is None :
254- raise
255254 relpath = package_directory .relative_to (archive_path )
256255 with ZipFile (archive_path ) as zf :
257256 toc = zf .namelist ()
0 commit comments