Skip to content

Commit b0f3b6d

Browse files
committed
Revert "Merge pull request #206 from asottile/revert-199-importlib_metadata_v2"
This reverts commit 07f4434, reversing changes made to d7a9792.
1 parent 7f8f8eb commit b0f3b6d

File tree

4 files changed

+55
-71
lines changed

4 files changed

+55
-71
lines changed

docs/conf.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
import pkg_resources
2+
import importlib_metadata
33

44

55
extensions = [
@@ -20,14 +20,13 @@
2020

2121
# General information about the project.
2222

23-
dist = pkg_resources.get_distribution("pluggy")
24-
project = dist.project_name
23+
project = "pluggy"
2524
copyright = u"2016, Holger Krekel"
2625
author = "Holger Krekel"
2726

28-
release = dist.version
27+
release = importlib_metadata.version(project)
2928
# The short X.Y version.
30-
version = u".".join(dist.version.split(".")[:2])
29+
version = u".".join(release.split(".")[:2])
3130

3231

3332
language = None

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def main():
3737
author_email="[email protected]",
3838
url="https://github.com/pytest-dev/pluggy",
3939
python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
40+
install_requires=["importlib-metadata>=0.9"],
4041
extras_require={"dev": ["pre-commit", "tox"]},
4142
classifiers=classifiers,
4243
packages=["pluggy"],

src/pluggy/manager.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from .hooks import HookImpl, _HookRelay, _HookCaller, normalize_hookimpl_opts
44
import warnings
55

6+
import importlib_metadata
7+
68

79
def _warn_for_function(warning, function):
810
warnings.warn_explicit(
@@ -25,6 +27,23 @@ def __init__(self, plugin, message):
2527
super(Exception, self).__init__(message)
2628

2729

30+
class DistFacade(object):
31+
"""Emulate a pkg_resources Distribution"""
32+
33+
def __init__(self, dist):
34+
self._dist = dist
35+
36+
@property
37+
def project_name(self):
38+
return self.metadata["name"]
39+
40+
def __getattr__(self, attr, default=None):
41+
return getattr(self._dist, attr, default)
42+
43+
def __dir__(self):
44+
return sorted(dir(self._dist) + ["_dist", "project_name"])
45+
46+
2847
class PluginManager(object):
2948
""" Core Pluginmanager class which manages registration
3049
of plugin objects and 1:N hook calling.
@@ -259,29 +278,18 @@ def load_setuptools_entrypoints(self, group, name=None):
259278
:rtype: int
260279
:return: return the number of loaded plugins by this call.
261280
"""
262-
from pkg_resources import (
263-
iter_entry_points,
264-
DistributionNotFound,
265-
VersionConflict,
266-
)
267-
268281
count = 0
269-
for ep in iter_entry_points(group, name=name):
270-
# is the plugin registered or blocked?
271-
if self.get_plugin(ep.name) or self.is_blocked(ep.name):
272-
continue
273-
try:
282+
for dist in importlib_metadata.distributions():
283+
for ep in dist.entry_points:
284+
if ep.group != group or (name is not None and ep.name != name):
285+
continue
286+
# is the plugin registered or blocked?
287+
if self.get_plugin(ep.name) or self.is_blocked(ep.name):
288+
continue
274289
plugin = ep.load()
275-
except DistributionNotFound:
276-
continue
277-
except VersionConflict as e:
278-
raise PluginValidationError(
279-
plugin=None,
280-
message="Plugin %r could not be loaded: %s!" % (ep.name, e),
281-
)
282-
self.register(plugin, name=ep.name)
283-
self._plugin_distinfo.append((plugin, ep.dist))
284-
count += 1
290+
self.register(plugin, name=ep.name)
291+
self._plugin_distinfo.append((plugin, DistFacade(dist)))
292+
count += 1
285293
return count
286294

287295
def list_plugin_distinfo(self):

testing/test_pluginmanager.py

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44
import pytest
55
import types
6-
import sys
6+
import importlib_metadata
77
from pluggy import (
88
PluginManager,
99
PluginValidationError,
@@ -447,64 +447,40 @@ def example_hook():
447447

448448

449449
def test_load_setuptools_instantiation(monkeypatch, pm):
450-
pkg_resources = pytest.importorskip("pkg_resources")
450+
class EntryPoint(object):
451+
name = "myname"
452+
group = "hello"
453+
value = "myname:foo"
451454

452-
def my_iter(group, name=None):
453-
assert group == "hello"
455+
def load(self):
456+
class PseudoPlugin(object):
457+
x = 42
454458

455-
class EntryPoint(object):
456-
name = "myname"
457-
dist = None
459+
return PseudoPlugin()
458460

459-
def load(self):
460-
class PseudoPlugin(object):
461-
x = 42
461+
class Distribution(object):
462+
entry_points = (EntryPoint(),)
462463

463-
return PseudoPlugin()
464+
dist = Distribution()
464465

465-
return iter([EntryPoint()])
466+
def my_distributions():
467+
return (dist,)
466468

467-
monkeypatch.setattr(pkg_resources, "iter_entry_points", my_iter)
469+
monkeypatch.setattr(importlib_metadata, "distributions", my_distributions)
468470
num = pm.load_setuptools_entrypoints("hello")
469471
assert num == 1
470472
plugin = pm.get_plugin("myname")
471473
assert plugin.x == 42
472-
assert pm.list_plugin_distinfo() == [(plugin, None)]
474+
ret = pm.list_plugin_distinfo()
475+
# poor man's `assert ret == [(plugin, mock.ANY)]`
476+
assert len(ret) == 1
477+
assert len(ret[0]) == 2
478+
assert ret[0][0] == plugin
479+
assert ret[0][1]._dist == dist
473480
num = pm.load_setuptools_entrypoints("hello")
474481
assert num == 0 # no plugin loaded by this call
475482

476483

477-
def test_load_setuptools_version_conflict(monkeypatch, pm):
478-
"""Check that we properly handle a VersionConflict problem when loading entry points"""
479-
pkg_resources = pytest.importorskip("pkg_resources")
480-
481-
def my_iter(group, name=None):
482-
assert group == "hello"
483-
484-
class EntryPoint(object):
485-
name = "myname"
486-
dist = None
487-
488-
def load(self):
489-
raise pkg_resources.VersionConflict("Some conflict")
490-
491-
return iter([EntryPoint()])
492-
493-
monkeypatch.setattr(pkg_resources, "iter_entry_points", my_iter)
494-
with pytest.raises(
495-
PluginValidationError,
496-
match="Plugin 'myname' could not be loaded: Some conflict!",
497-
):
498-
pm.load_setuptools_entrypoints("hello")
499-
500-
501-
def test_load_setuptools_not_installed(monkeypatch, pm):
502-
monkeypatch.setitem(sys.modules, "pkg_resources", types.ModuleType("pkg_resources"))
503-
504-
with pytest.raises(ImportError):
505-
pm.load_setuptools_entrypoints("qwe")
506-
507-
508484
def test_add_tracefuncs(he_pm):
509485
out = []
510486

0 commit comments

Comments
 (0)