Skip to content

Commit 8a5f7ad

Browse files
authored
[3.7] bpo-38597: Never statically link extension initialization code on Windows (GH-18724) (GH-18759)
Automerge-Triggered-By: @zooba
1 parent 69ded39 commit 8a5f7ad

File tree

4 files changed

+13
-99
lines changed

4 files changed

+13
-99
lines changed

Lib/distutils/_msvccompiler.py

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,11 @@ def _find_vc2017():
9090
return None, None
9191

9292
def _find_vcvarsall(plat_spec):
93+
# bpo-38597: Removed vcruntime return value
9394
_, best_dir = _find_vc2017()
94-
vcruntime = None
95-
vcruntime_plat = 'x64' if 'amd64' in plat_spec else 'x86'
96-
if best_dir:
97-
vcredist = os.path.join(best_dir, "..", "..", "redist", "MSVC", "**",
98-
vcruntime_plat, "Microsoft.VC14*.CRT", "vcruntime140.dll")
99-
try:
100-
import glob
101-
vcruntime = glob.glob(vcredist, recursive=True)[-1]
102-
except (ImportError, OSError, LookupError):
103-
vcruntime = None
10495

10596
if not best_dir:
10697
best_version, best_dir = _find_vc2015()
107-
if best_version:
108-
vcruntime = os.path.join(best_dir, 'redist', vcruntime_plat,
109-
"Microsoft.VC140.CRT", "vcruntime140.dll")
11098

11199
if not best_dir:
112100
log.debug("No suitable Visual C++ version found")
@@ -117,11 +105,7 @@ def _find_vcvarsall(plat_spec):
117105
log.debug("%s cannot be found", vcvarsall)
118106
return None, None
119107

120-
if not vcruntime or not os.path.isfile(vcruntime):
121-
log.debug("%s cannot be found", vcruntime)
122-
vcruntime = None
123-
124-
return vcvarsall, vcruntime
108+
return vcvarsall, None
125109

126110
def _get_vc_env(plat_spec):
127111
if os.getenv("DISTUTILS_USE_SDK"):
@@ -130,7 +114,7 @@ def _get_vc_env(plat_spec):
130114
for key, value in os.environ.items()
131115
}
132116

133-
vcvarsall, vcruntime = _find_vcvarsall(plat_spec)
117+
vcvarsall, _ = _find_vcvarsall(plat_spec)
134118
if not vcvarsall:
135119
raise DistutilsPlatformError("Unable to find vcvarsall.bat")
136120

@@ -151,8 +135,6 @@ def _get_vc_env(plat_spec):
151135
if key and value
152136
}
153137

154-
if vcruntime:
155-
env['py_vcruntime_redist'] = vcruntime
156138
return env
157139

158140
def _find_exe(exe, paths=None):
@@ -180,12 +162,6 @@ def _find_exe(exe, paths=None):
180162
'win-amd64' : 'x86_amd64',
181163
}
182164

183-
# A set containing the DLLs that are guaranteed to be available for
184-
# all micro versions of this Python version. Known extension
185-
# dependencies that are not in this set will be copied to the output
186-
# path.
187-
_BUNDLED_DLLS = frozenset(['vcruntime140.dll'])
188-
189165
class MSVCCompiler(CCompiler) :
190166
"""Concrete class that implements an interface to Microsoft Visual C++,
191167
as defined by the CCompiler abstract class."""
@@ -249,7 +225,6 @@ def initialize(self, plat_name=None):
249225
self.rc = _find_exe("rc.exe", paths) # resource compiler
250226
self.mc = _find_exe("mc.exe", paths) # message compiler
251227
self.mt = _find_exe("mt.exe", paths) # message compiler
252-
self._vcruntime_redist = vc_env.get('py_vcruntime_redist', '')
253228

254229
for dir in vc_env.get('include', '').split(os.pathsep):
255230
if dir:
@@ -260,13 +235,12 @@ def initialize(self, plat_name=None):
260235
self.add_library_dir(dir.rstrip(os.sep))
261236

262237
self.preprocess_options = None
263-
# If vcruntime_redist is available, link against it dynamically. Otherwise,
264-
# use /MT[d] to build statically, then switch from libucrt[d].lib to ucrt[d].lib
265-
# later to dynamically link to ucrtbase but not vcruntime.
238+
# bpo-38597: Always compile with dynamic linking
239+
# Future releases of Python 3.x will include all past
240+
# versions of vcruntime*.dll for compatibility.
266241
self.compile_options = [
267-
'/nologo', '/Ox', '/W3', '/GL', '/DNDEBUG'
242+
'/nologo', '/Ox', '/W3', '/GL', '/DNDEBUG', '/MD'
268243
]
269-
self.compile_options.append('/MD' if self._vcruntime_redist else '/MT')
270244

271245
self.compile_options_debug = [
272246
'/nologo', '/Od', '/MDd', '/Zi', '/W3', '/D_DEBUG'
@@ -275,8 +249,6 @@ def initialize(self, plat_name=None):
275249
ldflags = [
276250
'/nologo', '/INCREMENTAL:NO', '/LTCG'
277251
]
278-
if not self._vcruntime_redist:
279-
ldflags.extend(('/nodefaultlib:libucrt.lib', 'ucrt.lib'))
280252

281253
ldflags_debug = [
282254
'/nologo', '/INCREMENTAL:NO', '/LTCG', '/DEBUG:FULL'
@@ -518,24 +490,11 @@ def link(self,
518490
try:
519491
log.debug('Executing "%s" %s', self.linker, ' '.join(ld_args))
520492
self.spawn([self.linker] + ld_args)
521-
self._copy_vcruntime(output_dir)
522493
except DistutilsExecError as msg:
523494
raise LinkError(msg)
524495
else:
525496
log.debug("skipping %s (up-to-date)", output_filename)
526497

527-
def _copy_vcruntime(self, output_dir):
528-
vcruntime = self._vcruntime_redist
529-
if not vcruntime or not os.path.isfile(vcruntime):
530-
return
531-
532-
if os.path.basename(vcruntime).lower() in _BUNDLED_DLLS:
533-
return
534-
535-
log.debug('Copying "%s"', vcruntime)
536-
vcruntime = shutil.copy(vcruntime, output_dir)
537-
os.chmod(vcruntime, stat.S_IWRITE)
538-
539498
def spawn(self, cmd):
540499
old_path = os.getenv('path')
541500
try:

Lib/distutils/tests/test_msvccompiler.py

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -32,57 +32,6 @@ def _find_vcvarsall(plat_spec):
3232
finally:
3333
_msvccompiler._find_vcvarsall = old_find_vcvarsall
3434

35-
def test_compiler_options(self):
36-
import distutils._msvccompiler as _msvccompiler
37-
# suppress path to vcruntime from _find_vcvarsall to
38-
# check that /MT is added to compile options
39-
old_find_vcvarsall = _msvccompiler._find_vcvarsall
40-
def _find_vcvarsall(plat_spec):
41-
return old_find_vcvarsall(plat_spec)[0], None
42-
_msvccompiler._find_vcvarsall = _find_vcvarsall
43-
try:
44-
compiler = _msvccompiler.MSVCCompiler()
45-
compiler.initialize()
46-
47-
self.assertIn('/MT', compiler.compile_options)
48-
self.assertNotIn('/MD', compiler.compile_options)
49-
finally:
50-
_msvccompiler._find_vcvarsall = old_find_vcvarsall
51-
52-
def test_vcruntime_copy(self):
53-
import distutils._msvccompiler as _msvccompiler
54-
# force path to a known file - it doesn't matter
55-
# what we copy as long as its name is not in
56-
# _msvccompiler._BUNDLED_DLLS
57-
old_find_vcvarsall = _msvccompiler._find_vcvarsall
58-
def _find_vcvarsall(plat_spec):
59-
return old_find_vcvarsall(plat_spec)[0], __file__
60-
_msvccompiler._find_vcvarsall = _find_vcvarsall
61-
try:
62-
tempdir = self.mkdtemp()
63-
compiler = _msvccompiler.MSVCCompiler()
64-
compiler.initialize()
65-
compiler._copy_vcruntime(tempdir)
66-
67-
self.assertTrue(os.path.isfile(os.path.join(
68-
tempdir, os.path.basename(__file__))))
69-
finally:
70-
_msvccompiler._find_vcvarsall = old_find_vcvarsall
71-
72-
def test_vcruntime_skip_copy(self):
73-
import distutils._msvccompiler as _msvccompiler
74-
75-
tempdir = self.mkdtemp()
76-
compiler = _msvccompiler.MSVCCompiler()
77-
compiler.initialize()
78-
dll = compiler._vcruntime_redist
79-
self.assertTrue(os.path.isfile(dll), dll or "<None>")
80-
81-
compiler._copy_vcruntime(tempdir)
82-
83-
self.assertFalse(os.path.isfile(os.path.join(
84-
tempdir, os.path.basename(dll))), dll or "<None>")
85-
8635
def test_get_vc_env_unicode(self):
8736
import distutils._msvccompiler as _msvccompiler
8837

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:mod:`distutils` will no longer statically link :file:`vcruntime140.dll`
2+
when a redistributable version is unavailable. All future releases of
3+
CPython will include a copy of this DLL to ensure distributed extensions can
4+
continue to load.

PCbuild/pythoncore.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,8 @@
469469
<VCRuntimeDLL Include="$(VCRedistDir)\**\vcruntime*.dll" />
470470
</ItemGroup>
471471
<Target Name="_CopyVCRuntime" AfterTargets="Build" Inputs="@(VCRuntimeDLL)" Outputs="$(OutDir)%(Filename)%(Extension)">
472+
<!-- bpo-38597: When we switch to another VCRuntime DLL, include vcruntime140.dll as well -->
473+
<Warning Text="A copy of vcruntime140.dll is also required" Condition="!$(VCToolsRedistVersion.StartsWith(`14.`))" />
472474
<Copy SourceFiles="%(VCRuntimeDLL.FullPath)" DestinationFolder="$(OutDir)" />
473475
</Target>
474476
<Target Name="_CleanVCRuntime" AfterTargets="Clean">

0 commit comments

Comments
 (0)