Skip to content

Commit 1a3eb12

Browse files
miss-islingtonserhiy-storchaka
authored andcommitted
[3.7] bpo-26544: Get rid of dependence from distutils in platform. (GH-8356) (GH-8970) (GH-9061)
(cherry picked from commit 7d81e8f) (cherry picked from commit 20a8392) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent e2c1657 commit 1a3eb12

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

Lib/platform.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,35 @@
136136
# Constant used by test_platform to test linux_distribution().
137137
_UNIXCONFDIR = '/etc'
138138

139+
# Helper for comparing two version number strings.
140+
# Based on the description of the PHP's version_compare():
141+
# http://php.net/manual/en/function.version-compare.php
142+
143+
_ver_stages = {
144+
# any string not found in this dict, will get 0 assigned
145+
'dev': 10,
146+
'alpha': 20, 'a': 20,
147+
'beta': 30, 'b': 30,
148+
'c': 40,
149+
'RC': 50, 'rc': 50,
150+
# number, will get 100 assigned
151+
'pl': 200, 'p': 200,
152+
}
153+
154+
_component_re = re.compile(r'([0-9]+|[._+-])')
155+
156+
def _comparable_version(version):
157+
result = []
158+
for v in _component_re.split(version):
159+
if v not in '._+-':
160+
try:
161+
v = int(v, 10)
162+
t = 100
163+
except ValueError:
164+
t = _ver_stages.get(v, 0)
165+
result.extend((t, v))
166+
return result
167+
139168
### Platform specific APIs
140169

141170
_libc_search = re.compile(b'(__libc_init)'
@@ -159,7 +188,7 @@ def libc_ver(executable=sys.executable, lib='', version='', chunksize=16384):
159188
The file is read and scanned in chunks of chunksize bytes.
160189
161190
"""
162-
from distutils.version import LooseVersion as V
191+
V = _comparable_version
163192
if hasattr(os.path, 'realpath'):
164193
# Python 2.2 introduced os.path.realpath(); it is used
165194
# here to work around problems with Cygwin not being

Lib/test/test_platform.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,42 @@ def test_libc_ver(self):
285285
self.assertEqual(platform.libc_ver(support.TESTFN),
286286
('glibc', '1.23.4'))
287287

288+
@support.cpython_only
289+
def test__comparable_version(self):
290+
from platform import _comparable_version as V
291+
self.assertEqual(V('1.2.3'), V('1.2.3'))
292+
self.assertLess(V('1.2.3'), V('1.2.10'))
293+
self.assertEqual(V('1.2.3.4'), V('1_2-3+4'))
294+
self.assertLess(V('1.2spam'), V('1.2dev'))
295+
self.assertLess(V('1.2dev'), V('1.2alpha'))
296+
self.assertLess(V('1.2dev'), V('1.2a'))
297+
self.assertLess(V('1.2alpha'), V('1.2beta'))
298+
self.assertLess(V('1.2a'), V('1.2b'))
299+
self.assertLess(V('1.2beta'), V('1.2c'))
300+
self.assertLess(V('1.2b'), V('1.2c'))
301+
self.assertLess(V('1.2c'), V('1.2RC'))
302+
self.assertLess(V('1.2c'), V('1.2rc'))
303+
self.assertLess(V('1.2RC'), V('1.2.0'))
304+
self.assertLess(V('1.2rc'), V('1.2.0'))
305+
self.assertLess(V('1.2.0'), V('1.2pl'))
306+
self.assertLess(V('1.2.0'), V('1.2p'))
307+
308+
self.assertLess(V('1.5.1'), V('1.5.2b2'))
309+
self.assertLess(V('3.10a'), V('161'))
310+
self.assertEqual(V('8.02'), V('8.02'))
311+
self.assertLess(V('3.4j'), V('1996.07.12'))
312+
self.assertLess(V('3.1.1.6'), V('3.2.pl0'))
313+
self.assertLess(V('2g6'), V('11g'))
314+
self.assertLess(V('0.9'), V('2.2'))
315+
self.assertLess(V('1.2'), V('1.2.1'))
316+
self.assertLess(V('1.1'), V('1.2.2'))
317+
self.assertLess(V('1.1'), V('1.2'))
318+
self.assertLess(V('1.2.1'), V('1.2.2'))
319+
self.assertLess(V('1.2'), V('1.2.2'))
320+
self.assertLess(V('0.4'), V('0.4.0'))
321+
self.assertLess(V('1.13++'), V('5.5.kw'))
322+
self.assertLess(V('0.960923'), V('2.2beta29'))
323+
288324
def test_parse_release_file(self):
289325

290326
for input, output in (

0 commit comments

Comments
 (0)