]> granicus.if.org Git - python/commitdiff
bpo-35344: platform.platform() uses mac_ver() on macOS (GH-10780)
authorVictor Stinner <vstinner@redhat.com>
Wed, 5 Dec 2018 21:41:52 +0000 (22:41 +0100)
committerGitHub <noreply@github.com>
Wed, 5 Dec 2018 21:41:52 +0000 (22:41 +0100)
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.

Doc/library/platform.rst
Lib/platform.py
Lib/test/test_platform.py
Misc/NEWS.d/next/Library/2018-11-29-00-23-25.bpo-35344.4QOPJQ.rst [new file with mode: 0644]

index 7ac4b027418e6be3e3250993cf2cd65ec8874941..60c6089ad3ccb5820b0e714f7e15ccfb055f5e6c 100644 (file)
@@ -79,6 +79,11 @@ Cross Platform
    Setting *terse* to true causes the function to return only the absolute minimum
    information needed to identify the platform.
 
+   .. versionchanged:: 3.8
+      On macOS, the function now uses :func:`mac_ver`, if it returns a
+      non-empty release string, to get the macOS version rather than the darwin
+      version.
+
 
 .. function:: processor()
 
index f089a463ef9fe1430d083f7e798441e14c55e945..d8455256bb9a8b98bb0a55202a42899a2fb7cd6a 100755 (executable)
@@ -1182,6 +1182,14 @@ def platform(aliased=0, terse=0):
     if aliased:
         system, release, version = system_alias(system, release, version)
 
+    if system == 'Darwin':
+        # macOS (darwin kernel)
+        macos_release = mac_ver()[0]
+        if macos_release:
+            # note: 'macOS' is different than 'MacOS' used below
+            system = 'macOS'
+            release = macos_release
+
     if system == 'Windows':
         # MS platforms
         rel, vers, csd, ptype = win32_ver(version)
index 978d2f76ab6841171b276e43f2d0d7a9255d7b54..c1a7e3407934b5d885222ab2dd98894e7f2ffc70 100644 (file)
@@ -10,6 +10,11 @@ from unittest import mock
 from test import support
 
 class PlatformTest(unittest.TestCase):
+    def clear_caches(self):
+        platform._platform_cache.clear()
+        platform._sys_version_cache.clear()
+        platform._uname_cache = None
+
     def test_architecture(self):
         res = platform.architecture()
 
@@ -344,5 +349,33 @@ class PlatformTest(unittest.TestCase):
         self.assertLess(V('0.960923'), V('2.2beta29'))
 
 
+    def test_macos(self):
+        self.addCleanup(self.clear_caches)
+
+        uname = ('Darwin', 'hostname', '17.7.0',
+                 ('Darwin Kernel Version 17.7.0: '
+                  'Thu Jun 21 22:53:14 PDT 2018; '
+                  'root:xnu-4570.71.2~1/RELEASE_X86_64'),
+                 'x86_64', 'i386')
+        arch = ('64bit', '')
+        with mock.patch.object(platform, 'uname', return_value=uname), \
+             mock.patch.object(platform, 'architecture', return_value=arch):
+            for mac_ver, expected_terse, expected in [
+                # darwin: mac_ver() returns empty strings
+                (('', '', ''),
+                 'Darwin-17.7.0',
+                 'Darwin-17.7.0-x86_64-i386-64bit'),
+                # macOS: mac_ver() returns macOS version
+                (('10.13.6', ('', '', ''), 'x86_64'),
+                 'macOS-10.13.6',
+                 'macOS-10.13.6-x86_64-i386-64bit'),
+            ]:
+                with mock.patch.object(platform, 'mac_ver',
+                                       return_value=mac_ver):
+                    self.clear_caches()
+                    self.assertEqual(platform.platform(terse=1), expected_terse)
+                    self.assertEqual(platform.platform(), expected)
+
+
 if __name__ == '__main__':
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2018-11-29-00-23-25.bpo-35344.4QOPJQ.rst b/Misc/NEWS.d/next/Library/2018-11-29-00-23-25.bpo-35344.4QOPJQ.rst
new file mode 100644 (file)
index 0000000..a999cbe
--- /dev/null
@@ -0,0 +1,3 @@
+On macOS, :func:`platform.platform` now uses :func:`platform.mac_ver`, if it
+returns a non-empty release string, to get the macOS version rather than the
+darwin version.