From 2944a331a9589639c2140f1c1afbdc32d2f9e2fd Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 17 Jan 2020 20:44:41 -0500 Subject: [PATCH 1/4] importlib_resources now runs naturally on all Pythons --- importlib_resources/__init__.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/importlib_resources/__init__.py b/importlib_resources/__init__.py index fab437a4ad934c1..25afed09c6edbb9 100644 --- a/importlib_resources/__init__.py +++ b/importlib_resources/__init__.py @@ -11,26 +11,22 @@ 'path', 'read_binary', 'read_text', + 'Package', + 'Resource', + 'ResourceReader', ] -# Use the Python 3.7 stdlib implementation if available. -if sys.version_info >= (3, 7): - from importlib.resources import ( - Package, Resource, contents, is_resource, open_binary, open_text, path, - read_binary, read_text) - from importlib.abc import ResourceReader - __all__.extend(['Package', 'Resource', 'ResourceReader']) -elif sys.version_info >= (3,): +if sys.version_info >= (3,): from importlib_resources._py3 import ( Package, Resource, contents, is_resource, open_binary, open_text, path, read_binary, read_text) from importlib_resources.abc import ResourceReader - __all__.extend(['Package', 'Resource', 'ResourceReader']) else: from importlib_resources._py2 import ( contents, is_resource, open_binary, open_text, path, read_binary, read_text) + del __all__[-3:] __version__ = read_text('importlib_resources', 'version.txt').strip() From cb7f16243cd142c2dbdbe14ea50e4806675e9b78 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 17 Jan 2020 21:10:06 -0500 Subject: [PATCH 2/4] Fix test failures on Python 3.7 and 3.8 --- importlib_resources/_py3.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/importlib_resources/_py3.py b/importlib_resources/_py3.py index 4b138c6dfed90f3..8c73408fd576002 100644 --- a/importlib_resources/_py3.py +++ b/importlib_resources/_py3.py @@ -76,7 +76,8 @@ def open_binary(package: Package, resource: Resource) -> BinaryIO: return reader.open_resource(resource) # Using pathlib doesn't work well here due to the lack of 'strict' # argument for pathlib.Path.resolve() prior to Python 3.6. - absolute_package_path = os.path.abspath(package.__spec__.origin) + absolute_package_path = os.path.abspath( + package.__spec__.origin or 'non-existent file') package_path = os.path.dirname(absolute_package_path) full_path = os.path.join(package_path, resource) try: @@ -241,8 +242,11 @@ def contents(package: Package) -> Iterable[str]: return reader.contents() # Is the package a namespace package? By definition, namespace packages # cannot have resources. - if (package.__spec__.origin == 'namespace' and - not package.__spec__.has_location): + namespace = ( + package.__spec__.origin is None or + package.__spec__.origin == 'namespace' + ) + if namespace or not package.__spec__.has_location: return () package_directory = Path(package.__spec__.origin).parent try: From 8ec30ee11fd9d2f4e9a13939d9acbaf30d077369 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 17 Jan 2020 21:37:23 -0500 Subject: [PATCH 3/4] Fix coverage errors on Python 3.6 --- importlib_resources/_py3.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/importlib_resources/_py3.py b/importlib_resources/_py3.py index 8c73408fd576002..110dbae543c2cdb 100644 --- a/importlib_resources/_py3.py +++ b/importlib_resources/_py3.py @@ -185,10 +185,7 @@ def is_resource(package: Package, name: str) -> bool: reader = _get_resource_reader(package) if reader is not None: return reader.is_resource(name) - try: - package_contents = set(contents(package)) - except (NotADirectoryError, FileNotFoundError): - return False + package_contents = set(contents(package)) if name not in package_contents: return False # Just because the given file_name lives as an entry in the package's @@ -254,8 +251,6 @@ def contents(package: Package) -> Iterable[str]: except (NotADirectoryError, FileNotFoundError): # The package is probably in a zip file. archive_path = getattr(package.__spec__.loader, 'archive', None) - if archive_path is None: - raise relpath = package_directory.relative_to(archive_path) with ZipFile(archive_path) as zf: toc = zf.namelist() From 6591a31f18ea3121f9e81e11a53c0645f1e1c2a9 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 17 Jan 2020 21:46:30 -0500 Subject: [PATCH 4/4] Correct tox envlist comment --- tox.ini | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tox.ini b/tox.ini index 9c5b6cc4f996ded..3ae42681004fb58 100644 --- a/tox.ini +++ b/tox.ini @@ -1,7 +1,6 @@ [tox] -# Don't cover Python 3.7 since this is just a shim for that version. Do at -# least make sure we don't regress! -envlist = {py27,py35,py36}{,-cov,-diffcov},{py37,py38},qa,docs +# Coverage is missing on later version of Python +envlist = {py27,py35,py36}{,-cov,-diffcov},py37,py38,qa,docs skip_missing_interpreters = True