Skip to content

Commit bb018f1

Browse files
authored
Merge pull request #104 from lazka/get-platform-match-sysconfig
util.get_host_platform: use sysconfig.get_platform (v2)
2 parents bf8eab6 + 057adb2 commit bb018f1

File tree

2 files changed

+43
-180
lines changed

2 files changed

+43
-180
lines changed

distutils/tests/test_util.py

Lines changed: 20 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import sys
44
import unittest
5+
import sysconfig as stdlib_sysconfig
56
from copy import copy
67
from test.support import run_unittest
78
from unittest import mock
@@ -10,12 +11,10 @@
1011
from distutils.util import (get_platform, convert_path, change_root,
1112
check_environ, split_quoted, strtobool,
1213
rfc822_escape, byte_compile,
13-
grok_environment_error)
14+
grok_environment_error, get_host_platform)
1415
from distutils import util # used to patch _environ_checked
15-
from distutils.sysconfig import get_config_vars
1616
from distutils import sysconfig
1717
from distutils.tests import support
18-
import _osx_support
1918

2019
class UtilTestCase(support.EnvironGuard, unittest.TestCase):
2120

@@ -63,110 +62,26 @@ def _set_uname(self, uname):
6362
def _get_uname(self):
6463
return self._uname
6564

66-
def test_get_platform(self):
67-
68-
# windows XP, 32bits
69-
os.name = 'nt'
70-
sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
71-
'[MSC v.1310 32 bit (Intel)]')
72-
sys.platform = 'win32'
73-
self.assertEqual(get_platform(), 'win32')
74-
75-
# windows XP, amd64
76-
os.name = 'nt'
77-
sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
78-
'[MSC v.1310 32 bit (Amd64)]')
79-
sys.platform = 'win32'
80-
self.assertEqual(get_platform(), 'win-amd64')
81-
82-
# macbook
83-
os.name = 'posix'
84-
sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) '
85-
'\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]')
86-
sys.platform = 'darwin'
87-
self._set_uname(('Darwin', 'macziade', '8.11.1',
88-
('Darwin Kernel Version 8.11.1: '
89-
'Wed Oct 10 18:23:28 PDT 2007; '
90-
'root:xnu-792.25.20~1/RELEASE_I386'), 'i386'))
91-
_osx_support._remove_original_values(get_config_vars())
92-
get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
93-
94-
get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
95-
'-fwrapv -O3 -Wall -Wstrict-prototypes')
96-
97-
cursize = sys.maxsize
98-
sys.maxsize = (2 ** 31)-1
99-
try:
100-
self.assertEqual(get_platform(), 'macosx-10.3-i386')
101-
finally:
102-
sys.maxsize = cursize
103-
104-
# macbook with fat binaries (fat, universal or fat64)
105-
_osx_support._remove_original_values(get_config_vars())
106-
get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.4'
107-
get_config_vars()['CFLAGS'] = ('-arch ppc -arch i386 -isysroot '
108-
'/Developer/SDKs/MacOSX10.4u.sdk '
109-
'-fno-strict-aliasing -fno-common '
110-
'-dynamic -DNDEBUG -g -O3')
111-
112-
self.assertEqual(get_platform(), 'macosx-10.4-fat')
113-
114-
_osx_support._remove_original_values(get_config_vars())
115-
os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.1'
116-
self.assertEqual(get_platform(), 'macosx-10.4-fat')
117-
65+
def test_get_host_platform(self):
66+
with unittest.mock.patch('os.name', 'nt'):
67+
with unittest.mock.patch('sys.version', '... [... (ARM64)]'):
68+
self.assertEqual(get_host_platform(), 'win-arm64')
69+
with unittest.mock.patch('sys.version', '... [... (ARM)]'):
70+
self.assertEqual(get_host_platform(), 'win-arm32')
11871

119-
_osx_support._remove_original_values(get_config_vars())
120-
get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch i386 -isysroot '
121-
'/Developer/SDKs/MacOSX10.4u.sdk '
122-
'-fno-strict-aliasing -fno-common '
123-
'-dynamic -DNDEBUG -g -O3')
72+
with unittest.mock.patch('sys.version_info', (3, 9, 0, 'final', 0)):
73+
self.assertEqual(get_host_platform(), stdlib_sysconfig.get_platform())
12474

125-
self.assertEqual(get_platform(), 'macosx-10.4-intel')
126-
127-
_osx_support._remove_original_values(get_config_vars())
128-
get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc -arch i386 -isysroot '
129-
'/Developer/SDKs/MacOSX10.4u.sdk '
130-
'-fno-strict-aliasing -fno-common '
131-
'-dynamic -DNDEBUG -g -O3')
132-
self.assertEqual(get_platform(), 'macosx-10.4-fat3')
133-
134-
_osx_support._remove_original_values(get_config_vars())
135-
get_config_vars()['CFLAGS'] = ('-arch ppc64 -arch x86_64 -arch ppc -arch i386 -isysroot '
136-
'/Developer/SDKs/MacOSX10.4u.sdk '
137-
'-fno-strict-aliasing -fno-common '
138-
'-dynamic -DNDEBUG -g -O3')
139-
self.assertEqual(get_platform(), 'macosx-10.4-universal')
140-
141-
_osx_support._remove_original_values(get_config_vars())
142-
get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc64 -isysroot '
143-
'/Developer/SDKs/MacOSX10.4u.sdk '
144-
'-fno-strict-aliasing -fno-common '
145-
'-dynamic -DNDEBUG -g -O3')
146-
147-
self.assertEqual(get_platform(), 'macosx-10.4-fat64')
148-
149-
for arch in ('ppc', 'i386', 'x86_64', 'ppc64'):
150-
_osx_support._remove_original_values(get_config_vars())
151-
get_config_vars()['CFLAGS'] = ('-arch %s -isysroot '
152-
'/Developer/SDKs/MacOSX10.4u.sdk '
153-
'-fno-strict-aliasing -fno-common '
154-
'-dynamic -DNDEBUG -g -O3'%(arch,))
155-
156-
self.assertEqual(get_platform(), 'macosx-10.4-%s'%(arch,))
157-
158-
159-
# linux debian sarge
160-
os.name = 'posix'
161-
sys.version = ('2.3.5 (#1, Jul 4 2007, 17:28:59) '
162-
'\n[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]')
163-
sys.platform = 'linux2'
164-
self._set_uname(('Linux', 'aglae', '2.6.21.1dedibox-r7',
165-
'#1 Mon Apr 30 17:25:38 CEST 2007', 'i686'))
166-
167-
self.assertEqual(get_platform(), 'linux-i686')
168-
169-
# XXX more platforms to tests here
75+
def test_get_platform(self):
76+
with unittest.mock.patch('os.name', 'nt'):
77+
with unittest.mock.patch.dict('os.environ', {'VSCMD_ARG_TGT_ARCH': 'x86'}):
78+
self.assertEqual(get_platform(), 'win32')
79+
with unittest.mock.patch.dict('os.environ', {'VSCMD_ARG_TGT_ARCH': 'x64'}):
80+
self.assertEqual(get_platform(), 'win-amd64')
81+
with unittest.mock.patch.dict('os.environ', {'VSCMD_ARG_TGT_ARCH': 'arm'}):
82+
self.assertEqual(get_platform(), 'win-arm32')
83+
with unittest.mock.patch.dict('os.environ', {'VSCMD_ARG_TGT_ARCH': 'arm64'}):
84+
self.assertEqual(get_platform(), 'win-arm64')
17085

17186
def test_convert_path(self):
17287
# linux/mac

distutils/util.py

Lines changed: 23 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import importlib.util
1010
import string
1111
import sys
12+
import sysconfig
1213
from distutils.errors import DistutilsPlatformError
1314
from distutils.dep_util import newer
1415
from distutils.spawn import spawn
@@ -20,82 +21,29 @@
2021
def get_host_platform():
2122
"""Return a string that identifies the current platform. This is used mainly to
2223
distinguish platform-specific build directories and platform-specific built
23-
distributions. Typically includes the OS name and version and the
24-
architecture (as supplied by 'os.uname()'), although the exact information
25-
included depends on the OS; eg. on Linux, the kernel version isn't
26-
particularly important.
27-
28-
Examples of returned values:
29-
linux-i586
30-
linux-alpha (?)
31-
solaris-2.6-sun4u
32-
33-
Windows will return one of:
34-
win-amd64 (64bit Windows on AMD64 (aka x86_64, Intel64, EM64T, etc)
35-
win32 (all others - specifically, sys.platform is returned)
36-
37-
For other non-POSIX platforms, currently just returns 'sys.platform'.
38-
24+
distributions.
3925
"""
40-
if os.name == 'nt':
41-
if 'amd64' in sys.version.lower():
42-
return 'win-amd64'
43-
if '(arm)' in sys.version.lower():
44-
return 'win-arm32'
45-
if '(arm64)' in sys.version.lower():
46-
return 'win-arm64'
47-
return sys.platform
48-
49-
# Set for cross builds explicitly
50-
if "_PYTHON_HOST_PLATFORM" in os.environ:
51-
return os.environ["_PYTHON_HOST_PLATFORM"]
52-
53-
if os.name != "posix" or not hasattr(os, 'uname'):
54-
# XXX what about the architecture? NT is Intel or Alpha,
55-
# Mac OS is M68k or PPC, etc.
56-
return sys.platform
57-
58-
# Try to distinguish various flavours of Unix
59-
60-
(osname, host, release, version, machine) = os.uname()
61-
62-
# Convert the OS name to lowercase, remove '/' characters, and translate
63-
# spaces (for "Power Macintosh")
64-
osname = osname.lower().replace('/', '')
65-
machine = machine.replace(' ', '_')
66-
machine = machine.replace('/', '-')
67-
68-
if osname[:5] == "linux":
69-
# At least on Linux/Intel, 'machine' is the processor --
70-
# i386, etc.
71-
# XXX what about Alpha, SPARC, etc?
72-
return "%s-%s" % (osname, machine)
73-
elif osname[:5] == "sunos":
74-
if release[0] >= "5": # SunOS 5 == Solaris 2
75-
osname = "solaris"
76-
release = "%d.%s" % (int(release[0]) - 3, release[2:])
77-
# We can't use "platform.architecture()[0]" because a
78-
# bootstrap problem. We use a dict to get an error
79-
# if some suspicious happens.
80-
bitness = {2147483647:"32bit", 9223372036854775807:"64bit"}
81-
machine += ".%s" % bitness[sys.maxsize]
82-
# fall through to standard osname-release-machine representation
83-
elif osname[:3] == "aix":
84-
from .py38compat import aix_platform
85-
return aix_platform(osname, version, release)
86-
elif osname[:6] == "cygwin":
87-
osname = "cygwin"
88-
rel_re = re.compile (r'[\d.]+', re.ASCII)
89-
m = rel_re.match(release)
90-
if m:
91-
release = m.group()
92-
elif osname[:6] == "darwin":
93-
import _osx_support, distutils.sysconfig
94-
osname, release, machine = _osx_support.get_platform_osx(
95-
distutils.sysconfig.get_config_vars(),
96-
osname, release, machine)
97-
98-
return "%s-%s-%s" % (osname, release, machine)
26+
27+
# We initially exposed platforms as defined in Python 3.9
28+
# even with older Python versions when distutils was split out.
29+
# Now that we delegate to stdlib sysconfig we need to restore this
30+
# in case anyone has started to depend on it.
31+
32+
if sys.version_info < (3, 8):
33+
if os.name == 'nt':
34+
if '(arm)' in sys.version.lower():
35+
return 'win-arm32'
36+
if '(arm64)' in sys.version.lower():
37+
return 'win-arm64'
38+
39+
if sys.version_info < (3, 9):
40+
if os.name == "posix" and hasattr(os, 'uname'):
41+
osname, host, release, version, machine = os.uname()
42+
if osname[:3] == "aix":
43+
from .py38compat import aix_platform
44+
return aix_platform(osname, version, release)
45+
46+
return sysconfig.get_platform()
9947

10048
def get_platform():
10149
if os.name == 'nt':

0 commit comments

Comments
 (0)