diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2018-09-04 17:31:18 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-04 17:31:18 +0300 |
commit | 20a8392cec2967f15ae81633c1775645b3ca40da (patch) | |
tree | 620f5ce542dc0ab5b9989c45c180e8daedbd6dd9 | |
parent | 7aa3eadca2a50ce651ce603d6100b05bb7106f1c (diff) | |
download | cpython-git-20a8392cec2967f15ae81633c1775645b3ca40da.tar.gz |
[3.7] bpo-26544: Get rid of dependence from distutils in platform. (GH-8356). (GH-8970)
(cherry picked from commit 7d81e8f5995df6980a1a02923e224a481375f130)
-rwxr-xr-x | Lib/platform.py | 31 | ||||
-rw-r--r-- | Lib/test/test_platform.py | 36 |
2 files changed, 66 insertions, 1 deletions
diff --git a/Lib/platform.py b/Lib/platform.py index e227348507..4482f479bf 100755 --- a/Lib/platform.py +++ b/Lib/platform.py @@ -136,6 +136,35 @@ except AttributeError: # Constant used by test_platform to test linux_distribution(). _UNIXCONFDIR = '/etc' +# Helper for comparing two version number strings. +# Based on the description of the PHP's version_compare(): +# http://php.net/manual/en/function.version-compare.php + +_ver_stages = { + # any string not found in this dict, will get 0 assigned + 'dev': 10, + 'alpha': 20, 'a': 20, + 'beta': 30, 'b': 30, + 'c': 40, + 'RC': 50, 'rc': 50, + # number, will get 100 assigned + 'pl': 200, 'p': 200, +} + +_component_re = re.compile(r'([0-9]+|[._+-])') + +def _comparable_version(version): + result = [] + for v in _component_re.split(version): + if v not in '._+-': + try: + v = int(v, 10) + t = 100 + except ValueError: + t = _ver_stages.get(v, 0) + result.extend((t, v)) + return result + ### Platform specific APIs _libc_search = re.compile(b'(__libc_init)' @@ -159,7 +188,7 @@ def libc_ver(executable=sys.executable, lib='', version='', chunksize=16384): The file is read and scanned in chunks of chunksize bytes. """ - from distutils.version import LooseVersion as V + V = _comparable_version if hasattr(os.path, 'realpath'): # Python 2.2 introduced os.path.realpath(); it is used # here to work around problems with Cygwin not being diff --git a/Lib/test/test_platform.py b/Lib/test/test_platform.py index eaf14e0f18..509d8865c0 100644 --- a/Lib/test/test_platform.py +++ b/Lib/test/test_platform.py @@ -285,6 +285,42 @@ class PlatformTest(unittest.TestCase): self.assertEqual(platform.libc_ver(support.TESTFN), ('glibc', '1.23.4')) + @support.cpython_only + def test__comparable_version(self): + from platform import _comparable_version as V + self.assertEqual(V('1.2.3'), V('1.2.3')) + self.assertLess(V('1.2.3'), V('1.2.10')) + self.assertEqual(V('1.2.3.4'), V('1_2-3+4')) + self.assertLess(V('1.2spam'), V('1.2dev')) + self.assertLess(V('1.2dev'), V('1.2alpha')) + self.assertLess(V('1.2dev'), V('1.2a')) + self.assertLess(V('1.2alpha'), V('1.2beta')) + self.assertLess(V('1.2a'), V('1.2b')) + self.assertLess(V('1.2beta'), V('1.2c')) + self.assertLess(V('1.2b'), V('1.2c')) + self.assertLess(V('1.2c'), V('1.2RC')) + self.assertLess(V('1.2c'), V('1.2rc')) + self.assertLess(V('1.2RC'), V('1.2.0')) + self.assertLess(V('1.2rc'), V('1.2.0')) + self.assertLess(V('1.2.0'), V('1.2pl')) + self.assertLess(V('1.2.0'), V('1.2p')) + + self.assertLess(V('1.5.1'), V('1.5.2b2')) + self.assertLess(V('3.10a'), V('161')) + self.assertEqual(V('8.02'), V('8.02')) + self.assertLess(V('3.4j'), V('1996.07.12')) + self.assertLess(V('3.1.1.6'), V('3.2.pl0')) + self.assertLess(V('2g6'), V('11g')) + self.assertLess(V('0.9'), V('2.2')) + self.assertLess(V('1.2'), V('1.2.1')) + self.assertLess(V('1.1'), V('1.2.2')) + self.assertLess(V('1.1'), V('1.2')) + self.assertLess(V('1.2.1'), V('1.2.2')) + self.assertLess(V('1.2'), V('1.2.2')) + self.assertLess(V('0.4'), V('0.4.0')) + self.assertLess(V('1.13++'), V('5.5.kw')) + self.assertLess(V('0.960923'), V('2.2beta29')) + def test_parse_release_file(self): for input, output in ( |