diff options
author | Russell Keith-Magee <russell@keith-magee.com> | 2024-03-28 07:59:33 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-28 07:59:33 (GMT) |
commit | f006338017cfbf846e8f7391b9ee5f69df8dc620 (patch) | |
tree | 6db12f901d882f53401dc2d3fafbe101c2b8fdbc /Lib/platform.py | |
parent | b44898299a2ed97045c270f6474785da2ff07ced (diff) | |
download | cpython-f006338017cfbf846e8f7391b9ee5f69df8dc620.zip cpython-f006338017cfbf846e8f7391b9ee5f69df8dc620.tar.gz cpython-f006338017cfbf846e8f7391b9ee5f69df8dc620.tar.bz2 |
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>
Diffstat (limited to 'Lib/platform.py')
-rwxr-xr-x | Lib/platform.py | 53 |
1 files changed, 46 insertions, 7 deletions
diff --git a/Lib/platform.py b/Lib/platform.py index df1d987..dbcb636 100755 --- a/Lib/platform.py +++ b/Lib/platform.py @@ -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 |