Merge pull request #307098 from r-ryantm/auto-update/cilium-cli
[NixPkgs.git] / pkgs / development / python-modules / sopel / python311-support.patch
blobdfa0283409acd71d9cbf78cd10f61a0edc310db3
1 diff --git a/sopel/plugins/handlers.py b/sopel/plugins/handlers.py
2 index 76902aa0..05f0279d 100644
3 --- a/sopel/plugins/handlers.py
4 +++ b/sopel/plugins/handlers.py
5 @@ -46,20 +46,15 @@ from __future__ import absolute_import, division, print_function, unicode_litera
7 import imp
8 import importlib
9 +import importlib.util
10 import inspect
11 import itertools
12 import os
13 +import sys
15 from sopel import loader
16 from . import exceptions
18 -try:
19 - reload = importlib.reload
20 -except AttributeError:
21 - # py2: no reload function
22 - # TODO: imp is deprecated, to be removed when py2 support is dropped
23 - reload = imp.reload
26 class AbstractPluginHandler(object):
27 """Base class for plugin handlers.
28 @@ -301,7 +296,7 @@ class PyModulePlugin(AbstractPluginHandler):
30 This method assumes the plugin is already loaded.
31 """
32 - self._module = reload(self._module)
33 + self._module = importlib.reload(self._module)
35 def is_loaded(self):
36 return self._module is not None
37 @@ -402,45 +397,31 @@ class PyFilePlugin(PyModulePlugin):
39 if good_file:
40 name = os.path.basename(filename)[:-3]
41 - module_type = imp.PY_SOURCE
42 + spec = importlib.util.spec_from_file_location(
43 + name,
44 + filename,
45 + )
46 elif good_dir:
47 name = os.path.basename(filename)
48 - module_type = imp.PKG_DIRECTORY
49 + spec = importlib.util.spec_from_file_location(
50 + name,
51 + os.path.join(filename, '__init__.py'),
52 + submodule_search_locations=filename,
53 + )
54 else:
55 raise exceptions.PluginError('Invalid Sopel plugin: %s' % filename)
57 self.filename = filename
58 self.path = filename
59 - self.module_type = module_type
60 + self.module_spec = spec
62 super(PyFilePlugin, self).__init__(name)
64 def _load(self):
65 - # The current implementation uses `imp.load_module` to perform the
66 - # load action, which also reloads the module. However, `imp` is
67 - # deprecated in Python 3, so that might need to be changed when the
68 - # support for Python 2 is dropped.
69 - #
70 - # However, the solution for Python 3 is non-trivial, since the
71 - # `importlib` built-in module does not have a similar function,
72 - # therefore requires to dive into its public internals
73 - # (``importlib.machinery`` and ``importlib.util``).
74 - #
75 - # All of that is doable, but represents a lot of work. As long as
76 - # Python 2 is supported, we can keep it for now.
77 - #
78 - # TODO: switch to ``importlib`` when Python2 support is dropped.
79 - if self.module_type == imp.PY_SOURCE:
80 - with open(self.path) as mod:
81 - description = ('.py', 'U', self.module_type)
82 - mod = imp.load_module(self.name, mod, self.path, description)
83 - elif self.module_type == imp.PKG_DIRECTORY:
84 - description = ('', '', self.module_type)
85 - mod = imp.load_module(self.name, None, self.path, description)
86 - else:
87 - raise TypeError('Unsupported module type')
89 - return mod
90 + module = importlib.util.module_from_spec(self.module_spec)
91 + sys.modules[self.name] = module
92 + self.module_spec.loader.exec_module(module)
93 + return module
95 def get_meta_description(self):
96 """Retrieve a meta description for the plugin.