[Python-checkins] bpo-40570: Improve compatibility of uname_result with late-bound .platform (#20015)

Jason R. Coombs webhook-mailer at python.org
Sat May 9 10:12:46 EDT 2020


https://github.com/python/cpython/commit/2c3d508c5fabe40dac848fb9ae558069f0576879
commit: 2c3d508c5fabe40dac848fb9ae558069f0576879
branch: master
author: Jason R. Coombs <jaraco at jaraco.com>
committer: GitHub <noreply at github.com>
date: 2020-05-09T10:12:41-04:00
summary:

bpo-40570: Improve compatibility of uname_result with late-bound .platform (#20015)

* bpo-40570: Improve compatibility of uname_result with late-bound .platform.

* Add test capturing ability to cast uname to a tuple.

files:
M Lib/platform.py
M Lib/test/test_platform.py

diff --git a/Lib/platform.py b/Lib/platform.py
index 049c2c6ef25a1..e9f50ab622d31 100755
--- a/Lib/platform.py
+++ b/Lib/platform.py
@@ -798,9 +798,10 @@ def __iter__(self):
         )
 
     def __getitem__(self, key):
-        if key == 5:
-            return self.processor
-        return super().__getitem__(key)
+        return tuple(iter(self))[key]
+
+    def __len__(self):
+        return len(tuple(iter(self)))
 
 
 _uname_cache = None
diff --git a/Lib/test/test_platform.py b/Lib/test/test_platform.py
index 7664b38a720a7..a5c35dff79b8b 100644
--- a/Lib/test/test_platform.py
+++ b/Lib/test/test_platform.py
@@ -154,11 +154,26 @@ def test_uname(self):
         res = platform.uname()
         self.assertTrue(any(res))
         self.assertEqual(res[0], res.system)
+        self.assertEqual(res[-6], res.system)
         self.assertEqual(res[1], res.node)
+        self.assertEqual(res[-5], res.node)
         self.assertEqual(res[2], res.release)
+        self.assertEqual(res[-4], res.release)
         self.assertEqual(res[3], res.version)
+        self.assertEqual(res[-3], res.version)
         self.assertEqual(res[4], res.machine)
+        self.assertEqual(res[-2], res.machine)
         self.assertEqual(res[5], res.processor)
+        self.assertEqual(res[-1], res.processor)
+        self.assertEqual(len(res), 6)
+
+    def test_uname_cast_to_tuple(self):
+        res = platform.uname()
+        expected = (
+            res.system, res.node, res.release, res.version, res.machine,
+            res.processor,
+        )
+        self.assertEqual(tuple(res), expected)
 
     @unittest.skipIf(sys.platform in ['win32', 'OpenVMS'], "uname -p not used")
     def test_uname_processor(self):



More information about the Python-checkins mailing list