From 2fb427d0d9d9c70d08a707c6fb5bcc2ae2c4023b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Sat, 21 Dec 2019 15:51:24 +0100 Subject: Properly coerce versions with leading zeroes. A leading zero is forbidden in the SemVer spec, but could be valid under other schemes; when coercing, it can easily be removed. Closes #89, thanks to Andrew Ni for the report. --- ChangeLog | 6 +++++- semantic_version/base.py | 8 ++++++++ tests/test_base.py | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index b777535..a9efaad 100644 --- a/ChangeLog +++ b/ChangeLog @@ -4,7 +4,11 @@ ChangeLog 2.8.4 (unreleased) ------------------ -- Nothing changed yet. +*Bugfix:* + + * `#89 `_: + Properly coerce versions with leading zeroes in components (e.g. + ``1.01.007``) 2.8.3 (2019-11-21) diff --git a/semantic_version/base.py b/semantic_version/base.py index 1b0bac5..7fd871e 100644 --- a/semantic_version/base.py +++ b/semantic_version/base.py @@ -244,6 +244,14 @@ class Version(object): while version.count('.') < 2: version += '.0' + # Strip leading zeros in components + # Version is of the form nn, nn.pp or nn.pp.qq + version = '.'.join( + # If the part was '0', we end up with an empty string. + part.lstrip('0') or '0' + for part in version.split('.') + ) + if match.end() == len(version_string): return Version(version, partial=partial) diff --git a/tests/test_base.py b/tests/test_base.py index 2c0830f..c9770c9 100755 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -569,7 +569,7 @@ class CoerceTestCase(unittest.TestCase): examples = { # Dict of target: [list of equivalents] '0.0.0': ('0', '0.0', '0.0.0', '0.0.0+', '0-'), - '0.1.0': ('0.1', '0.1+', '0.1-', '0.1.0'), + '0.1.0': ('0.1', '0.1+', '0.1-', '0.1.0', '0.01.0', '000.0001.0000000000'), '0.1.0+2': ('0.1.0+2', '0.1.0.2'), '0.1.0+2.3.4': ('0.1.0+2.3.4', '0.1.0+2+3+4', '0.1.0.2+3+4'), '0.1.0+2-3.4': ('0.1.0+2-3.4', '0.1.0+2-3+4', '0.1.0.2-3+4', '0.1.0.2_3+4'), -- cgit v1.2.1