Skip to content

Commit a0339c1

Browse files
committed
Fix cross-platform compilation using distutils._msvccompiler.MSVCCompiler
Actually use the `plat_name` param in `MSVCCompiler.initialize` Added tests
1 parent 3ac1adc commit a0339c1

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

distutils/_msvccompiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def initialize(self, plat_name=None):
284284
f"--plat-name must be one of {tuple(_vcvars_names)}"
285285
)
286286

287-
plat_spec = _get_vcvars_spec(get_host_platform(), get_platform())
287+
plat_spec = _get_vcvars_spec(get_host_platform(), plat_name)
288288

289289
vc_env = _get_vc_env(plat_spec)
290290
if not vc_env:

distutils/tests/test_msvccompiler.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import os
44
import sys
5+
import sysconfig
56
import threading
67
import unittest.mock as mock
78
from distutils import _msvccompiler
89
from distutils.errors import DistutilsPlatformError
910
from distutils.tests import support
11+
from distutils.util import get_platform
1012

1113
import pytest
1214

@@ -28,6 +30,28 @@ def _find_vcvarsall(plat_spec):
2830
'wont find this version',
2931
)
3032

33+
@pytest.mark.skipif(
34+
not sysconfig.get_platform().startswith("win"),
35+
reason="Only run test for non-mingw Windows platforms",
36+
)
37+
@pytest.mark.parametrize(
38+
"plat_name, expected",
39+
[
40+
("win-arm64", "win-arm64"),
41+
("win-amd64", "win-amd64"),
42+
(None, get_platform()),
43+
],
44+
)
45+
def test_cross_platform_compilation_paths(self, monkeypatch, plat_name, expected):
46+
compiler = _msvccompiler.MSVCCompiler()
47+
48+
# makes sure that the right target platform name is used
49+
def _get_vcvars_spec(host_platform, platform):
50+
assert platform == expected
51+
52+
monkeypatch.setattr(_msvccompiler, '_get_vcvars_spec', _get_vcvars_spec)
53+
compiler.initialize(plat_name)
54+
3155
@needs_winreg
3256
def test_get_vc_env_unicode(self):
3357
test_var = 'ṰḖṤṪ┅ṼẨṜ'

newsfragments/298.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix cross-platform compilation using `distutils._msvccompiler.MSVCCompiler` -- by :user:`saschanaz` and :user:`Avasam`

0 commit comments

Comments
 (0)