From 23939cb85e170f96863b91dde8b17cdfb2faf121 Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Thu, 9 Jan 2025 15:47:35 +0000 Subject: [PATCH] mitogen: Replace uses of deprecated `pkgutil.find_loader()` fixes #1111 --- docs/changelog.rst | 2 ++ mitogen/master.py | 26 +++++++++++++++++++------- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index f35ea4930..3c99b684b 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -22,6 +22,8 @@ In progress (unreleased) ------------------------ * :gh:issue:`1209` docs: Fix Netlify build of website +* :gh:issue:`1111` :mod:`mitogen`: Replace uses of deprecated + :py:func:`pkgutil.find_loader` v0.3.20 (2025-01-07) diff --git a/mitogen/master.py b/mitogen/master.py index 865c9dc14..927ccaf11 100644 --- a/mitogen/master.py +++ b/mitogen/master.py @@ -54,21 +54,33 @@ import importlib.machinery import importlib.util from _imp import is_builtin as _is_builtin + + def _find_loader(fullname): + try: + maybe_spec = importlib.util.find_spec(fullname) + except (ImportError, AttributeError, TypeError, ValueError): + exc = sys.exc_info()[1] + raise ImportError(*exc.args) + try: + return maybe_spec.loader + except AttributeError: + return None except ImportError: # Python < 3.4, PEP 302 Import Hooks import imp from imp import is_builtin as _is_builtin + try: + from pkgutil import find_loader as _find_loader + except ImportError: + # Python < 2.5 + from mitogen.compat.pkgutil import find_loader as _find_loader + try: import sysconfig except ImportError: sysconfig = None -if not hasattr(pkgutil, 'find_loader'): - # find_loader() was new in >=2.5, but the modern pkgutil.py syntax has - # been kept intentionally 2.3 compatible so we can reuse it. - from mitogen.compat import pkgutil - import mitogen import mitogen.core import mitogen.minify @@ -175,7 +187,7 @@ def get_child_modules(path, fullname): return [to_text(name) for _, name, _ in pkgutil.iter_modules([mod_path])] else: # we loaded some weird package in memory, so we'll see if it has a custom loader we can use - loader = pkgutil.find_loader(fullname) + loader = _find_loader(fullname) return [to_text(name) for name, _ in loader.iter_modules(None)] if loader else [] @@ -528,7 +540,7 @@ def find(self, fullname): # then the containing package is imported. # Pre-'import spec' this returned None, in Python3.6 it raises # ImportError. - loader = pkgutil.find_loader(fullname) + loader = _find_loader(fullname) except ImportError: e = sys.exc_info()[1] LOG.debug('%r: find_loader(%r) failed: %s', self, fullname, e)