Skip to content

Switch from pkg_resources to importlib.metadata #2155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 4, 2022

Conversation

jeanas
Copy link
Contributor

@jeanas jeanas commented May 29, 2022

importlib.metadata is a standard library replacement for
pkg_resources, available starting with Python 3.8. It is faster than
pkg_resources. This lets the plugin interface use it when available,
i.e. on Python 3.8 and later. On earlier Python, it uses the
importlib_metadata PyPI backport if available, and finally falls back
on pkg_resources. setup.cfg gains an extra called plugins, which can
allow a project to install "pygments[plugins]" in order to ensure that
plugins support is present even if installed with older Python
versions.

Timings for lexing an empty file with a lexer from a random plugin:

Before:

real	0m0,238s
user	0m0,210s
sys	0m0,029s

After:

real	0m0,141s
user	0m0,125s
sys	0m0,017s

Fixes #2116, #2126

importlib.metadata is a standard library replacement for
pkg_resources, available starting with Python 3.8. It is faster than
pkg_resources. This lets the plugin interface use it when available,
i.e. on Python 3.8 and later. On earlier Python, it uses the
importlib_metadata PyPI backport if available, and finally falls back
on pkg_resources. setup.cfg gains an extra called plugins, which can
allow a project to install "pygments[plugins]" in order to ensure that
plugins support is present even if installed with older Python
versions.

Timings for lexing an empty file with a lexer from a random plugin:

Before:

real	0m0,238s
user	0m0,210s
sys	0m0,029s

After:

real	0m0,141s
user	0m0,125s
sys	0m0,017s

Fixes pygments#2116, pygments#2126
It was apparently added in Python 3.10 or recent versions of the
importlib_metadata backport.
@jeanas
Copy link
Contributor Author

jeanas commented May 29, 2022

Bah. This failed the CI initially because entry_points() didn't accept the group keyword argument. Turns out, it was added in Python 3.10 and in recent versions of the Python <3.8 backport. Of course, I had tested those two versions, and not 3.8 or 3.9…

@birkenfeld
Copy link
Member

Thanks for taking on this topic! I'll try to review when back home.

@jeanas
Copy link
Contributor Author

jeanas commented May 30, 2022

Thanks. No hurry, take your time.

Copy link
Member

@birkenfeld birkenfeld left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@jeanas jeanas merged commit 362c967 into pygments:master Jun 4, 2022
@jeanas jeanas deleted the importlib-metadata branch June 4, 2022 21:29
@Anteru Anteru added this to the 2.13.0 milestone Jun 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RFE: Replace use of pkg_resources with importlib.metadata
3 participants