Skip to content

Commit

Permalink
mitogen: Replace uses of deprecated pkgutil.find_loader()
Browse files Browse the repository at this point in the history
fixes #1111
  • Loading branch information
moreati committed Jan 12, 2025
1 parent fadcf5f commit 23939cb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 19 additions & 7 deletions mitogen/master.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 []


Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 23939cb

Please sign in to comment.