Skip to content

Commit ccafa28

Browse files
authored
Merge pull request #2594 from SnoopJ/bugfix/gh2593-fix-entrypoint-reloading
Fix version check for entrypoint plugins with unequal project/package names
2 parents 2e8de02 + 8d6853e commit ccafa28

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

sopel/plugins/handlers.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import importlib.util
4949
import inspect
5050
import itertools
51+
import logging
5152
import os
5253
import sys
5354
from typing import Optional, TYPE_CHECKING, TypedDict
@@ -61,6 +62,9 @@
6162
from types import ModuleType
6263

6364

65+
LOGGER = logging.getLogger(__name__)
66+
67+
6468
class PluginMetaDescription(TypedDict):
6569
"""Meta description of a plugin, as a dictionary.
6670
@@ -620,14 +624,20 @@ def get_version(self) -> Optional[str]:
620624

621625
if (
622626
version is None
623-
and hasattr(self.module, "__package__")
624-
and self.module.__package__ is not None
627+
and hasattr(self.entry_point, "dist")
628+
and hasattr(self.entry_point.dist, "name")
625629
):
630+
dist_name = self.entry_point.dist.name
626631
try:
627-
version = importlib.metadata.version(self.module.__package__)
628-
except ValueError:
629-
# package name is probably empty-string; just give up
630-
pass
632+
version = importlib.metadata.version(dist_name)
633+
except (ValueError, importlib.metadata.PackageNotFoundError):
634+
LOGGER.warning("Cannot determine version of %r", dist_name)
635+
except Exception:
636+
LOGGER.warning(
637+
"Unexpected error occurred while checking the version of %r",
638+
dist_name,
639+
exc_info=True,
640+
)
631641

632642
return version
633643

test/plugins/test_plugins_handlers.py

+18
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,21 @@ def test_folder_plugin_imports(plugin_folder):
142142
handler = handlers.PyFilePlugin(plugin_folder)
143143
handler.load()
144144
assert handler.module.foo == 'bar baz'
145+
146+
147+
def test_get_version_entrypoint_package_does_not_match(plugin_tmpfile):
148+
# See gh-2593, wherein an entrypoint plugin whose project/package names
149+
# are not equal raised an exception that propagated too far
150+
distrib_dir = os.path.dirname(plugin_tmpfile.strpath)
151+
sys.path.append(distrib_dir)
152+
153+
try:
154+
entry_point = importlib.metadata.EntryPoint(
155+
'test_plugin', 'file_mod', 'sopel.plugins')
156+
plugin = handlers.EntryPointPlugin(entry_point)
157+
plugin.load()
158+
plugin.module.__package__ = "FAKEFAKEFAKE"
159+
# Under gh-2593, this call raises a PackageNotFound error
160+
assert plugin.get_version() is None
161+
finally:
162+
sys.path.remove(distrib_dir)

0 commit comments

Comments
 (0)