File tree Expand file tree Collapse file tree 2 files changed +34
-6
lines changed
Expand file tree Collapse file tree 2 files changed +34
-6
lines changed Original file line number Diff line number Diff line change 4848import importlib .util
4949import inspect
5050import itertools
51+ import logging
5152import os
5253import sys
5354from typing import Optional , TYPE_CHECKING , TypedDict
6162 from types import ModuleType
6263
6364
65+ LOGGER = logging .getLogger (__name__ )
66+
67+
6468class 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
Original file line number Diff line number Diff 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 )
You can’t perform that action at this time.
0 commit comments