Path: blob/master/venv/Lib/site-packages/certifi/core.py
811 views
# -*- coding: utf-8 -*-12"""3certifi.py4~~~~~~~~~~56This module returns the installation location of cacert.pem or its contents.7"""8import os910try:11from importlib.resources import path as get_path, read_text1213_CACERT_CTX = None14_CACERT_PATH = None1516def where():17# This is slightly terrible, but we want to delay extracting the file18# in cases where we're inside of a zipimport situation until someone19# actually calls where(), but we don't want to re-extract the file20# on every call of where(), so we'll do it once then store it in a21# global variable.22global _CACERT_CTX23global _CACERT_PATH24if _CACERT_PATH is None:25# This is slightly janky, the importlib.resources API wants you to26# manage the cleanup of this file, so it doesn't actually return a27# path, it returns a context manager that will give you the path28# when you enter it and will do any cleanup when you leave it. In29# the common case of not needing a temporary file, it will just30# return the file system location and the __exit__() is a no-op.31#32# We also have to hold onto the actual context manager, because33# it will do the cleanup whenever it gets garbage collected, so34# we will also store that at the global level as well.35_CACERT_CTX = get_path("certifi", "cacert.pem")36_CACERT_PATH = str(_CACERT_CTX.__enter__())3738return _CACERT_PATH394041except ImportError:42# This fallback will work for Python versions prior to 3.7 that lack the43# importlib.resources module but relies on the existing `where` function44# so won't address issues with environments like PyOxidizer that don't set45# __file__ on modules.46def read_text(_module, _path, encoding="ascii"):47with open(where(), "r", encoding=encoding) as data:48return data.read()4950# If we don't have importlib.resources, then we will just do the old logic51# of assuming we're on the filesystem and munge the path directly.52def where():53f = os.path.dirname(__file__)5455return os.path.join(f, "cacert.pem")565758def contents():59return read_text("certifi", "cacert.pem", encoding="ascii")606162