Ticket #5627: cmp_versions_v2.patch

File cmp_versions_v2.patch, 4.6 KB (added by Vladimir Perić, 5 years ago)

Second version, fix silly typo :-)

  • twisted/python/test/test_versions.py

    diff --git twisted/python/test/test_versions.py twisted/python/test/test_versions.py
    index 79388cf..f75a205 100644
    import sys 
    55from cStringIO import StringIO
    66
    77from twisted.python.versions import getVersionString, IncomparableVersions
    8 from twisted.python.versions import Version, _inf
     8from twisted.python.versions import Version
    99from twisted.python.filepath import FilePath
    1010
    1111from twisted.trial import unittest
    class VersionsTest(unittest.TestCase): 
    101101        """
    102102        va = Version("whatever", 1, 0, 0, prerelease=1)
    103103        vb = Version("whatever", 1, 0, 0, prerelease=2)
     104        vc = Version("whatever", 1, 0, 0, prerelease=2)
     105
    104106        self.assertTrue(va < vb)
    105107        self.assertFalse(va > vb)
    106108        self.assertNotEqual(va, vb)
    107 
    108 
    109     def test_infComparison(self):
    110         """
    111         L{_inf} is equal to L{_inf}.
    112 
    113         This is a regression test.
    114         """
    115         self.assertEqual(_inf, _inf)
     109        self.assertEqual(vb, vc)
    116110
    117111
    118112    def testDontAllowBuggyComparisons(self):
    119113        self.assertRaises(IncomparableVersions,
    120                           cmp,
     114                          Version._cmp,
    121115                          Version("dummy", 1, 0, 0),
    122116                          Version("dumym", 1, 0, 0))
    123117
  • twisted/python/versions.py

    diff --git twisted/python/versions.py twisted/python/versions.py
    index d6f6715..0d9a48d 100644
    See L{Version}. 
    1111import sys, os
    1212
    1313
    14 class _inf(object):
    15     """
    16     An object that is bigger than all other objects.
    17     """
    18     def __cmp__(self, other):
    19         """
    20         @param other: Another object.
    21         @type other: any
    22 
    23         @return: 0 if other is inf, 1 otherwise.
    24         @rtype: C{int}
    25         """
    26         if other is _inf:
    27             return 0
    28         return 1
    29 
    30 _inf = _inf()
    31 
    32 
    3314class IncomparableVersions(TypeError):
    3415    """
    3516    Two versions could not be compared.
    class Version(object): 
    11192            self.package,
    11293            self.short())
    11394
     95    def __eq__(self, other):
     96        c = self._cmp(other)
     97        if c is NotImplemented:
     98            return c
     99        return c == 0
     100
     101    def __ne__(self, other):
     102        c = self._cmp(other)
     103        if c is NotImplemented:
     104            return c
     105        return c != 0
     106
     107    def __lt__(self, other):
     108        c = self._cmp(other)
     109        if c is NotImplemented:
     110            return c
     111        return c < 0
    114112
    115     def __cmp__(self, other):
     113    def __le__(self, other):
     114        c = self._cmp(other)
     115        if c is NotImplemented:
     116            return c
     117        return c <= 0
     118
     119    def __gt__(self, other):
     120        c = self._cmp(other)
     121        if c is NotImplemented:
     122            return c
     123        return c > 0
     124
     125    def __ge__(self, other):
     126        c = self._cmp(other)
     127        if c is NotImplemented:
     128            return c
     129        return c >= 0
     130
     131    def _cmp(self, other):
    116132        """
    117133        Compare two versions, considering major versions, minor versions, micro
    118134        versions, then prereleases.
    class Version(object): 
    136152            raise IncomparableVersions("%r != %r"
    137153                                       % (self.package, other.package))
    138154
    139         if self.prerelease is None:
    140             prerelease = _inf
    141         else:
    142             prerelease = self.prerelease
    143 
    144         if other.prerelease is None:
    145             otherpre = _inf
    146         else:
    147             otherpre = other.prerelease
     155        v1 = (self.major, self.minor, self.micro)
     156        v2 = (other.major, other.minor, other.micro)
     157       
     158        if v1 != v2:
     159            # if version number don't match, the prerelease doesn't matter
     160            if v1 < v2:
     161                return -1
     162            else:
     163                return 1
    148164
    149         x = cmp((self.major,
    150                     self.minor,
    151                     self.micro,
    152                     prerelease),
    153                    (other.major,
    154                     other.minor,
    155                     other.micro,
    156                     otherpre))
    157         return x
     165        if (not self.prerelease and not other.prerelease):
     166            return 0
     167        elif (self.prerelease and not other.prerelease):
     168            return -1
     169        elif (not self.prerelease and other.prerelease):
     170            return 1
     171        elif (self.prerelease and other.prerelease):
     172            if self.prerelease == other.prerelease:
     173                return 0
     174            elif self.prerelease < other.prerelease:
     175                return -1
     176            else:
     177                return 1
    158178
    159179
    160180    def _parseSVNEntries_4(self, entriesFile):