diff --git a/Doc/library/platform.rst b/Doc/library/platform.rst --- a/Doc/library/platform.rst +++ b/Doc/library/platform.rst @@ -56,6 +56,12 @@ Cross Platform value cannot be determined. +.. function:: major() + + Returns the system's major version as an integer, e.g. 2 on Linux 2.6.28. + Return 0 if the value cannot be determined. + + .. function:: node() Returns the computer's network name (may not be fully qualified!). An empty diff --git a/Lib/platform.py b/Lib/platform.py --- a/Lib/platform.py +++ b/Lib/platform.py @@ -1189,6 +1189,26 @@ def release(): """ return uname()[2] +def major(): + + """ Returns the system's major version as an integer, + e.g. 2 on Linux 2.6.28. + + Return 0 if the value cannot be determined. + + """ + if system() in ("UnixWare", "OpenUNIX"): + text = version() + else: + text = release() + text = re.sub('[ /]', '', text) + text = re.sub('^[[A-Z]]\.', '', text) + text = re.sub(r'\..*', '', text) + if text: + return int(text) + else: + return 0 + def version(): """ Returns the system's release version, e.g. '#3 on degas' diff --git a/Lib/test/test_platform.py b/Lib/test/test_platform.py --- a/Lib/test/test_platform.py +++ b/Lib/test/test_platform.py @@ -45,9 +45,16 @@ class PlatformTest(unittest.TestCase): def test_release(self): res = platform.release() + self.assertIsInstance(res, str) def test_version(self): res = platform.version() + self.assertIsInstance(res, str) + + def test_major(self): + res = platform.major() + self.assertIsInstance(res, int) + self.assertGreaterEqual(res, 0) def test_machine(self): res = platform.machine()