Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ea0ca21

Browse files
authored
bpo-35344: platform.platform() uses mac_ver() on macOS (GH-10780)
On macOS, platform.platform() now uses mac_ver(), if it returns a non-empty release string, to get the macOS version rather than darwin version.
1 parent 40a61da commit ea0ca21

4 files changed

Lines changed: 49 additions & 0 deletions

File tree

Doc/library/platform.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ Cross Platform
7979
Setting *terse* to true causes the function to return only the absolute minimum
8080
information needed to identify the platform.
8181

82+
.. versionchanged:: 3.8
83+
On macOS, the function now uses :func:`mac_ver`, if it returns a
84+
non-empty release string, to get the macOS version rather than the darwin
85+
version.
86+
8287

8388
.. function:: processor()
8489

Lib/platform.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,14 @@ def platform(aliased=0, terse=0):
11821182
if aliased:
11831183
system, release, version = system_alias(system, release, version)
11841184

1185+
if system == 'Darwin':
1186+
# macOS (darwin kernel)
1187+
macos_release = mac_ver()[0]
1188+
if macos_release:
1189+
# note: 'macOS' is different than 'MacOS' used below
1190+
system = 'macOS'
1191+
release = macos_release
1192+
11851193
if system == 'Windows':
11861194
# MS platforms
11871195
rel, vers, csd, ptype = win32_ver(version)

Lib/test/test_platform.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
from test import support
1111

1212
class PlatformTest(unittest.TestCase):
13+
def clear_caches(self):
14+
platform._platform_cache.clear()
15+
platform._sys_version_cache.clear()
16+
platform._uname_cache = None
17+
1318
def test_architecture(self):
1419
res = platform.architecture()
1520

@@ -344,5 +349,33 @@ def test__comparable_version(self):
344349
self.assertLess(V('0.960923'), V('2.2beta29'))
345350

346351

352+
def test_macos(self):
353+
self.addCleanup(self.clear_caches)
354+
355+
uname = ('Darwin', 'hostname', '17.7.0',
356+
('Darwin Kernel Version 17.7.0: '
357+
'Thu Jun 21 22:53:14 PDT 2018; '
358+
'root:xnu-4570.71.2~1/RELEASE_X86_64'),
359+
'x86_64', 'i386')
360+
arch = ('64bit', '')
361+
with mock.patch.object(platform, 'uname', return_value=uname), \
362+
mock.patch.object(platform, 'architecture', return_value=arch):
363+
for mac_ver, expected_terse, expected in [
364+
# darwin: mac_ver() returns empty strings
365+
(('', '', ''),
366+
'Darwin-17.7.0',
367+
'Darwin-17.7.0-x86_64-i386-64bit'),
368+
# macOS: mac_ver() returns macOS version
369+
(('10.13.6', ('', '', ''), 'x86_64'),
370+
'macOS-10.13.6',
371+
'macOS-10.13.6-x86_64-i386-64bit'),
372+
]:
373+
with mock.patch.object(platform, 'mac_ver',
374+
return_value=mac_ver):
375+
self.clear_caches()
376+
self.assertEqual(platform.platform(terse=1), expected_terse)
377+
self.assertEqual(platform.platform(), expected)
378+
379+
347380
if __name__ == '__main__':
348381
unittest.main()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
On macOS, :func:`platform.platform` now uses :func:`platform.mac_ver`, if it
2+
returns a non-empty release string, to get the macOS version rather than the
3+
darwin version.

0 commit comments

Comments
 (0)