aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/importlib-metadata/py3/importlib_metadata/_compat.py
diff options
context:
space:
mode:
authornkozlovskiy <nmk@ydb.tech>2023-09-29 12:24:06 +0300
committernkozlovskiy <nmk@ydb.tech>2023-09-29 12:41:34 +0300
commite0e3e1717e3d33762ce61950504f9637a6e669ed (patch)
treebca3ff6939b10ed60c3d5c12439963a1146b9711 /contrib/python/importlib-metadata/py3/importlib_metadata/_compat.py
parent38f2c5852db84c7b4d83adfcb009eb61541d1ccd (diff)
downloadydb-e0e3e1717e3d33762ce61950504f9637a6e669ed.tar.gz
add ydb deps
Diffstat (limited to 'contrib/python/importlib-metadata/py3/importlib_metadata/_compat.py')
-rw-r--r--contrib/python/importlib-metadata/py3/importlib_metadata/_compat.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/contrib/python/importlib-metadata/py3/importlib_metadata/_compat.py b/contrib/python/importlib-metadata/py3/importlib_metadata/_compat.py
new file mode 100644
index 0000000000..a48b609be6
--- /dev/null
+++ b/contrib/python/importlib-metadata/py3/importlib_metadata/_compat.py
@@ -0,0 +1,70 @@
+import os
+import sys
+import platform
+
+from typing import Union
+
+
+__all__ = ['install', 'NullFinder']
+
+
+def install(flag):
+ def dec_install(cls):
+ """
+ Class decorator for installation on sys.meta_path.
+
+ Adds the backport DistributionFinder to sys.meta_path and
+ attempts to disable the finder functionality of the stdlib
+ DistributionFinder.
+ """
+ if flag:
+ sys.meta_path.append(cls())
+ disable_stdlib_finder()
+ return cls
+ return dec_install
+
+
+def disable_stdlib_finder():
+ """
+ Give the backport primacy for discovering path-based distributions
+ by monkey-patching the stdlib O_O.
+
+ See #91 for more background for rationale on this sketchy
+ behavior.
+ """
+
+ def matches(finder):
+ return getattr(
+ finder, '__module__', None
+ ) == '_frozen_importlib_external' and hasattr(finder, 'find_distributions')
+
+ for finder in filter(matches, sys.meta_path): # pragma: nocover
+ del finder.find_distributions
+
+
+class NullFinder:
+ """
+ A "Finder" (aka "MetaClassFinder") that never finds any modules,
+ but may find distributions.
+ """
+
+ @staticmethod
+ def find_spec(*args, **kwargs):
+ return None
+
+
+def pypy_partial(val):
+ """
+ Adjust for variable stacklevel on partial under PyPy.
+
+ Workaround for #327.
+ """
+ is_pypy = platform.python_implementation() == 'PyPy'
+ return val + is_pypy
+
+
+if sys.version_info >= (3, 9):
+ StrPath = Union[str, os.PathLike[str]]
+else:
+ # PathLike is only subscriptable at runtime in 3.9+
+ StrPath = Union[str, "os.PathLike[str]"] # pragma: no cover