From 102e01c2462f20fdd29073bc0b1fe2252b517add Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Sat, 18 Apr 2020 18:34:13 -0400 Subject: [PATCH] Load entry_points once using importlib.metadata. This should be more performant. Fixes #942. --- docs/change_log/index.md | 1 + markdown/core.py | 8 +++----- markdown/util.py | 7 +++++++ setup.py | 2 +- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/docs/change_log/index.md b/docs/change_log/index.md index 3370c4f1f..8f1b9db84 100644 --- a/docs/change_log/index.md +++ b/docs/change_log/index.md @@ -5,6 +5,7 @@ Python-Markdown Change Log Under development: version 3.2.2 (a bug-fix release). +* Load entry_points (for extensions) only once using `importlib.metadata`. * Fixed issue where double escaped entities could end up in TOC. Feb 12, 2020: Released version 3.2.1 (a bug-fix release). diff --git a/markdown/core.py b/markdown/core.py index 6c7822cba..e2c0d88f7 100644 --- a/markdown/core.py +++ b/markdown/core.py @@ -23,7 +23,6 @@ import sys import logging import importlib -import pkg_resources from . import util from .preprocessors import build_preprocessors from .blockprocessors import build_block_parser @@ -141,9 +140,8 @@ def build_extension(self, ext_name, configs): Build extension from a string name, then return an instance. First attempt to load an entry point. The string name must be registered as an entry point in the - `markdown.extensions` group which points to a subclass of the `markdown.extensions.Extension` class. If - multiple distributions have registered the same name, the first one found by `pkg_resources.iter_entry_points` - is returned. + `markdown.extensions` group which points to a subclass of the `markdown.extensions.Extension` class. + If multiple distributions have registered the same name, the first one found is returned. If no entry point is found, assume dot notation (`path.to.module:ClassName`). Load the specified class and return an instance. If no class is specified, import the module and call a `makeExtension` function and return @@ -151,7 +149,7 @@ def build_extension(self, ext_name, configs): """ configs = dict(configs) - entry_points = [ep for ep in pkg_resources.iter_entry_points('markdown.extensions', ext_name)] + entry_points = [ep for ep in util.INSTALLED_EXTENSIONS if ep.name == ext_name] if entry_points: ext = entry_points[0].load() return ext(**configs) diff --git a/markdown/util.py b/markdown/util.py index e7bc295fb..056fd72a2 100644 --- a/markdown/util.py +++ b/markdown/util.py @@ -27,6 +27,11 @@ import xml.etree.ElementTree from .pep562 import Pep562 +try: + from importlib import metadata +except ImportError: + # = 36'], + install_requires=['setuptools >= 36', "importlib_metadata;python_version<'3.8'"], extras_require={ 'testing': [ 'coverage',