aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/Lib/importlib
diff options
context:
space:
mode:
authorshadchin <shadchin@yandex-team.com>2024-04-28 21:17:44 +0300
committershadchin <shadchin@yandex-team.com>2024-04-28 21:25:54 +0300
commita55d99a3eb72f90355bc146baeda18aa7eb97352 (patch)
treeb17cfed786effe8b81bba022239d6729f716fbeb /contrib/tools/python3/Lib/importlib
parent67bf49d08acf1277eff4c336021ac22d964bb4c4 (diff)
downloadydb-a55d99a3eb72f90355bc146baeda18aa7eb97352.tar.gz
Update Python 3 to 3.12.3
7d09de7d8b99ea2be554ef0fc61276942ca9c2e1
Diffstat (limited to 'contrib/tools/python3/Lib/importlib')
-rw-r--r--contrib/tools/python3/Lib/importlib/_bootstrap_external.py3
-rw-r--r--contrib/tools/python3/Lib/importlib/metadata/__init__.py1
-rw-r--r--contrib/tools/python3/Lib/importlib/resources/simple.py2
-rw-r--r--contrib/tools/python3/Lib/importlib/util.py82
4 files changed, 56 insertions, 32 deletions
diff --git a/contrib/tools/python3/Lib/importlib/_bootstrap_external.py b/contrib/tools/python3/Lib/importlib/_bootstrap_external.py
index e6f75a9f6f..61dafc0f4c 100644
--- a/contrib/tools/python3/Lib/importlib/_bootstrap_external.py
+++ b/contrib/tools/python3/Lib/importlib/_bootstrap_external.py
@@ -1450,6 +1450,9 @@ class PathFinder:
# https://bugs.python.org/issue45703
_NamespacePath._epoch += 1
+ from importlib.metadata import MetadataPathFinder
+ MetadataPathFinder.invalidate_caches()
+
@staticmethod
def _path_hooks(path):
"""Search sys.path_hooks for a finder for 'path'."""
diff --git a/contrib/tools/python3/Lib/importlib/metadata/__init__.py b/contrib/tools/python3/Lib/importlib/metadata/__init__.py
index 82e0ce1b28..54156e93af 100644
--- a/contrib/tools/python3/Lib/importlib/metadata/__init__.py
+++ b/contrib/tools/python3/Lib/importlib/metadata/__init__.py
@@ -795,6 +795,7 @@ class MetadataPathFinder(DistributionFinder):
path.search(prepared) for path in map(FastPath, paths)
)
+ @classmethod
def invalidate_caches(cls):
FastPath.__new__.cache_clear()
diff --git a/contrib/tools/python3/Lib/importlib/resources/simple.py b/contrib/tools/python3/Lib/importlib/resources/simple.py
index 7770c922c8..96f117fec6 100644
--- a/contrib/tools/python3/Lib/importlib/resources/simple.py
+++ b/contrib/tools/python3/Lib/importlib/resources/simple.py
@@ -88,7 +88,7 @@ class ResourceHandle(Traversable):
def open(self, mode='r', *args, **kwargs):
stream = self.parent.reader.open_binary(self.name)
if 'b' not in mode:
- stream = io.TextIOWrapper(*args, **kwargs)
+ stream = io.TextIOWrapper(stream, *args, **kwargs)
return stream
def joinpath(self, name):
diff --git a/contrib/tools/python3/Lib/importlib/util.py b/contrib/tools/python3/Lib/importlib/util.py
index f4d6e82331..3743e6aa91 100644
--- a/contrib/tools/python3/Lib/importlib/util.py
+++ b/contrib/tools/python3/Lib/importlib/util.py
@@ -13,6 +13,7 @@ from ._bootstrap_external import spec_from_file_location
import _imp
import sys
+import threading
import types
@@ -145,7 +146,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 +172,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):
@@ -244,5 +262,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