replace pkg_resources.load_entry_point

pkg_resources is deprecated. Use the corresponding importlib.metadata
interface instead. Use the stdlib version on python >= 3.8 and use the
importlib_metadata backport on older versions.
This commit is contained in:
Maxwell G 2023-04-04 18:50:22 +00:00
parent b8d6b1e97c
commit 7f480daf07
No known key found for this signature in database
GPG Key ID: F79E4E25E8C661F8
2 changed files with 16 additions and 8 deletions

View File

@ -22,7 +22,10 @@ import time
import traceback
import warnings
import pkg_resources
try:
import importlib.metadata as importlib_metadata
except ImportError:
import importlib_metadata
from gunicorn.errors import AppImportError
from gunicorn.workers import SUPPORTED_WORKERS
@ -54,6 +57,15 @@ except ImportError:
pass
def load_entry_point(distribution, group, name):
dist_obj = importlib_metadata.distribution(distribution)
eps = [ep for ep in dist_obj.entry_points
if ep.group == group and ep.name == name]
if not eps:
raise ImportError("Entry point %r not found" % ((group, name),))
return eps[0].load()
def load_class(uri, default="gunicorn.workers.sync.SyncWorker",
section="gunicorn.workers"):
if inspect.isclass(uri):
@ -68,7 +80,7 @@ def load_class(uri, default="gunicorn.workers.sync.SyncWorker",
name = default
try:
return pkg_resources.load_entry_point(dist, section, name)
return load_entry_point(dist, section, name)
except Exception:
exc = traceback.format_exc()
msg = "class uri %r invalid or not found: \n\n[%s]"
@ -85,7 +97,7 @@ def load_class(uri, default="gunicorn.workers.sync.SyncWorker",
break
try:
return pkg_resources.load_entry_point(
return load_entry_point(
"gunicorn", section, uri
)
except Exception:

View File

@ -70,11 +70,7 @@ class PyTestCommand(TestCommand):
install_requires = [
# We depend on functioning pkg_resources.working_set.add_entry() and
# pkg_resources.load_entry_point(). These both work as of 3.0 which
# is the first version to support Python 3.4 which we require as a
# floor.
'setuptools>=3.0',
'importlib_metadata; python_version<"3.8"',
'packaging',
]