Bug #266493 - Never return a long from vercmp() since that can trigger an
authorZac Medico <zmedico@gentoo.org>
Thu, 30 Apr 2009 07:16:36 +0000 (07:16 -0000)
committerZac Medico <zmedico@gentoo.org>
Thu, 30 Apr 2009 07:16:36 +0000 (07:16 -0000)
OverflowError if it's returned by a __cmp__ implementation. Thanks to
Douglas Anderson <dja@gendja.com> for the initial patch. I've modified it
to use the (a > b) - (a < b) construct as suggested in the py3k docs, since
cmp() is no longer supported in py3k. (trunk r13353)

svn path=/main/branches/2.1.6/; revision=13509

pym/portage/versions.py

index d3f256dc08864f174bf12b3418d9ad382995cbec..13ce47897afa1e9d93cf6b0448e3714d6db33ea4 100644 (file)
@@ -124,9 +124,12 @@ def vercmp(ver1, ver2, silent=1):
                        vercmp_cache[mykey] = 1
                        return 1
                elif list1[i] != list2[i]:
-                       vercmp_cache[mykey] = list1[i] - list2[i]
-                       return list1[i] - list2[i]
-       
+                       a = list1[i]
+                       b = list2[i]
+                       rval = (a > b) - (a < b)
+                       vercmp_cache[mykey] = rval
+                       return rval
+
        # main version is equal, so now compare the _suffix part
        list1 = match1.group(6).split("_")[1:]
        list2 = match2.group(6).split("_")[1:]
@@ -142,7 +145,11 @@ def vercmp(ver1, ver2, silent=1):
                else:
                        s2 = suffix_regexp.match(list2[i]).groups()
                if s1[0] != s2[0]:
-                       return suffix_value[s1[0]] - suffix_value[s2[0]]
+                       a = suffix_value[s1[0]]
+                       b = suffix_value[s2[0]]
+                       rval = (a > b) - (a < b)
+                       vercmp_cache[mykey] = rval
+                       return rval
                if s1[1] != s2[1]:
                        # it's possible that the s(1|2)[1] == ''
                        # in such a case, fudge it.
@@ -154,9 +161,11 @@ def vercmp(ver1, ver2, silent=1):
                                r2 = int(s2[1])
                        except ValueError:
                                r2 = 0
-                       if r1 - r2:
-                               return r1 - r2
-       
+                       rval = (r1 > r2) - (r1 < r2)
+                       if rval:
+                               vercmp_cache[mykey] = rval
+                               return rval
+
        # the suffix part is equal to, so finally check the revision
        if match1.group(10):
                r1 = int(match1.group(10))
@@ -166,8 +175,9 @@ def vercmp(ver1, ver2, silent=1):
                r2 = int(match2.group(10))
        else:
                r2 = 0
-       vercmp_cache[mykey] = r1 - r2
-       return r1 - r2
+       rval = (r1 > r2) - (r1 < r2)
+       vercmp_cache[mykey] = rval
+       return rval
        
 def pkgcmp(pkg1, pkg2):
        """