One function

compare in python-semver/python-semver

The author described this change as fix version comparisons. It counts as a record because the check below fails on the code as it stood at 41a071595 and passes on e1a633cc4, with nothing else changed between the two runs.

Fix saved2017-01-16
Sharing licenceBSD-3-Clause · LICENSE.txt
Change size+18 2

What the code was meant to do, written into the code itself as a docstring

Compare two versions :param ver1: version string 1 :param ver2: version string 2 :return: The return value is negative if ver1 < ver2, zero if ver1 == ver2 and strictly positive if ver1 > ver2 :rtype: int

The change

99 """
1010 def nat_cmp(a, b):
1111 def convert(text):
12- return (2, int(text)) if re.match('[0-9]+', text) else (1, text)
12+ return int(text) if re.match('[0-9]+', text) else text
1313
1414 def split_key(key):
1515 return [convert(c) for c in key.split('.')]
1616
17+ def cmp_prerelease_tag(a, b):
18+ if isinstance(a, int) and isinstance(b, int):
19+ return cmp(a, b)
20+ elif isinstance(a, int):
21+ return -1
22+ elif isinstance(b, int):
23+ return 1
24+ else:
25+ return cmp(a, b)
26+
1727 a, b = a or '', b or ''
18- return cmp(split_key(a), split_key(b))
28+ a_parts, b_parts = split_key(a), split_key(b)
29+ for sub_a, sub_b in zip(a_parts, b_parts):
30+ cmp_result = cmp_prerelease_tag(sub_a, sub_b)
31+ if cmp_result != 0:
32+ return cmp_result
33+ else:
34+ return cmp(len(a), len(b))
1935
2036 def compare_by_keys(d1, d2):
2137 for key in ['major', 'minor', 'patch']:

The check that tells the two apart

failpass·tests.py::test_should_get_less

Check file tests.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.

Origin and history

The code before it41a071595cdb400e625f366838b35d61d538ac7e
Broken version dated2016-12-19
Modulesemver
Units changedcompare
Fingerprintd94d1590c8869620
Checked2026-08-17 by goldset/0.1

Every field above is generated by our program. None of it is written by hand.