Skip to content

Commit baf23c5

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

3 files changed

Lines changed: 55 additions & 7 deletions

File tree

Lib/platform.py

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

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

716736
""" Queries the given executable (defaults to the Python interpreter
@@ -745,10 +765,15 @@ def architecture(executable=sys.executable, bits='', linkage=''):
745765
else:
746766
fileout = ''
747767

748-
if not fileout and \
749-
executable == sys.executable:
768+
if not fileout and executable == sys.executable:
750769
# "file" command did not return anything; we'll try to provide
751770
# some sensible defaults then...
771+
if os.name == "nt":
772+
# Use _sysconfig.get_platform() if available
773+
_, b, l = _sysconfig_platform()
774+
if b:
775+
return (b, l)
776+
752777
if sys.platform in _default_architecture:
753778
b, l = _default_architecture[sys.platform]
754779
if b:
@@ -790,10 +815,10 @@ def architecture(executable=sys.executable, bits='', linkage=''):
790815

791816

792817
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
818+
# Use _sysconfig.get_platform() if available
819+
arch, bits, linkage = _sysconfig_platform()
820+
if arch:
821+
return arch
797822

798823
# WOW64 processes mask the native architecture
799824
try:
@@ -811,6 +836,11 @@ def _get_machine_win32():
811836
else:
812837
if arch:
813838
return arch
839+
840+
# Try to use the PROCESSOR_* environment variables
841+
# available on Win XP and later; see
842+
# http://support.microsoft.com/kb/888731 and
843+
# http://www.geocities.com/rick_lively/MANUALS/ENV/MSWIN/PROCESSI.HTM
814844
return (
815845
os.environ.get('PROCESSOR_ARCHITEW6432', '') or
816846
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+
('win32-arm', 'ARM'),
385+
('win64-amd64', 'AMD64'),
386+
('win64-arm', '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)