Skip to content

Commit

Permalink
Don't use pathlib.Path.resolve() due to lack of 'strict' argument in …
Browse files Browse the repository at this point in the history
…Python 3.5 and older
  • Loading branch information
brettcannon committed Oct 27, 2017
1 parent 43cb163 commit 1294d40
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions importlib_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,21 @@ def open(package: Package, file_name: FileName) -> BinaryIO:
"""Return a file-like object opened for binary-reading of the resource."""
file_name = _normalize_path(file_name)
package = _get_package(package)
package_path = pathlib.Path(package.__spec__.origin).resolve().parent
full_path = package_path / file_name
# 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)
package_path = os.path.dirname(absolute_package_path)
full_path = os.path.join(package_path, file_name)
try:
return full_path.open('rb')
return builtins.open(full_path, 'rb')
except IOError:
# Just assume the loader is a resource loader; all the relevant
# importlib.machinery loaders are and an AttributeError for get_data()
# will make it clear what is needed from the loader.
loader = typing.cast(importlib.abc.ResourceLoader,
package.__spec__.loader)
try:
data = loader.get_data(str(full_path))
data = loader.get_data(full_path)
except IOError:
package_name = package.__spec__.name
message = '{!r} resource not found in {!r}'.format(file_name, package_name)
Expand Down

0 comments on commit 1294d40

Please sign in to comment.