Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Update importlib/test_importlib from CPython 3.10.6 #3919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Lib/importlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import _frozen_importlib_external as _bootstrap_external
except ImportError:
from . import _bootstrap_external
_bootstrap_external._setup(_bootstrap)
_bootstrap_external._set_bootstrap_module(_bootstrap)
_bootstrap._bootstrap_external = _bootstrap_external
else:
_bootstrap_external.__name__ = 'importlib._bootstrap_external'
Expand All @@ -54,7 +54,6 @@
# Fully bootstrapped at this point, import whatever you like, circular
# dependencies and startup overhead minimisation permitting :)

import types
import warnings


Expand All @@ -79,8 +78,8 @@ def find_loader(name, path=None):
This function is deprecated in favor of importlib.util.find_spec().

"""
warnings.warn('Deprecated since Python 3.4. '
'Use importlib.util.find_spec() instead.',
warnings.warn('Deprecated since Python 3.4 and slated for removal in '
'Python 3.12; use importlib.util.find_spec() instead',
DeprecationWarning, stacklevel=2)
try:
loader = sys.modules[name].__loader__
Expand Down Expand Up @@ -136,12 +135,13 @@ def reload(module):
The module must have been successfully imported before.

"""
if not module or not isinstance(module, types.ModuleType):
raise TypeError("reload() argument must be a module")
try:
name = module.__spec__.name
except AttributeError:
name = module.__name__
try:
name = module.__name__
except AttributeError:
raise TypeError("reload() argument must be a module")

if sys.modules.get(name) is not module:
msg = "module {} not in sys.modules"
Expand Down
54 changes: 54 additions & 0 deletions Lib/importlib/_abc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""Subset of importlib.abc used to reduce importlib.util imports."""
from . import _bootstrap
import abc
import warnings


class Loader(metaclass=abc.ABCMeta):

"""Abstract base class for import loaders."""

def create_module(self, spec):
"""Return a module to initialize and into which to load.

This method should raise ImportError if anything prevents it
from creating a new module. It may return None to indicate
that the spec should create the new module.
"""
# By default, defer to default semantics for the new module.
return None

# We don't define exec_module() here since that would break
# hasattr checks we do to support backward compatibility.

def load_module(self, fullname):
"""Return the loaded module.

The module must be added to sys.modules and have import-related
attributes set properly. The fullname is a str.

ImportError is raised on failure.

This method is deprecated in favor of loader.exec_module(). If
exec_module() exists then it is used to provide a backwards-compatible
functionality for this method.

"""
if not hasattr(self, 'exec_module'):
raise ImportError
# Warning implemented in _load_module_shim().
return _bootstrap._load_module_shim(self, fullname)

def module_repr(self, module):
"""Return a module's repr.

Used by the module type when the method does not raise
NotImplementedError.

This method is deprecated.

"""
warnings.warn("importlib.abc.Loader.module_repr() is deprecated and "
"slated for removal in Python 3.12", DeprecationWarning)
# The exception will cause ModuleType.__repr__ to ignore this method.
raise NotImplementedError
83 changes: 83 additions & 0 deletions Lib/importlib/_adapters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from contextlib import suppress

from . import abc


class SpecLoaderAdapter:
"""
Adapt a package spec to adapt the underlying loader.
"""

def __init__(self, spec, adapter=lambda spec: spec.loader):
self.spec = spec
self.loader = adapter(spec)

def __getattr__(self, name):
return getattr(self.spec, name)


class TraversableResourcesLoader:
"""
Adapt a loader to provide TraversableResources.
"""

def __init__(self, spec):
self.spec = spec

def get_resource_reader(self, name):
return DegenerateFiles(self.spec)._native()


class DegenerateFiles:
"""
Adapter for an existing or non-existant resource reader
to provide a degenerate .files().
"""

class Path(abc.Traversable):
def iterdir(self):
return iter(())

def is_dir(self):
return False

is_file = exists = is_dir # type: ignore

def joinpath(self, other):
return DegenerateFiles.Path()

@property
def name(self):
return ''

def open(self, mode='rb', *args, **kwargs):
raise ValueError()

def __init__(self, spec):
self.spec = spec

@property
def _reader(self):
with suppress(AttributeError):
return self.spec.loader.get_resource_reader(self.spec.name)

def _native(self):
"""
Return the native reader if it supports files().
"""
reader = self._reader
return reader if hasattr(reader, 'files') else self

def __getattr__(self, attr):
return getattr(self._reader, attr)

def files(self):
return DegenerateFiles.Path()


def wrap_spec(package):
"""
Construct a package spec with traversable compatibility
on the spec/loader/reader.
"""
return SpecLoaderAdapter(package.__spec__, TraversableResourcesLoader)
Loading