Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Doc/library/sysconfig.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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()

Expand Down
28 changes: 24 additions & 4 deletions Lib/sysconfig/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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")
Expand Down
5 changes: 5 additions & 0 deletions Lib/sysconfig/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from sysconfig import (
_ALWAYS_STR,
_PYTHON_BUILD,
_get_platform,
_get_sysconfigdata_name,
get_config_h_filename,
get_config_var,
Expand Down Expand Up @@ -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')
Expand Down
81 changes: 80 additions & 1 deletion Lib/test/test_sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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'):
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Loading