Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/setuptools/monkey.py
7757 views
"""1Monkey patching of distutils.2"""34import sys5import distutils.filelist6import platform7import types8import functools9from importlib import import_module10import inspect1112from setuptools.extern import six1314import setuptools1516__all__ = []17"""18Everything is private. Contact the project team19if you think you need this functionality.20"""212223def _get_mro(cls):24"""25Returns the bases classes for cls sorted by the MRO.2627Works around an issue on Jython where inspect.getmro will not return all28base classes if multiple classes share the same name. Instead, this29function will return a tuple containing the class itself, and the contents30of cls.__bases__. See https://github.com/pypa/setuptools/issues/1024.31"""32if platform.python_implementation() == "Jython":33return (cls,) + cls.__bases__34return inspect.getmro(cls)353637def get_unpatched(item):38lookup = (39get_unpatched_class if isinstance(item, six.class_types) else40get_unpatched_function if isinstance(item, types.FunctionType) else41lambda item: None42)43return lookup(item)444546def get_unpatched_class(cls):47"""Protect against re-patching the distutils if reloaded4849Also ensures that no other distutils extension monkeypatched the distutils50first.51"""52external_bases = (53cls54for cls in _get_mro(cls)55if not cls.__module__.startswith('setuptools')56)57base = next(external_bases)58if not base.__module__.startswith('distutils'):59msg = "distutils has already been patched by %r" % cls60raise AssertionError(msg)61return base626364def patch_all():65# we can't patch distutils.cmd, alas66distutils.core.Command = setuptools.Command6768has_issue_12885 = sys.version_info <= (3, 5, 3)6970if has_issue_12885:71# fix findall bug in distutils (http://bugs.python.org/issue12885)72distutils.filelist.findall = setuptools.findall7374needs_warehouse = (75sys.version_info < (2, 7, 13)76or77(3, 4) < sys.version_info < (3, 4, 6)78or79(3, 5) < sys.version_info <= (3, 5, 3)80)8182if needs_warehouse:83warehouse = 'https://upload.pypi.org/legacy/'84distutils.config.PyPIRCCommand.DEFAULT_REPOSITORY = warehouse8586_patch_distribution_metadata()8788# Install Distribution throughout the distutils89for module in distutils.dist, distutils.core, distutils.cmd:90module.Distribution = setuptools.dist.Distribution9192# Install the patched Extension93distutils.core.Extension = setuptools.extension.Extension94distutils.extension.Extension = setuptools.extension.Extension95if 'distutils.command.build_ext' in sys.modules:96sys.modules['distutils.command.build_ext'].Extension = (97setuptools.extension.Extension98)99100patch_for_msvc_specialized_compiler()101102103def _patch_distribution_metadata():104"""Patch write_pkg_file and read_pkg_file for higher metadata standards"""105for attr in ('write_pkg_file', 'read_pkg_file', 'get_metadata_version'):106new_val = getattr(setuptools.dist, attr)107setattr(distutils.dist.DistributionMetadata, attr, new_val)108109110def patch_func(replacement, target_mod, func_name):111"""112Patch func_name in target_mod with replacement113114Important - original must be resolved by name to avoid115patching an already patched function.116"""117original = getattr(target_mod, func_name)118119# set the 'unpatched' attribute on the replacement to120# point to the original.121vars(replacement).setdefault('unpatched', original)122123# replace the function in the original module124setattr(target_mod, func_name, replacement)125126127def get_unpatched_function(candidate):128return getattr(candidate, 'unpatched')129130131def patch_for_msvc_specialized_compiler():132"""133Patch functions in distutils to use standalone Microsoft Visual C++134compilers.135"""136# import late to avoid circular imports on Python < 3.5137msvc = import_module('setuptools.msvc')138139if platform.system() != 'Windows':140# Compilers only availables on Microsoft Windows141return142143def patch_params(mod_name, func_name):144"""145Prepare the parameters for patch_func to patch indicated function.146"""147repl_prefix = 'msvc9_' if 'msvc9' in mod_name else 'msvc14_'148repl_name = repl_prefix + func_name.lstrip('_')149repl = getattr(msvc, repl_name)150mod = import_module(mod_name)151if not hasattr(mod, func_name):152raise ImportError(func_name)153return repl, mod, func_name154155# Python 2.7 to 3.4156msvc9 = functools.partial(patch_params, 'distutils.msvc9compiler')157158# Python 3.5+159msvc14 = functools.partial(patch_params, 'distutils._msvccompiler')160161try:162# Patch distutils.msvc9compiler163patch_func(*msvc9('find_vcvarsall'))164patch_func(*msvc9('query_vcvarsall'))165except ImportError:166pass167168try:169# Patch distutils._msvccompiler._get_vc_env170patch_func(*msvc14('_get_vc_env'))171except ImportError:172pass173174try:175# Patch distutils._msvccompiler.gen_lib_options for Numpy176patch_func(*msvc14('gen_lib_options'))177except ImportError:178pass179180181