2
Monkey patching of distutils.
6
import distutils.filelist
12
from .py26compat import import_module
19
Everything is private. Contact the project team
20
if you think you need this functionality.
24
def get_unpatched(item):
26
get_unpatched_class if isinstance(item, six.class_types) else
27
get_unpatched_function if isinstance(item, types.FunctionType) else
33
def get_unpatched_class(cls):
34
"""Protect against re-patching the distutils if reloaded
36
Also ensures that no other distutils extension monkeypatched the distutils
41
for cls in inspect.getmro(cls)
42
if not cls.__module__.startswith('setuptools')
44
base = next(external_bases)
45
if not base.__module__.startswith('distutils'):
46
msg = "distutils has already been patched by %r" % cls
47
raise AssertionError(msg)
52
# we can't patch distutils.cmd, alas
53
distutils.core.Command = setuptools.Command
55
has_issue_12885 = sys.version_info <= (3, 5, 3)
58
# fix findall bug in distutils (http://bugs.python.org/issue12885)
59
distutils.filelist.findall = setuptools.findall
62
sys.version_info < (2, 7, 13)
64
(3, 0) < sys.version_info < (3, 3, 7)
66
(3, 4) < sys.version_info < (3, 4, 6)
68
(3, 5) < sys.version_info <= (3, 5, 3)
72
warehouse = 'https://upload.pypi.org/legacy/'
73
distutils.config.PyPIRCCommand.DEFAULT_REPOSITORY = warehouse
75
_patch_distribution_metadata_write_pkg_file()
76
_patch_distribution_metadata_write_pkg_info()
78
# Install Distribution throughout the distutils
79
for module in distutils.dist, distutils.core, distutils.cmd:
80
module.Distribution = setuptools.dist.Distribution
82
# Install the patched Extension
83
distutils.core.Extension = setuptools.extension.Extension
84
distutils.extension.Extension = setuptools.extension.Extension
85
if 'distutils.command.build_ext' in sys.modules:
86
sys.modules['distutils.command.build_ext'].Extension = (
87
setuptools.extension.Extension
90
patch_for_msvc_specialized_compiler()
93
def _patch_distribution_metadata_write_pkg_file():
94
"""Patch write_pkg_file to also write Requires-Python/Requires-External"""
95
distutils.dist.DistributionMetadata.write_pkg_file = (
96
setuptools.dist.write_pkg_file
100
def _patch_distribution_metadata_write_pkg_info():
102
Workaround issue #197 - Python 3 prior to 3.2.2 uses an environment-local
103
encoding to save the pkg_info. Monkey-patch its write_pkg_info method to
104
correct this undesirable behavior.
106
environment_local = (3,) <= sys.version_info[:3] < (3, 2, 2)
107
if not environment_local:
110
distutils.dist.DistributionMetadata.write_pkg_info = (
111
setuptools.dist.write_pkg_info
115
def patch_func(replacement, target_mod, func_name):
117
Patch func_name in target_mod with replacement
119
Important - original must be resolved by name to avoid
120
patching an already patched function.
122
original = getattr(target_mod, func_name)
124
# set the 'unpatched' attribute on the replacement to
125
# point to the original.
126
vars(replacement).setdefault('unpatched', original)
128
# replace the function in the original module
129
setattr(target_mod, func_name, replacement)
132
def get_unpatched_function(candidate):
133
return getattr(candidate, 'unpatched')
136
def patch_for_msvc_specialized_compiler():
138
Patch functions in distutils to use standalone Microsoft Visual C++
141
# import late to avoid circular imports on Python < 3.5
142
msvc = import_module('setuptools.msvc')
144
if platform.system() != 'Windows':
145
# Compilers only availables on Microsoft Windows
148
def patch_params(mod_name, func_name):
150
Prepare the parameters for patch_func to patch indicated function.
152
repl_prefix = 'msvc9_' if 'msvc9' in mod_name else 'msvc14_'
153
repl_name = repl_prefix + func_name.lstrip('_')
154
repl = getattr(msvc, repl_name)
155
mod = import_module(mod_name)
156
if not hasattr(mod, func_name):
157
raise ImportError(func_name)
158
return repl, mod, func_name
161
msvc9 = functools.partial(patch_params, 'distutils.msvc9compiler')
164
msvc14 = functools.partial(patch_params, 'distutils._msvccompiler')
167
# Patch distutils.msvc9compiler
168
patch_func(*msvc9('find_vcvarsall'))
169
patch_func(*msvc9('query_vcvarsall'))
174
# Patch distutils._msvccompiler._get_vc_env
175
patch_func(*msvc14('_get_vc_env'))
180
# Patch distutils._msvccompiler.gen_lib_options for Numpy
181
patch_func(*msvc14('gen_lib_options'))