Skip to content

Commit

Permalink
Switch from pkg_resources importlib-metadata (#1071)
Browse files Browse the repository at this point in the history
See e.g. pypa/setuptools#510 (comment)

On a moderatly large project (~200 packages) at $work this saves ~150 ms on
`python -c 'from kombu import Exchange'`
  • Loading branch information
davidszotten authored and auvipy committed Jul 5, 2019
1 parent 052f760 commit 193054d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
5 changes: 5 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
Change history
================

Unreleased
==========

- Use importlib-metadata instead of pkg_resources for better performance

.. _version-4.6.3:

4.6.3
Expand Down
10 changes: 5 additions & 5 deletions kombu/utils/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from functools import wraps

from contextlib import contextmanager
import importlib_metadata

from kombu.five import reraise

Expand Down Expand Up @@ -83,11 +84,10 @@ def detect_environment():

def entrypoints(namespace):
"""Return setuptools entrypoints for namespace."""
try:
from pkg_resources import iter_entry_points
except ImportError:
return iter([])
return ((ep, ep.load()) for ep in iter_entry_points(namespace))
return (
(ep, ep.load())
for ep in importlib_metadata.entry_points().get(namespace, [])
)


def fileno(f):
Expand Down
1 change: 1 addition & 0 deletions requirements/default.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
amqp>=2.5.0,<3.0
importlib-metadata>=0.18
26 changes: 11 additions & 15 deletions t/unit/utils/test_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,17 @@
from kombu.utils.compat import entrypoints, maybe_fileno


class test_entrypoints:

@mock.mask_modules('pkg_resources')
def test_without_pkg_resources(self):
assert list(entrypoints('kombu.test')) == []

@mock.module_exists('pkg_resources')
def test_with_pkg_resources(self):
with patch('pkg_resources.iter_entry_points', create=True) as iterep:
eps = iterep.return_value = [Mock(), Mock()]

assert list(entrypoints('kombu.test'))
iterep.assert_called_with('kombu.test')
eps[0].load.assert_called_with()
eps[1].load.assert_called_with()
def test_entrypoints():
with patch(
'kombu.utils.compat.importlib_metadata.entry_points', create=True
) as iterep:
eps = [Mock(), Mock()]
iterep.return_value = {'kombu.test': eps}

assert list(entrypoints('kombu.test'))
iterep.assert_called_with()
eps[0].load.assert_called_with()
eps[1].load.assert_called_with()


def test_maybe_fileno():
Expand Down

0 comments on commit 193054d

Please sign in to comment.