update
This commit is contained in:
84
.CondaPkg/env/Lib/importlib/util.py
vendored
84
.CondaPkg/env/Lib/importlib/util.py
vendored
@@ -145,7 +145,7 @@ class _incompatible_extension_module_restrictions:
|
||||
|
||||
You can get the same effect as this function by implementing the
|
||||
basic interface of multi-phase init (PEP 489) and lying about
|
||||
support for mulitple interpreters (or per-interpreter GIL).
|
||||
support for multiple interpreters (or per-interpreter GIL).
|
||||
"""
|
||||
|
||||
def __init__(self, *, disable_check):
|
||||
@@ -171,36 +171,53 @@ class _LazyModule(types.ModuleType):
|
||||
|
||||
def __getattribute__(self, attr):
|
||||
"""Trigger the load of the module and return the attribute."""
|
||||
# All module metadata must be garnered from __spec__ in order to avoid
|
||||
# using mutated values.
|
||||
# Stop triggering this method.
|
||||
self.__class__ = types.ModuleType
|
||||
# Get the original name to make sure no object substitution occurred
|
||||
# in sys.modules.
|
||||
original_name = self.__spec__.name
|
||||
# Figure out exactly what attributes were mutated between the creation
|
||||
# of the module and now.
|
||||
attrs_then = self.__spec__.loader_state['__dict__']
|
||||
attrs_now = self.__dict__
|
||||
attrs_updated = {}
|
||||
for key, value in attrs_now.items():
|
||||
# Code that set the attribute may have kept a reference to the
|
||||
# assigned object, making identity more important than equality.
|
||||
if key not in attrs_then:
|
||||
attrs_updated[key] = value
|
||||
elif id(attrs_now[key]) != id(attrs_then[key]):
|
||||
attrs_updated[key] = value
|
||||
self.__spec__.loader.exec_module(self)
|
||||
# If exec_module() was used directly there is no guarantee the module
|
||||
# object was put into sys.modules.
|
||||
if original_name in sys.modules:
|
||||
if id(self) != id(sys.modules[original_name]):
|
||||
raise ValueError(f"module object for {original_name!r} "
|
||||
"substituted in sys.modules during a lazy "
|
||||
"load")
|
||||
# Update after loading since that's what would happen in an eager
|
||||
# loading situation.
|
||||
self.__dict__.update(attrs_updated)
|
||||
__spec__ = object.__getattribute__(self, '__spec__')
|
||||
loader_state = __spec__.loader_state
|
||||
with loader_state['lock']:
|
||||
# Only the first thread to get the lock should trigger the load
|
||||
# and reset the module's class. The rest can now getattr().
|
||||
if object.__getattribute__(self, '__class__') is _LazyModule:
|
||||
# Reentrant calls from the same thread must be allowed to proceed without
|
||||
# triggering the load again.
|
||||
# exec_module() and self-referential imports are the primary ways this can
|
||||
# happen, but in any case we must return something to avoid deadlock.
|
||||
if loader_state['is_loading']:
|
||||
return object.__getattribute__(self, attr)
|
||||
loader_state['is_loading'] = True
|
||||
|
||||
__dict__ = object.__getattribute__(self, '__dict__')
|
||||
|
||||
# All module metadata must be gathered from __spec__ in order to avoid
|
||||
# using mutated values.
|
||||
# Get the original name to make sure no object substitution occurred
|
||||
# in sys.modules.
|
||||
original_name = __spec__.name
|
||||
# Figure out exactly what attributes were mutated between the creation
|
||||
# of the module and now.
|
||||
attrs_then = loader_state['__dict__']
|
||||
attrs_now = __dict__
|
||||
attrs_updated = {}
|
||||
for key, value in attrs_now.items():
|
||||
# Code that set an attribute may have kept a reference to the
|
||||
# assigned object, making identity more important than equality.
|
||||
if key not in attrs_then:
|
||||
attrs_updated[key] = value
|
||||
elif id(attrs_now[key]) != id(attrs_then[key]):
|
||||
attrs_updated[key] = value
|
||||
__spec__.loader.exec_module(self)
|
||||
# If exec_module() was used directly there is no guarantee the module
|
||||
# object was put into sys.modules.
|
||||
if original_name in sys.modules:
|
||||
if id(self) != id(sys.modules[original_name]):
|
||||
raise ValueError(f"module object for {original_name!r} "
|
||||
"substituted in sys.modules during a lazy "
|
||||
"load")
|
||||
# Update after loading since that's what would happen in an eager
|
||||
# loading situation.
|
||||
__dict__.update(attrs_updated)
|
||||
# Finally, stop triggering this method.
|
||||
self.__class__ = types.ModuleType
|
||||
|
||||
return getattr(self, attr)
|
||||
|
||||
def __delattr__(self, attr):
|
||||
@@ -235,6 +252,9 @@ class LazyLoader(Loader):
|
||||
|
||||
def exec_module(self, module):
|
||||
"""Make the module load lazily."""
|
||||
# Threading is only needed for lazy loading, and importlib.util can
|
||||
# be pulled in at interpreter startup, so defer until needed.
|
||||
import threading
|
||||
module.__spec__.loader = self.loader
|
||||
module.__loader__ = self.loader
|
||||
# Don't need to worry about deep-copying as trying to set an attribute
|
||||
@@ -244,5 +264,7 @@ class LazyLoader(Loader):
|
||||
loader_state = {}
|
||||
loader_state['__dict__'] = module.__dict__.copy()
|
||||
loader_state['__class__'] = module.__class__
|
||||
loader_state['lock'] = threading.RLock()
|
||||
loader_state['is_loading'] = False
|
||||
module.__spec__.loader_state = loader_state
|
||||
module.__class__ = _LazyModule
|
||||
|
||||
Reference in New Issue
Block a user