From 8e9b41b5da14fc159465bc4f3b422ebb0ef5a67b Mon Sep 17 00:00:00 2001 From: Lucas Zhou Date: Fri, 20 Nov 2026 14:03:04 -0600 Subject: [PATCH] gh-162323: Preserve the build platform in sysconfig.get_platform() --- Doc/library/sysconfig.rst | 12 ++- Lib/sysconfig/__init__.py | 28 ++++++- Lib/sysconfig/__main__.py | 5 ++ Lib/test/test_sysconfig.py | 81 ++++++++++++++++++- ...-11-20-14-00-00.gh-issue-162323.9oExnW.rst | 4 + 5 files changed, 121 insertions(+), 9 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-20-14-00-00.gh-issue-162323.9oExnW.rst diff --git a/Doc/library/sysconfig.rst b/Doc/library/sysconfig.rst index 8aa912d99ba756..633ce67729489c 100644 --- a/Doc/library/sysconfig.rst +++ b/Doc/library/sysconfig.rst @@ -369,13 +369,13 @@ Other functions .. function:: get_platform() - Return a string that identifies the current platform. + Return a string that identifies the platform Python was built for. This is used mainly to distinguish platform-specific build directories and platform-specific built distributions. Typically includes the OS name and - version and the architecture (as supplied by :func:`os.uname`), although the - exact information included depends on the OS; e.g., on Linux, the kernel - version isn't particularly important. + version and the architecture (as supplied by :func:`os.uname` at build time), + although the exact information included depends on the OS; e.g., on Linux, + the kernel version isn't particularly important. Examples of returned values: @@ -398,6 +398,10 @@ Other functions For other non-POSIX platforms, currently just returns :data:`sys.platform`. + .. versionchanged:: 3.16 + On POSIX platforms, use the platform recorded at build time, so that + running Python on a compatible architecture does not change the result. + .. function:: is_python_build() diff --git a/Lib/sysconfig/__init__.py b/Lib/sysconfig/__init__.py index aeb0a904e9cdc8..f26ffd14b61ada 100644 --- a/Lib/sysconfig/__init__.py +++ b/Lib/sysconfig/__init__.py @@ -651,13 +651,13 @@ def get_config_var(name): def get_platform(): - """Return a string that identifies the current platform. + """Return a string that identifies the platform Python was built for. This is used mainly to distinguish platform-specific build directories and platform-specific built distributions. Typically includes the OS name and - version and the architecture (as supplied by 'os.uname()'), although the - exact information included depends on the OS; on Linux, the kernel version - isn't particularly important. + version and the architecture (as supplied by 'os.uname()' at build time), + although the exact information included depends on the OS; on Linux, the + kernel version isn't particularly important. Examples of returned values: linux-x86_64 @@ -679,6 +679,16 @@ def get_platform(): - android-24-arm64_v8a For other non-POSIX platforms, currently just returns :data:`sys.platform`.""" + if os.name == 'posix' and '_PYTHON_HOST_PLATFORM' not in os.environ: + # The runtime kernel may report a different, compatible architecture. + # During the build, HOST_PLATFORM has not been generated yet. + platform = get_config_var('HOST_PLATFORM') + if platform: + return platform + return _get_platform() + + +def _get_platform(): if os.name == 'nt': import _sysconfig platform = _sysconfig.get_platform() @@ -704,6 +714,16 @@ def get_platform(): machine = machine.replace(' ', '_') machine = machine.replace('/', '-') + if osname == 'linux': + # A native 32-bit build can run on a 64-bit kernel too. Use the + # compiler's target to distinguish it from a 64-bit build. + multiarch = get_config_var('MULTIARCH') or '' + build_machine = multiarch.partition('-')[0] + if build_machine in ('i386', 'i686') and machine == 'x86_64': + machine = 'i686' + elif build_machine == 'arm' and machine == 'aarch64': + machine = 'armv8l' + if osname == "android" or sys.platform == "android": osname = "android" release = get_config_var("ANDROID_API_LEVEL") diff --git a/Lib/sysconfig/__main__.py b/Lib/sysconfig/__main__.py index 0cf0cf4dbb9ec7..00581cb41a5c98 100644 --- a/Lib/sysconfig/__main__.py +++ b/Lib/sysconfig/__main__.py @@ -5,6 +5,7 @@ from sysconfig import ( _ALWAYS_STR, _PYTHON_BUILD, + _get_platform, _get_sysconfigdata_name, get_config_h_filename, get_config_var, @@ -194,6 +195,10 @@ def _generate_posix_vars(): module.build_time_vars = vars sys.modules[name] = module + # Compute this from the build configuration, not a previous build's data. + vars['HOST_PLATFORM'] = _get_platform() + get_config_vars()['HOST_PLATFORM'] = vars['HOST_PLATFORM'] + pybuilddir = _get_pybuilddir() os.makedirs(pybuilddir, exist_ok=True) destfile = os.path.join(pybuilddir, name + '.py') diff --git a/Lib/test/test_sysconfig.py b/Lib/test/test_sysconfig.py index af642e25ac8c3d..04c14e8c12d532 100644 --- a/Lib/test/test_sysconfig.py +++ b/Lib/test/test_sysconfig.py @@ -22,7 +22,7 @@ ) from test.support.import_helper import import_module from test.support.os_helper import (TESTFN, unlink, skip_unless_symlink, - change_cwd, temp_dir, + change_cwd, temp_cwd, temp_dir, EnvironmentVarGuard) from test.support.venv import VirtualEnvironmentMixin @@ -257,6 +257,9 @@ def test_get_platform(self): self.assertIsInstance(actual_platform, str) self.assertTrue(actual_platform) + # Exercise platform detection without the saved build platform. + get_config_vars().pop('HOST_PLATFORM', None) + # Windows os.name = 'nt' with patch('_sysconfig.get_platform', create=True, return_value='win32'): @@ -394,6 +397,82 @@ def test_get_platform(self): # XXX more platforms to tests here + def test_get_platform_build(self): + os.name = 'posix' + sys.platform = 'linux' + with EnvironmentVarGuard() as env: + env.unset('_PYTHON_HOST_PLATFORM') + for build_platform, machine in ( + ('linux-i686', 'x86_64'), + ('linux-armv7l', 'armv8l'), + ('linux-armv7l', 'aarch64'), + ('linux-arm', 'armv7l'), + ): + with self.subTest(build_platform=build_platform, machine=machine): + get_config_vars()['HOST_PLATFORM'] = build_platform + self._set_uname(('Linux', 'localhost', '6.1', '', machine)) + self.assertEqual(get_platform(), build_platform) + + def test_get_platform_cross_compile(self): + os.name = 'posix' + sys.platform = 'linux' + get_config_vars()['HOST_PLATFORM'] = 'linux-x86_64' + with EnvironmentVarGuard() as env: + for target in ('linux-i686', 'linux-arm', 'linux-aarch64'): + with self.subTest(target=target): + env['_PYTHON_HOST_PLATFORM'] = target + self.assertEqual(get_platform(), target) + + def test_get_platform_linux_build_architecture(self): + os.name = 'posix' + sys.platform = 'linux' + get_config_vars().pop('HOST_PLATFORM', None) + with EnvironmentVarGuard() as env: + env.unset('_PYTHON_HOST_PLATFORM') + for multiarch, machine, expected in ( + ('i386-linux-gnu', 'x86_64', 'i686'), + ('i386-linux-musl', 'x86_64', 'i686'), + ('x86_64-linux-gnu', 'x86_64', 'x86_64'), + ('x86_64-linux-gnux32', 'x86_64', 'x86_64'), + ('arm-linux-gnueabihf', 'aarch64', 'armv8l'), + ('arm-linux-gnueabihf', 'armv7l', 'armv7l'), + ('aarch64-linux-gnu', 'aarch64', 'aarch64'), + ): + with self.subTest(multiarch=multiarch, machine=machine): + get_config_vars()['MULTIARCH'] = multiarch + self._set_uname(('Linux', 'localhost', '6.1', '', machine)) + self.assertEqual(get_platform(), f'linux-{expected}') + + @unittest.skipUnless(os.name == 'posix', 'POSIX-specific test') + @unittest.skipIf(is_wasi or is_apple_mobile, + 'Makefile and pyconfig.h are not available') + @requires_subprocess() + def test_generate_posix_vars_platform(self): + from runpy import run_path + + expected = sysconfig._get_platform() + for initialized in (False, True): + with self.subTest(initialized=initialized), temp_cwd(): + script = textwrap.dedent(f""" + import sysconfig + from sysconfig.__main__ import _generate_posix_vars + if {initialized!r}: + sysconfig.get_config_vars()['HOST_PLATFORM'] = 'stale-platform' + _generate_posix_vars() + """) + subprocess.run([sys.executable, '-S', '-c', script], + check=True, capture_output=True) + + with open('pybuilddir.txt', encoding='utf-8') as f: + pybuilddir = f.read() + datafile = os.path.join( + pybuilddir, sysconfig._get_sysconfigdata_name() + '.py') + build_vars = run_path(datafile)['build_time_vars'] + self.assertEqual(build_vars['HOST_PLATFORM'], expected) + with open(os.path.join(pybuilddir, _get_json_data_name())) as f: + install_vars = json.load(f) + self.assertEqual(install_vars['HOST_PLATFORM'], expected) + @unittest.skipIf(is_wasi, "Incompatible with WASI mapdir and OOT builds") @unittest.skipIf(is_apple_mobile, f"{sys.platform} doesn't distribute header files in the runtime environment") diff --git a/Misc/NEWS.d/next/Library/2026-11-20-14-00-00.gh-issue-162323.9oExnW.rst b/Misc/NEWS.d/next/Library/2026-11-20-14-00-00.gh-issue-162323.9oExnW.rst new file mode 100644 index 00000000000000..4064b6689c116c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-20-14-00-00.gh-issue-162323.9oExnW.rst @@ -0,0 +1,4 @@ +:func:`sysconfig.get_platform` now uses the platform recorded at build time on +POSIX, so that running Python on a compatible architecture does not change +the result. Fix platform detection for native 32-bit x86 and ARM builds on +64-bit Linux kernels.