bpo-42403: Use @staticmethod in importlib (GH-23395)

Use @staticmethod on methods using @classmethod but don't use their
cls parameter on the following classes:

* BuiltinImporter
* FrozenImporter
* WindowsRegistryFinder
* PathFinder

Leave methods using @_requires_builtin or @_requires_frozen unchanged,
since this decorator requires the wrapped method to have an extra parameter
(cls or self).
This commit is contained in:
Victor Stinner 2020-11-20 14:44:02 +01:00 committed by GitHub
parent a6109ef68d
commit 3be8e220ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 2359 additions and 2360 deletions

View File

@ -761,16 +761,16 @@ class BuiltinImporter:
spec = cls.find_spec(fullname, path) spec = cls.find_spec(fullname, path)
return spec.loader if spec is not None else None return spec.loader if spec is not None else None
@classmethod @staticmethod
def create_module(self, spec): def create_module(spec):
"""Create a built-in module""" """Create a built-in module"""
if spec.name not in sys.builtin_module_names: if spec.name not in sys.builtin_module_names:
raise ImportError('{!r} is not a built-in module'.format(spec.name), raise ImportError('{!r} is not a built-in module'.format(spec.name),
name=spec.name) name=spec.name)
return _call_with_frames_removed(_imp.create_builtin, spec) return _call_with_frames_removed(_imp.create_builtin, spec)
@classmethod @staticmethod
def exec_module(self, module): def exec_module(module):
"""Exec a built-in module""" """Exec a built-in module"""
_call_with_frames_removed(_imp.exec_builtin, module) _call_with_frames_removed(_imp.exec_builtin, module)
@ -831,8 +831,8 @@ class FrozenImporter:
""" """
return cls if _imp.is_frozen(fullname) else None return cls if _imp.is_frozen(fullname) else None
@classmethod @staticmethod
def create_module(cls, spec): def create_module(spec):
"""Use default semantics for module creation.""" """Use default semantics for module creation."""
@staticmethod @staticmethod

View File

@ -754,8 +754,8 @@ class WindowsRegistryFinder:
'\\Modules\\{fullname}\\Debug') '\\Modules\\{fullname}\\Debug')
DEBUG_BUILD = (_MS_WINDOWS and '_d.pyd' in EXTENSION_SUFFIXES) DEBUG_BUILD = (_MS_WINDOWS and '_d.pyd' in EXTENSION_SUFFIXES)
@classmethod @staticmethod
def _open_registry(cls, key): def _open_registry(key):
try: try:
return winreg.OpenKey(winreg.HKEY_CURRENT_USER, key) return winreg.OpenKey(winreg.HKEY_CURRENT_USER, key)
except OSError: except OSError:
@ -1219,8 +1219,8 @@ class _NamespaceLoader:
def __init__(self, name, path, path_finder): def __init__(self, name, path, path_finder):
self._path = _NamespacePath(name, path, path_finder) self._path = _NamespacePath(name, path, path_finder)
@classmethod @staticmethod
def module_repr(cls, module): def module_repr(module):
"""Return repr for the module. """Return repr for the module.
The method is deprecated. The import machinery does the job itself. The method is deprecated. The import machinery does the job itself.
@ -1261,8 +1261,8 @@ class PathFinder:
"""Meta path finder for sys.path and package __path__ attributes.""" """Meta path finder for sys.path and package __path__ attributes."""
@classmethod @staticmethod
def invalidate_caches(cls): def invalidate_caches():
"""Call the invalidate_caches() method on all path entry finders """Call the invalidate_caches() method on all path entry finders
stored in sys.path_importer_caches (where implemented).""" stored in sys.path_importer_caches (where implemented)."""
for name, finder in list(sys.path_importer_cache.items()): for name, finder in list(sys.path_importer_cache.items()):
@ -1271,8 +1271,8 @@ class PathFinder:
elif hasattr(finder, 'invalidate_caches'): elif hasattr(finder, 'invalidate_caches'):
finder.invalidate_caches() finder.invalidate_caches()
@classmethod @staticmethod
def _path_hooks(cls, path): def _path_hooks(path):
"""Search sys.path_hooks for a finder for 'path'.""" """Search sys.path_hooks for a finder for 'path'."""
if sys.path_hooks is not None and not sys.path_hooks: if sys.path_hooks is not None and not sys.path_hooks:
_warnings.warn('sys.path_hooks is empty', ImportWarning) _warnings.warn('sys.path_hooks is empty', ImportWarning)
@ -1390,8 +1390,8 @@ class PathFinder:
return None return None
return spec.loader return spec.loader
@classmethod @staticmethod
def find_distributions(cls, *args, **kwargs): def find_distributions(*args, **kwargs):
""" """
Find distributions. Find distributions.

1421
Python/importlib.h generated

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff