File tree 2 files changed +34
-6
lines changed
2 files changed +34
-6
lines changed Original file line number Diff line number Diff line change 48
48
import importlib .util
49
49
import inspect
50
50
import itertools
51
+ import logging
51
52
import os
52
53
import sys
53
54
from typing import Optional , TYPE_CHECKING , TypedDict
61
62
from types import ModuleType
62
63
63
64
65
+ LOGGER = logging .getLogger (__name__ )
66
+
67
+
64
68
class PluginMetaDescription (TypedDict ):
65
69
"""Meta description of a plugin, as a dictionary.
66
70
@@ -620,14 +624,20 @@ def get_version(self) -> Optional[str]:
620
624
621
625
if (
622
626
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" )
625
629
):
630
+ dist_name = self .entry_point .dist .name
626
631
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
+ )
631
641
632
642
return version
633
643
Original file line number Diff line number Diff line change @@ -142,3 +142,21 @@ def test_folder_plugin_imports(plugin_folder):
142
142
handler = handlers .PyFilePlugin (plugin_folder )
143
143
handler .load ()
144
144
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