Skip to content

Commit

Permalink
Stop using pkg_resources (#158)
Browse files Browse the repository at this point in the history
* stop using pkg_resources

* make mypy happy

* add str comment

* specify min version

* remove setuptools dep

* link to source
  • Loading branch information
bmw committed Mar 9, 2023
1 parent 19e300d commit 8f1b4b5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
27 changes: 23 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ python = "^3.7"
cryptography = ">=1.5"
# Connection.set_tlsext_host_name (>=0.13)
pyopenssl = ">=0.13"
# For pkg_resources. >=1.0 so pip resolves it to a version cryptography
# will tolerate; see #2599:
setuptools = ">=1.0"
# >=4.3.0 is needed for Python 3.10 support
sphinx = {version = ">=4.3.0", optional = true}
sphinx-rtd-theme = {version = ">=1.0", optional = true}
Expand All @@ -56,6 +53,10 @@ coverage = {version = ">=4.0", extras = ["toml"]}
# https://github.com/PyCQA/flake8/issues/1701.
flake8 = ">=4.0"
flake8-assertive = "*"
# importlib_resources 1.3 was the version included in Python 3.9 which
# introduced the functionality we are using. See
# https://github.com/python/importlib_resources/tree/7f4fbb5ee026d7610636d5ece18b09c64aa0c893#compatibility.
importlib_resources = {version = ">=1.3", python = "<3.9"}
isort = "*"
mypy = "*"
types-pyOpenSSL = "*"
Expand Down
26 changes: 21 additions & 5 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""Test utilities."""
import atexit
import contextlib
import os
import sys
from typing import Any

import pkg_resources
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from OpenSSL import crypto
Expand All @@ -11,17 +13,31 @@
from josepy import ComparableRSAKey, ComparableX509
from josepy.util import ComparableECKey

# This approach is based on the recommendation at
# https://github.com/python/mypy/issues/1153#issuecomment-1207333806.
if sys.version_info >= (3, 9):
import importlib.resources as importlib_resources
else:
import importlib_resources

TESTDATA = importlib_resources.files('testdata')


def vector_path(*names: str) -> str:
"""Path to a test vector."""
return pkg_resources.resource_filename(
__name__, os.path.join('testdata', *names))
# This code is based on the recommendation at
# https://web.archive.org/web/20230131043552/https://importlib-resources.readthedocs.io/en/latest/migration.html#pkg-resources-resource-filename.
file_manager = contextlib.ExitStack()
atexit.register(file_manager.close)
ref = TESTDATA.joinpath(*names)
# We convert the value to str here because some of the calling code doesn't
# work with pathlib objects.
return str(file_manager.enter_context(importlib_resources.as_file(ref)))


def load_vector(*names: str) -> bytes:
"""Load contents of a test vector."""
# luckily, resource_string opens file in binary mode
return pkg_resources.resource_string(__name__, os.path.join('testdata', *names))
return TESTDATA.joinpath(*names).read_bytes()


def _guess_loader(filename: str, loader_pem: Any, loader_der: Any) -> Any:
Expand Down
Empty file added tests/testdata/__init__.py
Empty file.

0 comments on commit 8f1b4b5

Please sign in to comment.