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

Skip to content

Commit 68386bc

Browse files
committed
Issue #15164: Change return value of platform.uname() from a
plain tuple to a collections.namedtuple.
1 parent 56ed284 commit 68386bc

6 files changed

Lines changed: 33 additions & 18 deletions

File tree

Doc/library/platform.rst

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,20 @@ Cross Platform
158158

159159
.. function:: uname()
160160

161-
Fairly portable uname interface. Returns a tuple of strings ``(system, node,
162-
release, version, machine, processor)`` identifying the underlying platform.
161+
Fairly portable uname interface. Returns a :func:`~collections.namedtuple`
162+
containing six attributes: :attr:`system`, :attr:`node`, :attr:`release`,
163+
:attr:`version`, :attr:`machine`, and :attr:`processor`.
163164

164-
Note that unlike the :func:`os.uname` function this also returns possible
165-
processor information as additional tuple entry.
165+
Note that this adds a sixth attribute (:attr:`processor`) not present
166+
in the :func:`os.uname` result. Also, the attribute names are different
167+
for the first two attributes; :func:`os.uname` names them
168+
:attr:`sysname` and :attr:`nodename`.
166169

167170
Entries which cannot be determined are set to ``''``.
168171

172+
.. versionchanged:: 3.3
173+
Result changed from a tuple to a namedtuple.
174+
169175

170176
Java Platform
171177
-------------

Lib/platform.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111

112112
__version__ = '1.0.7'
113113

114+
import collections
114115
import sys, os, re
115116

116117
### Globals & Constants
@@ -1027,6 +1028,9 @@ def architecture(executable=sys.executable,bits='',linkage=''):
10271028

10281029
### Portable uname() interface
10291030

1031+
uname_result = collections.namedtuple("uname_result",
1032+
"system node release version machine processor")
1033+
10301034
_uname_cache = None
10311035

10321036
def uname():
@@ -1161,7 +1165,7 @@ def uname():
11611165
system = 'Windows'
11621166
release = 'Vista'
11631167

1164-
_uname_cache = system,node,release,version,machine,processor
1168+
_uname_cache = uname_result(system,node,release,version,machine,processor)
11651169
return _uname_cache
11661170

11671171
### Direct interfaces to some of the uname() return values
@@ -1173,7 +1177,7 @@ def system():
11731177
An empty string is returned if the value cannot be determined.
11741178
11751179
"""
1176-
return uname()[0]
1180+
return uname().system
11771181

11781182
def node():
11791183

@@ -1183,7 +1187,7 @@ def node():
11831187
An empty string is returned if the value cannot be determined.
11841188
11851189
"""
1186-
return uname()[1]
1190+
return uname().node
11871191

11881192
def release():
11891193

@@ -1192,7 +1196,7 @@ def release():
11921196
An empty string is returned if the value cannot be determined.
11931197
11941198
"""
1195-
return uname()[2]
1199+
return uname().release
11961200

11971201
def version():
11981202

@@ -1201,7 +1205,7 @@ def version():
12011205
An empty string is returned if the value cannot be determined.
12021206
12031207
"""
1204-
return uname()[3]
1208+
return uname().version
12051209

12061210
def machine():
12071211

@@ -1210,7 +1214,7 @@ def machine():
12101214
An empty string is returned if the value cannot be determined.
12111215
12121216
"""
1213-
return uname()[4]
1217+
return uname().machine
12141218

12151219
def processor():
12161220

@@ -1222,7 +1226,7 @@ def processor():
12221226
e.g. NetBSD does this.
12231227
12241228
"""
1225-
return uname()[5]
1229+
return uname().processor
12261230

12271231
### Various APIs for extracting information from sys.version
12281232

Lib/sysconfig.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ def get_config_vars(*args):
553553
_CONFIG_VARS['srcdir'] = os.path.normpath(srcdir)
554554

555555
if sys.platform == 'darwin':
556-
kernel_version = os.uname()[2] # Kernel version (8.4.3)
556+
kernel_version = os.uname().release # Kernel version (8.4.3)
557557
major_version = int(kernel_version.split('.')[0])
558558

559559
if major_version < 8:

Lib/test/test__locale.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
from platform import uname
1212
from test.support import run_unittest
1313

14-
if uname()[0] == "Darwin":
15-
maj, min, mic = [int(part) for part in uname()[2].split(".")]
14+
if uname().system == "Darwin":
15+
maj, min, mic = [int(part) for part in uname().release.split(".")]
1616
if (maj, min, mic) < (8, 0, 0):
1717
raise unittest.SkipTest("locale support broken for OS X < 10.4")
1818

Lib/test/test_platform.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ def test_system_alias(self):
133133
def test_uname(self):
134134
res = platform.uname()
135135
self.assertTrue(any(res))
136+
self.assertEqual(res[0], res.system)
137+
self.assertEqual(res[1], res.node)
138+
self.assertEqual(res[2], res.release)
139+
self.assertEqual(res[3], res.version)
140+
self.assertEqual(res[4], res.machine)
141+
self.assertEqual(res[5], res.processor)
136142

137143
@unittest.skipUnless(sys.platform.startswith('win'), "windows only test")
138144
def test_uname_win32_ARCHITEW6432(self):
@@ -166,7 +172,7 @@ def test_win32_ver(self):
166172
def test_mac_ver(self):
167173
res = platform.mac_ver()
168174

169-
if platform.uname()[0] == 'Darwin':
175+
if platform.uname().system == 'Darwin':
170176
# We're on a MacOSX system, check that
171177
# the right version information is returned
172178
fd = os.popen('sw_vers', 'r')

Misc/NEWS

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ Core and Builtins
5959
Library
6060
-------
6161

62-
- Support Mageia Linux in the platform module.
63-
64-
- Issue #11678: Support Arch linux in the platform module.
62+
- Issue #15164: Change return value of platform.uname() from a
63+
plain tuple to a collections.namedtuple.
6564

6665
- Issue #15118: Change return value of os.uname() and os.times() from
6766
plain tuples to immutable iterable objects with named attributes

0 commit comments

Comments
 (0)