Unverified Kaydet (Commit) 20a8392c authored tarafından Serhiy Storchaka's avatar Serhiy Storchaka Kaydeden (comit) GitHub

[3.7] bpo-26544: Get rid of dependence from distutils in platform. (GH-8356). (GH-8970)

(cherry picked from commit 7d81e8f5)
üst 7aa3eadc
......@@ -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
......
......@@ -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 (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment