Skip to content

Commit 10fe3b6

Browse files
committed
Merge branch 'dynamic-loading' into dynamic-loading-3.9
2 parents b1f09e1 + 92089b7 commit 10fe3b6

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

patch/Python/sitecustomize.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
# packages cross-platform. If the folder containing this file is on
33
# your PYTHONPATH when you invoke pip, pip will behave as if it were
44
# running on {{os}}.
5+
import distutils.ccompiler
6+
import distutils.unixccompiler
57
import os
68
import platform
79
import sys
810
import sysconfig
11+
import types
912

1013
# Make platform.system() return "{{os}}"
1114
def custom_system():
@@ -19,6 +22,71 @@ def custom_get_platform():
1922

2023
sysconfig.get_platform = custom_get_platform
2124

25+
# Make distutils raise errors if you try to use it to build modules.
26+
DISABLED_COMPILER_ERROR = "Cannot compile native modules"
27+
28+
distutils.ccompiler.get_default_compiler = lambda *args, **kwargs: "disabled"
29+
distutils.ccompiler.compiler_class["disabled"] = (
30+
"disabledcompiler",
31+
"DisabledCompiler",
32+
"Compiler disabled ({})".format(DISABLED_COMPILER_ERROR),
33+
)
34+
35+
36+
def disabled_compiler(prefix):
37+
# No need to give any more advice here: that will come from the higher-level code in pip.
38+
from distutils.errors import DistutilsPlatformError
39+
40+
raise DistutilsPlatformError("{}: {}".format(prefix, DISABLED_COMPILER_ERROR))
41+
42+
43+
class DisabledCompiler(distutils.ccompiler.CCompiler):
44+
compiler_type = "disabled"
45+
46+
def preprocess(*args, **kwargs):
47+
disabled_compiler("CCompiler.preprocess")
48+
49+
def compile(*args, **kwargs):
50+
disabled_compiler("CCompiler.compile")
51+
52+
def create_static_lib(*args, **kwargs):
53+
disabled_compiler("CCompiler.create_static_lib")
54+
55+
def link(*args, **kwargs):
56+
disabled_compiler("CCompiler.link")
57+
58+
59+
# To maximize the chance of the build getting as far as actually calling compile(), make
60+
# sure the class has all of the expected attributes.
61+
for name in [
62+
"src_extensions",
63+
"obj_extension",
64+
"static_lib_extension",
65+
"shared_lib_extension",
66+
"static_lib_format",
67+
"shared_lib_format",
68+
"exe_extension",
69+
]:
70+
setattr(
71+
DisabledCompiler, name, getattr(distutils.unixccompiler.UnixCCompiler, name)
72+
)
73+
74+
DisabledCompiler.executables = {
75+
name: [DISABLED_COMPILER_ERROR.replace(" ", "_")]
76+
for name in distutils.unixccompiler.UnixCCompiler.executables
77+
}
78+
79+
disabled_mod = types.ModuleType("distutils.disabledcompiler")
80+
disabled_mod.DisabledCompiler = DisabledCompiler
81+
sys.modules["distutils.disabledcompiler"] = disabled_mod
82+
83+
84+
# Try to disable native builds for packages which don't use the distutils native build
85+
# system at all (e.g. uwsgi), or only use it to wrap an external build script (e.g. pynacl).
86+
for tool in ["ar", "as", "cc", "cxx", "ld"]:
87+
os.environ[tool.upper()] = DISABLED_COMPILER_ERROR.replace(" ", "_")
88+
89+
2290
# Call the next sitecustomize script if there is one
2391
# (https://nedbatchelder.com/blog/201001/running_code_at_python_startup.html).
2492
del sys.modules["sitecustomize"]

0 commit comments

Comments
 (0)