Skip to content

Commit ce2fa42

Browse files
committed
Merge branch 'feature/run-everywhere' into 'master'
Update package to run on all versions of Python Closes #70 See merge request python-devs/importlib_resources!83
2 parents 6d86ad9 + 6591a31 commit ce2fa42

File tree

3 files changed

+15
-21
lines changed

3 files changed

+15
-21
lines changed

importlib_resources/__init__.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,22 @@
1111
'path',
1212
'read_binary',
1313
'read_text',
14+
'Package',
15+
'Resource',
16+
'ResourceReader',
1417
]
1518

1619

17-
# Use the Python 3.7 stdlib implementation if available.
18-
if sys.version_info >= (3, 7):
19-
from importlib.resources import (
20-
Package, Resource, contents, is_resource, open_binary, open_text, path,
21-
read_binary, read_text)
22-
from importlib.abc import ResourceReader
23-
__all__.extend(['Package', 'Resource', 'ResourceReader'])
24-
elif sys.version_info >= (3,):
20+
if sys.version_info >= (3,):
2521
from importlib_resources._py3 import (
2622
Package, Resource, contents, is_resource, open_binary, open_text, path,
2723
read_binary, read_text)
2824
from importlib_resources.abc import ResourceReader
29-
__all__.extend(['Package', 'Resource', 'ResourceReader'])
3025
else:
3126
from importlib_resources._py2 import (
3227
contents, is_resource, open_binary, open_text, path, read_binary,
3328
read_text)
29+
del __all__[-3:]
3430

3531

3632
__version__ = read_text('importlib_resources', 'version.txt').strip()

importlib_resources/_py3.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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()

tox.ini

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[tox]
2-
# Don't cover Python 3.7 since this is just a shim for that version. Do at
3-
# least make sure we don't regress!
4-
envlist = {py27,py35,py36}{,-cov,-diffcov},{py37,py38},qa,docs
2+
# Coverage is missing on later version of Python
3+
envlist = {py27,py35,py36}{,-cov,-diffcov},py37,py38,qa,docs
54
skip_missing_interpreters = True
65

76

0 commit comments

Comments
 (0)