diff --git a/Doc/library/platform.rst b/Doc/library/platform.rst --- a/Doc/library/platform.rst +++ b/Doc/library/platform.rst @@ -260,6 +260,11 @@ Unix Platforms parameters. ``id`` is the item in parentheses after the version number. It is usually the version codename. +.. function:: linux_version() + + Get the version of the Linux kernel as a tuple: (kernel version, major + version, minor revision). Return ``None`` if the system is not Linux. + .. function:: libc_ver(executable=sys.executable, lib='', version='', chunksize=2048) Tries to determine the libc version against which the file executable (defaults diff --git a/Lib/platform.py b/Lib/platform.py --- a/Lib/platform.py +++ b/Lib/platform.py @@ -1492,6 +1492,15 @@ def platform(aliased=0, terse=0): _platform_cache[(aliased, terse)] = platform return platform +def linux_version(): + info = uname() + if info[0] != 'Linux': + return None + version = info[2] + # platform.release() is something like '2.6.33.7-desktop-2mnb' + version = version.split('-', 1)[0] + return tuple(int(item) for item in version.split('.')) + ### Command line interface if __name__ == '__main__': 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 @@ -271,6 +271,15 @@ class PlatformTest(unittest.TestCase): returncode = ret >> 8 self.assertEqual(returncode, len(data)) + def test_linux_version(self): + version = platform.linux_version() + if platform.system() == 'Linux': + self.assertEqual(len(version), 3) + for field in version: + self.assertEqual(type(field), int) + else: + self.assertIsNone(version) + def test_main(): support.run_unittest(