Skip to content

Commit

Permalink
Merge branch 'feature/run-everywhere' into 'master'
Browse files Browse the repository at this point in the history
Update package to run on all versions of Python

Closes python#70

See merge request python-devs/importlib_resources!83
  • Loading branch information
jaraco committed Jan 18, 2020
2 parents 6d86ad9 + 6591a31 commit ce2fa42
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
14 changes: 5 additions & 9 deletions importlib_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
17 changes: 8 additions & 9 deletions importlib_resources/_py3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -184,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
Expand Down Expand Up @@ -241,17 +239,18 @@ 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:
return os.listdir(str(package_directory))
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()
Expand Down
5 changes: 2 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -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


Expand Down

0 comments on commit ce2fa42

Please sign in to comment.