From d4e52bdfa9ee9c0d31474347dac8ab796eddd621 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Sun, 5 Apr 2020 21:09:15 +0100 Subject: [PATCH] Revert "Use resources loader to handle non-filesystem situations (#120)" This reverts commit a9ab4b3e095d5d84943c197d1e827e014a4a7d6b. --- certifi/core.py | 39 ++++++++++++--------------------------- 1 file changed, 12 insertions(+), 27 deletions(-) diff --git a/certifi/core.py b/certifi/core.py index b2ed599b..56b52a3c 100644 --- a/certifi/core.py +++ b/certifi/core.py @@ -6,40 +6,25 @@ This module returns the installation location of cacert.pem or its contents. """ -import importlib import os try: - import importlib.resources - # Defeat lazy module importers. - importlib.resources.open_binary - _HAVE_RESOURCE_READER = True + from importlib.resources import read_text except ImportError: - _HAVE_RESOURCE_READER = False - -try: - import pkg_resources - # Defeat lazy module importers. - _HAVE_PKG_RESOURCES = True -except ImportError: - _HAVE_PKG_RESOURCES = False - -_PACKAGE_NAME = "certifi" -_CACERT_NAME = "cacert.pem" + # This fallback will work for Python versions prior to 3.7 that lack the + # importlib.resources module but relies on the existing `where` function + # so won't address issues with environments like PyOxidizer that don't set + # __file__ on modules. + def read_text(_module, _path, encoding="ascii"): + with open(where(), "r", encoding=encoding) as data: + return data.read() def where(): - if _HAVE_PKG_RESOURCES: - return pkg_resources.resource_filename(_PACKAGE_NAME, _CACERT_NAME) - else: - mod = importlib.import_module(_PACKAGE_NAME) - path = os.path.dirname(mod.__file__) - return os.path.join(path, _CACERT_NAME) + f = os.path.dirname(__file__) + + return os.path.join(f, "cacert.pem") def contents(): - if _HAVE_RESOURCE_READER: - return importlib.resources.read_text(_PACKAGE_NAME, _CACERT_NAME, encoding="ascii") - else: - with open(where(), "r", encoding="ascii") as data: - return data.read() + return read_text("certifi", "cacert.pem", encoding="ascii")