Skip to content

Commit

Permalink
Allow comparing of all LooseVersions in Python 3
Browse files Browse the repository at this point in the history
When a number is in a place where the other version has a letter, the
comparison would fail in Python 3 but succeed in Python 2.
Add patch from https://bugs.python.org/issue14894
  • Loading branch information
Flamefire committed Aug 23, 2022
1 parent 935e082 commit 275dd62
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
20 changes: 14 additions & 6 deletions easybuild/tools/loose_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# - Changes to documentation and formatting

import re
from itertools import zip_longest


class LooseVersion(object):
Expand Down Expand Up @@ -48,12 +49,19 @@ def _cmp(self, other):
if isinstance(other, str):
other = LooseVersion(other)

if self.version == other.version:
return 0
if self.version < other.version:
return -1
if self.version > other.version:
return 1
# Modified: Behave the same in Python 2 & 3 when parts are of different types
# Taken from https://bugs.python.org/issue14894
for i, j in zip_longest(self.version, other.version, fillvalue=''):
if not type(i) is type(j):
i = str(i)
j = str(j)
if i == j:
continue
elif i < j:
return -1
else: # i > j
return 1
return 0

def __eq__(self, other):
return self._cmp(other) == 0
Expand Down
9 changes: 8 additions & 1 deletion test/framework/utilities_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ def test_LooseVersion(self):
self.assertLess(LooseVersion('2.1.5'), LooseVersion('2.2'))
self.assertLess(LooseVersion('2.1.3'), LooseVersion('3'))
self.assertLessEqual(LooseVersion('2.1.0'), LooseVersion('2.2'))
# Careful here: 1.0 > 1 !!!
self.assertGreater(LooseVersion('1.0'), LooseVersion('1'))
self.assertLess(LooseVersion('1'), LooseVersion('1.0'))

# The following test is taken from Python disutils tests
# licensed under the Python Software Foundation License Version 2
Expand All @@ -141,7 +144,11 @@ def test_LooseVersion(self):
('3.2.pl0', '3.1.1.6', 1),
('2g6', '11g', -1),
('0.960923', '2.2beta29', -1),
('1.13++', '5.5.kw', -1))
('1.13++', '5.5.kw', -1),
# Added from https://bugs.python.org/issue14894
('a.12.b.c', 'a.b.3', -1),
('1.0', '1', 1),
('1', '1.0', -1))

for v1, v2, wanted in versions:
res = LooseVersion(v1)._cmp(LooseVersion(v2))
Expand Down

0 comments on commit 275dd62

Please sign in to comment.