forked from certifi/python-certifi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to more recent 'files' API from importlib.resources from Pytho…
…n 3.9 and importlib_metadata 1.1. Add explicit temporary file cleanup behavior using atexit. Fixes certifi#113.
- Loading branch information
Showing
3 changed files
with
50 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
""" | ||
Fallback for Python prior to 3.9 where importlib_resources | ||
is not available. | ||
Does not support modules where __file__ is not defined on | ||
the module. | ||
""" | ||
|
||
import os | ||
|
||
|
||
def read_text(_module, _path): | ||
with open(where(), "r", encoding="ascii") as data: | ||
return data.read() | ||
|
||
|
||
def where(): | ||
return os.path.join(os.path.dirname(__file__), "cacert.pem") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import atexit | ||
import functools | ||
|
||
try: | ||
from importlib import resources | ||
except ImportError: | ||
import importlib_resources as resources | ||
|
||
|
||
# ensure 'files' API is present | ||
resources.files | ||
read_text = resources.read_text | ||
|
||
|
||
def as_file(path): | ||
""" | ||
Ensure the path is a file on the file system for the duration | ||
of the interpreter run. | ||
""" | ||
ctx = resources.as_file(path) | ||
tmp_copy = ctx.__enter__() | ||
atexit.register(tmp_copy.__exit__, None, None, None) | ||
return tmp_copy | ||
|
||
|
||
@functools.lru_cache() | ||
def where(): | ||
return as_file(resources.files('certifi') / 'cacert.pem') |