Skip to content

Commit 102e01c

Browse files
committed
Load entry_points once using importlib.metadata.
This should be more performant. Fixes #942.
1 parent ada40c6 commit 102e01c

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

docs/change_log/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Python-Markdown Change Log
55

66
Under development: version 3.2.2 (a bug-fix release).
77

8+
* Load entry_points (for extensions) only once using `importlib.metadata`.
89
* Fixed issue where double escaped entities could end up in TOC.
910

1011
Feb 12, 2020: Released version 3.2.1 (a bug-fix release).

markdown/core.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import sys
2424
import logging
2525
import importlib
26-
import pkg_resources
2726
from . import util
2827
from .preprocessors import build_preprocessors
2928
from .blockprocessors import build_block_parser
@@ -141,17 +140,16 @@ def build_extension(self, ext_name, configs):
141140
Build extension from a string name, then return an instance.
142141
143142
First attempt to load an entry point. The string name must be registered as an entry point in the
144-
`markdown.extensions` group which points to a subclass of the `markdown.extensions.Extension` class. If
145-
multiple distributions have registered the same name, the first one found by `pkg_resources.iter_entry_points`
146-
is returned.
143+
`markdown.extensions` group which points to a subclass of the `markdown.extensions.Extension` class.
144+
If multiple distributions have registered the same name, the first one found is returned.
147145
148146
If no entry point is found, assume dot notation (`path.to.module:ClassName`). Load the specified class and
149147
return an instance. If no class is specified, import the module and call a `makeExtension` function and return
150148
the Extension instance returned by that function.
151149
"""
152150
configs = dict(configs)
153151

154-
entry_points = [ep for ep in pkg_resources.iter_entry_points('markdown.extensions', ext_name)]
152+
entry_points = [ep for ep in util.INSTALLED_EXTENSIONS if ep.name == ext_name]
155153
if entry_points:
156154
ext = entry_points[0].load()
157155
return ext(**configs)

markdown/util.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
import xml.etree.ElementTree
2828
from .pep562 import Pep562
2929

30+
try:
31+
from importlib import metadata
32+
except ImportError:
33+
# <PY38 use backport
34+
import importlib_metadata as metadata
3035

3136
PY37 = (3, 7) <= sys.version_info
3237

@@ -76,6 +81,8 @@
7681
-----------------------------------------------------------------------------
7782
"""
7883

84+
# Only load extension entry_points once.
85+
INSTALLED_EXTENSIONS = metadata.entry_points().get('markdown.extensions', ())
7986
RTL_BIDI_RANGES = (
8087
('\u0590', '\u07FF'),
8188
# Hebrew (0590-05FF), Arabic (0600-06FF),

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def get_version():
8888
license='BSD License',
8989
packages=['markdown', 'markdown.extensions'],
9090
python_requires='>=3.5',
91-
install_requires=['setuptools >= 36'],
91+
install_requires=['setuptools >= 36', "importlib_metadata;python_version<'3.8'"],
9292
extras_require={
9393
'testing': [
9494
'coverage',

0 commit comments

Comments
 (0)