From 4fbbf1f9f47f56d7619c8c4a8cc2d03a59668ac0 Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Sun, 4 Feb 2024 22:32:05 -0700 Subject: [PATCH] remove pkg_resources from DottedNameResolver --- CHANGES.rst | 3 +++ src/pyramid/path.py | 22 ++++++++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index c05bc66fa..fd0b8e22f 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -28,6 +28,9 @@ Features See https://github.com/Pylons/pyramid/pull/3745 +- Replace usage of ``pkg_resources`` in ``pyramid.path.DottedNameResolver``. + See https://github.com/Pylons/pyramid/pull/3748 + Bug Fixes --------- diff --git a/src/pyramid/path.py b/src/pyramid/path.py index a5b7ad212..160d97abe 100644 --- a/src/pyramid/path.py +++ b/src/pyramid/path.py @@ -1,3 +1,5 @@ +import functools +from importlib import import_module from importlib.machinery import SOURCE_SUFFIXES import os import pkg_resources @@ -344,14 +346,18 @@ def _pkg_resources_style(self, value, package): value = package.__name__ else: value = package.__name__ + value - # Calling EntryPoint.load with an argument is deprecated. - # See https://pythonhosted.org/setuptools/history.html#id8 - ep = pkg_resources.EntryPoint.parse('x=%s' % value) - if hasattr(ep, 'resolve'): - # setuptools>=10.2 - return ep.resolve() # pragma: NO COVER - else: - return ep.load(False) # pragma: NO COVER + # logic below is similar to importlib.metadata.EntryPoint.load() + module = value + attrs = [] + parts = value.split(':', 1) + if len(parts) == 2: + module, attrs = parts + attrs = attrs.split('.') + module = import_module(module) + try: + return functools.reduce(getattr, attrs, module) + except AttributeError as ex: + raise ImportError(str(ex)) def _zope_dottedname_style(self, value, package): """package.module.attr style"""