gh-114099: Additions to standard library to support iOS (GH-117052)

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Co-authored-by: Malcolm Smith <smith@chaquo.com>
Co-authored-by: Ned Deily <nad@python.org>
This commit is contained in:
Russell Keith-Magee 2024-03-28 15:59:33 +08:00 committed by GitHub
parent b44898299a
commit f006338017
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
22 changed files with 474 additions and 48 deletions

View file

@ -496,6 +496,30 @@ def mac_ver(release='', versioninfo=('', '', ''), machine=''):
# If that also doesn't work return the default values
return release, versioninfo, machine
# A namedtuple for iOS version information.
IOSVersionInfo = collections.namedtuple(
"IOSVersionInfo",
["system", "release", "model", "is_simulator"]
)
def ios_ver(system="", release="", model="", is_simulator=False):
"""Get iOS version information, and return it as a namedtuple:
(system, release, model, is_simulator).
If values can't be determined, they are set to values provided as
parameters.
"""
if sys.platform == "ios":
import _ios_support
result = _ios_support.get_platform_ios()
if result is not None:
return IOSVersionInfo(*result)
return IOSVersionInfo(system, release, model, is_simulator)
def _java_getprop(name, default):
"""This private helper is deprecated in 3.13 and will be removed in 3.15"""
from java.lang import System
@ -654,7 +678,7 @@ def _platform(*args):
if cleaned == platform:
break
platform = cleaned
while platform[-1] == '-':
while platform and platform[-1] == '-':
platform = platform[:-1]
return platform
@ -695,7 +719,7 @@ def _syscmd_file(target, default=''):
default in case the command should fail.
"""
if sys.platform in ('dos', 'win32', 'win16'):
if sys.platform in {'dos', 'win32', 'win16', 'ios', 'tvos', 'watchos'}:
# XXX Others too ?
return default
@ -859,6 +883,14 @@ class _Processor:
csid, cpu_number = vms_lib.getsyi('SYI$_CPU', 0)
return 'Alpha' if cpu_number >= 128 else 'VAX'
# On the iOS simulator, os.uname returns the architecture as uname.machine.
# On device it returns the model name for some reason; but there's only one
# CPU architecture for iOS devices, so we know the right answer.
def get_ios():
if sys.implementation._multiarch.endswith("simulator"):
return os.uname().machine
return 'arm64'
def from_subprocess():
"""
Fall back to `uname -p`
@ -1018,6 +1050,10 @@ def uname():
system = 'Android'
release = android_ver().release
# Normalize responses on iOS
if sys.platform == 'ios':
system, release, _, _ = ios_ver()
vals = system, node, release, version, machine
# Replace 'unknown' values with the more portable ''
_uname_cache = uname_result(*map(_unknown_as_blank, vals))
@ -1297,11 +1333,14 @@ def platform(aliased=False, terse=False):
system, release, version = system_alias(system, release, version)
if system == 'Darwin':
# macOS (darwin kernel)
macos_release = mac_ver()[0]
if macos_release:
system = 'macOS'
release = macos_release
# macOS and iOS both report as a "Darwin" kernel
if sys.platform == "ios":
system, release, _, _ = ios_ver()
else:
macos_release = mac_ver()[0]
if macos_release:
system = 'macOS'
release = macos_release
if system == 'Windows':
# MS platforms