python311Packages.sopel: fix build

This commit is contained in:
natsukium 2023-07-28 12:28:45 +09:00
parent 35abc09040
commit 739d918f4e
No known key found for this signature in database
GPG key ID: 9EA45A31DB994C53
2 changed files with 103 additions and 0 deletions

View file

@ -27,6 +27,13 @@ buildPythonPackage rec {
hash = "sha256-IJ+ovLQv6/UU1oepmUQjzaWBG3Rdd3xvui7FjK85Urs=";
};
patches = [
# https://github.com/sopel-irc/sopel/issues/2401
# https://github.com/sopel-irc/sopel/commit/596adc44330939519784389cbb927435305ef758.patch
# rewrite the patch because there are too many patches needed to apply the above patch.
./python311-support.patch
];
propagatedBuildInputs = [
dnspython
geoip2

View file

@ -0,0 +1,96 @@
diff --git a/sopel/plugins/handlers.py b/sopel/plugins/handlers.py
index 76902aa0..05f0279d 100644
--- a/sopel/plugins/handlers.py
+++ b/sopel/plugins/handlers.py
@@ -46,20 +46,15 @@ from __future__ import absolute_import, division, print_function, unicode_litera
import imp
import importlib
+import importlib.util
import inspect
import itertools
import os
+import sys
from sopel import loader
from . import exceptions
-try:
- reload = importlib.reload
-except AttributeError:
- # py2: no reload function
- # TODO: imp is deprecated, to be removed when py2 support is dropped
- reload = imp.reload
-
class AbstractPluginHandler(object):
"""Base class for plugin handlers.
@@ -301,7 +296,7 @@ class PyModulePlugin(AbstractPluginHandler):
This method assumes the plugin is already loaded.
"""
- self._module = reload(self._module)
+ self._module = importlib.reload(self._module)
def is_loaded(self):
return self._module is not None
@@ -402,45 +397,31 @@ class PyFilePlugin(PyModulePlugin):
if good_file:
name = os.path.basename(filename)[:-3]
- module_type = imp.PY_SOURCE
+ spec = importlib.util.spec_from_file_location(
+ name,
+ filename,
+ )
elif good_dir:
name = os.path.basename(filename)
- module_type = imp.PKG_DIRECTORY
+ spec = importlib.util.spec_from_file_location(
+ name,
+ os.path.join(filename, '__init__.py'),
+ submodule_search_locations=filename,
+ )
else:
raise exceptions.PluginError('Invalid Sopel plugin: %s' % filename)
self.filename = filename
self.path = filename
- self.module_type = module_type
+ self.module_spec = spec
super(PyFilePlugin, self).__init__(name)
def _load(self):
- # The current implementation uses `imp.load_module` to perform the
- # load action, which also reloads the module. However, `imp` is
- # deprecated in Python 3, so that might need to be changed when the
- # support for Python 2 is dropped.
- #
- # However, the solution for Python 3 is non-trivial, since the
- # `importlib` built-in module does not have a similar function,
- # therefore requires to dive into its public internals
- # (``importlib.machinery`` and ``importlib.util``).
- #
- # All of that is doable, but represents a lot of work. As long as
- # Python 2 is supported, we can keep it for now.
- #
- # TODO: switch to ``importlib`` when Python2 support is dropped.
- if self.module_type == imp.PY_SOURCE:
- with open(self.path) as mod:
- description = ('.py', 'U', self.module_type)
- mod = imp.load_module(self.name, mod, self.path, description)
- elif self.module_type == imp.PKG_DIRECTORY:
- description = ('', '', self.module_type)
- mod = imp.load_module(self.name, None, self.path, description)
- else:
- raise TypeError('Unsupported module type')
-
- return mod
+ module = importlib.util.module_from_spec(self.module_spec)
+ sys.modules[self.name] = module
+ self.module_spec.loader.exec_module(module)
+ return module
def get_meta_description(self):
"""Retrieve a meta description for the plugin.