Skip to content

Commit 1294d40

Browse files
committed
Don't use pathlib.Path.resolve() due to lack of 'strict' argument in Python 3.5 and older
1 parent 43cb163 commit 1294d40

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

importlib_resources/__init__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,21 @@ def open(package: Package, file_name: FileName) -> BinaryIO:
4646
"""Return a file-like object opened for binary-reading of the resource."""
4747
file_name = _normalize_path(file_name)
4848
package = _get_package(package)
49-
package_path = pathlib.Path(package.__spec__.origin).resolve().parent
50-
full_path = package_path / file_name
49+
# Using pathlib doesn't work well here due to the lack of 'strict' argument
50+
# for pathlib.Path.resolve() prior to Python 3.6.
51+
absolute_package_path = os.path.abspath(package.__spec__.origin)
52+
package_path = os.path.dirname(absolute_package_path)
53+
full_path = os.path.join(package_path, file_name)
5154
try:
52-
return full_path.open('rb')
55+
return builtins.open(full_path, 'rb')
5356
except IOError:
5457
# Just assume the loader is a resource loader; all the relevant
5558
# importlib.machinery loaders are and an AttributeError for get_data()
5659
# will make it clear what is needed from the loader.
5760
loader = typing.cast(importlib.abc.ResourceLoader,
5861
package.__spec__.loader)
5962
try:
60-
data = loader.get_data(str(full_path))
63+
data = loader.get_data(full_path)
6164
except IOError:
6265
package_name = package.__spec__.name
6366
message = '{!r} resource not found in {!r}'.format(file_name, package_name)

0 commit comments

Comments
 (0)