Skip to content

Commit ab19a7c

Browse files
committed
fix comparison for OpenSSL pre-release
Signed-off-by: Keshav Priyadarshi <git@keshav.space>
1 parent 033e23f commit ab19a7c

1 file changed

Lines changed: 36 additions & 1 deletion

File tree

src/univers/versions.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,19 @@ class LegacyOpensslVersion(Version):
385385
univers.versions.InvalidVersion: '3.0.2' is not a valid <class 'univers.versions.LegacyOpensslVersion'>
386386
"""
387387

388+
major = attr.ib(type=int, default=None, repr=False)
389+
minor = attr.ib(type=int, default=None, repr=False)
390+
build = attr.ib(type=int, default=None, repr=False)
391+
patch = attr.ib(type=str, default=None, repr=False)
392+
393+
def __attrs_post_init__(self):
394+
Version.__attrs_post_init__(self)
395+
major, minor, build, patch = self.parse(self.string)
396+
object.__setattr__(self, "major", major)
397+
object.__setattr__(self, "minor", minor)
398+
object.__setattr__(self, "build", build)
399+
object.__setattr__(self, "patch", patch)
400+
388401
@classmethod
389402
def is_valid(cls, string):
390403
return bool(cls.parse(string))
@@ -445,7 +458,29 @@ def build_value(cls, string):
445458
return cls.parse(string)
446459

447460
def __str__(self):
448-
return f"{self.value[0]}.{self.value[1]}.{self.value[2]}{self.value[3]}"
461+
return f"{self.major}.{self.minor}.{self.build}{self.patch}"
462+
463+
def __lt__(self, other):
464+
if not isinstance(other, self.__class__):
465+
return NotImplemented
466+
# Check if versions have the same base, and `one and only one` of them is a pre-release.
467+
if (self.major, self.minor, self.build) == (other.major, other.minor, other.build) and (
468+
self.is_prerelease() != other.is_prerelease()
469+
):
470+
return self.is_prerelease()
471+
return self.value.__lt__(other.value)
472+
473+
def __gt__(self, other):
474+
if not isinstance(other, self.__class__):
475+
return NotImplemented
476+
if (self.major, self.minor, self.build) == (other.major, other.minor, other.build) and (
477+
self.is_prerelease() != other.is_prerelease()
478+
):
479+
return other.is_prerelease()
480+
return self.value.__gt__(other.value)
481+
482+
def is_prerelease(self):
483+
return self.patch.startswith(("-beta", "-alpha"))
449484

450485

451486
@attr.s(frozen=True, order=False, eq=False, hash=True)

0 commit comments

Comments
 (0)