Skip to content

Commit 07f4434

Browse files
authored
Merge pull request #206 from asottile/revert-199-importlib_metadata_v2
Revert "Switch to importlib-metadata"
2 parents d7a9792 + 2b06ab4 commit 07f4434

File tree

5 files changed

+72
-55
lines changed

5 files changed

+72
-55
lines changed

changelog/205.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Revert changes made in 0.10.0 release breaking ``.egg`` installs.

docs/conf.py

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

44

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

2121
# General information about the project.
2222

23-
project = "pluggy"
23+
dist = pkg_resources.get_distribution("pluggy")
24+
project = dist.project_name
2425
copyright = u"2016, Holger Krekel"
2526
author = "Holger Krekel"
2627

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

3132

3233
language = None

pluggy/manager.py

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

6-
import importlib_metadata
7-
86

97
def _warn_for_function(warning, function):
108
warnings.warn_explicit(
@@ -27,23 +25,6 @@ def __init__(self, plugin, message):
2725
super(Exception, self).__init__(message)
2826

2927

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-
4728
class PluginManager(object):
4829
""" Core Pluginmanager class which manages registration
4930
of plugin objects and 1:N hook calling.
@@ -278,18 +259,29 @@ def load_setuptools_entrypoints(self, group, name=None):
278259
:rtype: int
279260
:return: return the number of loaded plugins by this call.
280261
"""
262+
from pkg_resources import (
263+
iter_entry_points,
264+
DistributionNotFound,
265+
VersionConflict,
266+
)
267+
281268
count = 0
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
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:
289274
plugin = ep.load()
290-
self.register(plugin, name=ep.name)
291-
self._plugin_distinfo.append((plugin, DistFacade(dist)))
292-
count += 1
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
293285
return count
294286

295287
def list_plugin_distinfo(self):

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ def main():
3636
author_email="[email protected]",
3737
url="https://github.com/pytest-dev/pluggy",
3838
python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
39-
install_requires=["importlib-metadata>=0.9"],
4039
extras_require={"dev": ["pre-commit", "tox"]},
4140
classifiers=classifiers,
4241
packages=["pluggy"],

testing/test_pluginmanager.py

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

448448

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

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

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

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

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

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

469-
monkeypatch.setattr(importlib_metadata, "distributions", my_distributions)
467+
monkeypatch.setattr(pkg_resources, "iter_entry_points", my_iter)
470468
num = pm.load_setuptools_entrypoints("hello")
471469
assert num == 1
472470
plugin = pm.get_plugin("myname")
473471
assert plugin.x == 42
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
472+
assert pm.list_plugin_distinfo() == [(plugin, None)]
480473
num = pm.load_setuptools_entrypoints("hello")
481474
assert num == 0 # no plugin loaded by this call
482475

483476

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+
484508
def test_add_tracefuncs(he_pm):
485509
out = []
486510

0 commit comments

Comments
 (0)