Skip to content

Commit 46e7a5d

Browse files
committed
Replace the use of pkg_resources with the importlib library.
Change-Id: Iafb96d53196ff74c98f2fbc7caf2eb8fbdc70029
1 parent 6055be0 commit 46e7a5d

5 files changed

Lines changed: 33 additions & 10 deletions

File tree

doc/build/caching.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ using a :class:`.CacheImpl` subclass. This class implements
284284
the rudimental methods Mako needs to implement the caching
285285
API. Mako includes the :class:`.BeakerCacheImpl` class to
286286
provide the default implementation. A :class:`.CacheImpl` class
287-
is acquired by Mako using a ``pkg_resources`` entrypoint, using
287+
is acquired by Mako using a ``importlib.metatada`` entrypoint, using
288288
the name given as the ``cache_impl`` argument to :class:`.Template`
289289
or :class:`.TemplateLookup`. This entry point can be
290290
installed via the standard `setuptools`/``setup()`` procedure, underneath
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.. change::
2+
:tags: setup
3+
4+
Replaced the use of ``pkg_resources`` with the ``importlib`` library.
5+
For Python < 3.8 the library ``importlib_metadata`` is used.

mako/compat.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
win32 = sys.platform.startswith("win")
1313
pypy = hasattr(sys, "pypy_version_info")
14+
py38 = sys.version_info >= (3, 8)
1415

1516
ArgSpec = collections.namedtuple(
1617
"ArgSpec", ["args", "varargs", "keywords", "defaults"]
@@ -59,3 +60,17 @@ def exception_as():
5960

6061
def exception_name(exc):
6162
return exc.__class__.__name__
63+
64+
65+
if py38:
66+
from importlib import metadata as importlib_metadata
67+
else:
68+
import importlib_metadata # noqa
69+
70+
71+
def importlib_metadata_get(group):
72+
ep = importlib_metadata.entry_points()
73+
if hasattr(ep, "select"):
74+
return ep.select(group=group)
75+
else:
76+
return ep.get(group, ())

mako/util.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import re
1212
import timeit
1313

14+
from .compat import importlib_metadata_get
15+
1416

1517
def update_wrapper(decorated, fn):
1618
decorated.__wrapped__ = fn
@@ -26,17 +28,17 @@ def __init__(self, group):
2628
def load(self, name):
2729
if name in self.impls:
2830
return self.impls[name]()
29-
import pkg_resources
3031

31-
for impl in pkg_resources.iter_entry_points(self.group, name):
32-
self.impls[name] = impl.load
33-
return impl.load()
34-
else:
35-
from mako import exceptions
32+
for impl in importlib_metadata_get(self.group):
33+
if impl.name == name:
34+
self.impls[name] = impl.load
35+
return impl.load()
3636

37-
raise exceptions.RuntimeException(
38-
"Can't load plugin %s %s" % (self.group, name)
39-
)
37+
from mako import exceptions
38+
39+
raise exceptions.RuntimeException(
40+
"Can't load plugin %s %s" % (self.group, name)
41+
)
4042

4143
def register(self, name, modulepath, objname):
4244
def load():

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ zip_safe = false
3535

3636
install_requires =
3737
MarkupSafe >= 0.9.2
38+
importlib-metadata;python_version<"3.8"
3839

3940
[options.packages.find]
4041
exclude =

0 commit comments

Comments
 (0)