From fc2337d28125e67b780c1328b6da3be8898e1667 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Tue, 31 Oct 2017 10:56:16 -0700 Subject: [PATCH] Use LBYL --- importlib_resources/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/importlib_resources/__init__.py b/importlib_resources/__init__.py index 5f560291..0d596534 100644 --- a/importlib_resources/__init__.py +++ b/importlib_resources/__init__.py @@ -47,9 +47,9 @@ 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) - try: + if hasattr(package.__spec__.loader, 'open_resource'): return package.__spec__.loader.open_resource(file_name) - except AttributeError: + else: # 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)