Skip to content

Commit 4e48213

Browse files
committed
pythongh-146145: Use _sysconfig.get_platform() in the platform module
1 parent b9d4318 commit 4e48213

3 files changed

Lines changed: 56 additions & 7 deletions

File tree

Lib/platform.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,27 @@ def _syscmd_file(target, default=''):
711711
'dos': ('', 'MSDOS'),
712712
})
713713

714+
715+
_sysconfig_architecture = frozendict({
716+
# platform: (arch, bits, linkage)
717+
'win32': ('x86', '32bit', 'WindowsPE'),
718+
'win-amd64': ('AMD64', '32bit', 'WindowsPE'),
719+
'win-arm32': ('ARM', '32bit', 'WindowsPE'),
720+
'win-arm64': ('ARM64', '64bit', 'WindowsPE'),
721+
})
722+
723+
def _sysconfig_platform():
724+
try:
725+
import _sysconfig
726+
except ImportError:
727+
return ('', '', '')
728+
729+
platform = _sysconfig.get_platform()
730+
if platform in _sysconfig_architecture:
731+
return _sysconfig_architecture[platform]
732+
return ('', '', '')
733+
734+
714735
def architecture(executable=sys.executable, bits='', linkage=''):
715736

716737
""" Queries the given executable (defaults to the Python interpreter
@@ -745,10 +766,15 @@ def architecture(executable=sys.executable, bits='', linkage=''):
745766
else:
746767
fileout = ''
747768

748-
if not fileout and \
749-
executable == sys.executable:
769+
if not fileout and executable == sys.executable:
750770
# "file" command did not return anything; we'll try to provide
751771
# some sensible defaults then...
772+
if os.name == "nt":
773+
# Use _sysconfig.get_platform() if available
774+
_, b, l = _sysconfig_platform()
775+
if b:
776+
return (b, l)
777+
752778
if sys.platform in _default_architecture:
753779
b, l = _default_architecture[sys.platform]
754780
if b:
@@ -790,10 +816,10 @@ def architecture(executable=sys.executable, bits='', linkage=''):
790816

791817

792818
def _get_machine_win32():
793-
# Try to use the PROCESSOR_* environment variables
794-
# available on Win XP and later; see
795-
# http://support.microsoft.com/kb/888731 and
796-
# http://www.geocities.com/rick_lively/MANUALS/ENV/MSWIN/PROCESSI.HTM
819+
# Use _sysconfig.get_platform() if available
820+
arch, bits, linkage = _sysconfig_platform()
821+
if arch:
822+
return arch
797823

798824
# WOW64 processes mask the native architecture
799825
try:
@@ -811,6 +837,11 @@ def _get_machine_win32():
811837
else:
812838
if arch:
813839
return arch
840+
841+
# Try to use the PROCESSOR_* environment variables
842+
# available on Win XP and later; see
843+
# http://support.microsoft.com/kb/888731 and
844+
# http://www.geocities.com/rick_lively/MANUALS/ENV/MSWIN/PROCESSI.HTM
814845
return (
815846
os.environ.get('PROCESSOR_ARCHITEW6432', '') or
816847
os.environ.get('PROCESSOR_ARCHITECTURE', '')

Lib/test/test_platform.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,19 +379,35 @@ def test_uname_win32_ARCHITEW6432(self):
379379
# using it, per
380380
# http://blogs.msdn.com/david.wang/archive/2006/03/26/HOWTO-Detect-Process-Bitness.aspx
381381

382+
for sysconfig_platform, arch in (
383+
('win32', 'x86'),
384+
('win-arm32', 'ARM'),
385+
('win-amd64', 'AMD64'),
386+
('win-arm64', 'ARM64'),
387+
):
388+
with mock.patch('_sysconfig.get_platform', return_value=sysconfig_platform):
389+
try:
390+
platform._uname_cache = None
391+
system, node, release, version, machine, processor = platform.uname()
392+
self.assertEqual(machine, arch)
393+
finally:
394+
platform._uname_cache = None
395+
382396
# We also need to suppress WMI checks, as those are reliable and
383397
# overrule the environment variables
384398
def raises_oserror(*a):
385399
raise OSError()
386400

387-
with support.swap_attr(platform, '_wmi_query', raises_oserror):
401+
with (mock.patch('platform._sysconfig_platform', return_value=('', '', '')),
402+
support.swap_attr(platform, '_wmi_query', raises_oserror)):
388403
with os_helper.EnvironmentVarGuard() as environ:
389404
try:
390405
del environ['PROCESSOR_ARCHITEW6432']
391406
environ['PROCESSOR_ARCHITECTURE'] = 'foo'
392407
platform._uname_cache = None
393408
system, node, release, version, machine, processor = platform.uname()
394409
self.assertEqual(machine, 'foo')
410+
395411
environ['PROCESSOR_ARCHITEW6432'] = 'bar'
396412
platform._uname_cache = None
397413
system, node, release, version, machine, processor = platform.uname()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`platform`: On Windows, use the ``_sysconfig`` module to get the
2+
architecture and the machine. Patch by Victor Stinner.

0 commit comments

Comments
 (0)