]> arthur.barton.de Git - bup.git/commitdiff
Don't use cmp()
authorRob Browning <rlb@defaultvalue.org>
Mon, 13 Mar 2017 06:08:39 +0000 (01:08 -0500)
committerRob Browning <rlb@defaultvalue.org>
Sat, 27 Jan 2018 16:11:32 +0000 (10:11 -0600)
Python 3 dropped it.

Signed-off-by: Rob Browning <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
cmd/midx-cmd.py
lib/bup/_helpers.c
lib/bup/git.py
lib/bup/helpers.py
lib/bup/index.py

index 0224ab40238f3239c937d5b53f67e85a4802fa02..7b4a2adc689c535cc948395ea0fbdb3497b3198b 100755 (executable)
@@ -105,7 +105,7 @@ def _do_midx(outdir, outfilename, infilenames, prefixstr):
             for n in ix.idxnames:
                 allfilenames.append(os.path.basename(n))
             total += len(ix)
-        inp.sort(lambda x,y: cmp(str(y[0][y[2]:y[2]+20]),str(x[0][x[2]:x[2]+20])))
+        inp.sort(reverse=True, key=lambda x: str(x[0][x[2]:x[2]+20]))
 
         if not _first: _first = outdir
         dirprefix = (_first != outdir) and git.repo_rel(outdir)+': ' or ''
index 8085cfc4b2ea3aeb9091a409cdfd3e265571bf17..04a5cb6a7bd01c71ea383ecc66dbcb6f2865374c 100644 (file)
@@ -15,6 +15,7 @@
 #include <stdint.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
 
 #ifdef HAVE_SYS_MMAN_H
 #include <sys/mman.h>
@@ -188,6 +189,27 @@ static int bup_ullong_from_py(unsigned PY_LONG_LONG *x, PyObject *py,
 }
 
 
+static PyObject *bup_bytescmp(PyObject *self, PyObject *args)
+{
+    PyObject *py_s1, *py_s2;  // This is really a PyBytes/PyString
+    if (!PyArg_ParseTuple(args, "SS", &py_s1, &py_s2))
+       return NULL;
+    char *s1, *s2;
+    Py_ssize_t s1_len, s2_len;
+    if (PyBytes_AsStringAndSize(py_s1, &s1, &s1_len) == -1)
+        return NULL;
+    if (PyBytes_AsStringAndSize(py_s2, &s2, &s2_len) == -1)
+        return NULL;
+    const Py_ssize_t n = (s1_len < s2_len) ? s1_len : s2_len;
+    const int cmp = memcmp(s1, s2, n);
+    if (cmp != 0)
+        return PyLong_FromLong(cmp);
+    if (s1_len == s2_len)
+        return PyLong_FromLong(0);;
+    return PyLong_FromLong((s1_len < s2_len) ? -1 : 1);
+}
+
+
 // Probably we should use autoconf or something and set HAVE_PY_GETARGCARGV...
 #if __WIN32__ || __CYGWIN__
 
@@ -1543,6 +1565,8 @@ static PyMethodDef helper_methods[] = {
     { "localtime", bup_localtime, METH_VARARGS,
       "Return struct_time elements plus the timezone offset and name." },
 #endif
+    { "bytescmp", bup_bytescmp, METH_VARARGS,
+      "Return a negative value if x < y, zero if equal, positive otherwise."},
 #ifdef BUP_MINCORE_BUF_TYPE
     { "mincore", bup_mincore, METH_VARARGS,
       "For mincore(src, src_n, src_off, dest, dest_off)"
index 798bea2cb7328c9ca9356379843e2f5029194952..8150f7c0c593ef3a1795b0866ee16b8a1b3282f7 100644 (file)
@@ -552,7 +552,7 @@ class PackIdxList:
             if self.bloom is None and os.path.exists(bfull):
                 self.bloom = bloom.ShaBloom(bfull)
             self.packs = list(set(d.values()))
-            self.packs.sort(lambda x,y: -cmp(len(x),len(y)))
+            self.packs.sort(reverse=True, key=lambda x: len(x))
             if self.bloom and self.bloom.valid() and len(self.bloom) >= len(self):
                 self.do_bloom = True
             else:
index a4dc3376c67aac5b0b9c94a7c10822eecbed912a..26e96c8e0d8934bf7841cd567b15efae1d0924e0 100644 (file)
@@ -28,7 +28,6 @@ sc_arg_max = os.sysconf('SC_ARG_MAX')
 if sc_arg_max == -1:  # "no definite limit" - let's choose 2M
     sc_arg_max = 2 * 1024 * 1024
 
-
 def last(iterable):
     result = None
     for result in iterable:
index 5f6c366957cb93d0b98e90dd411c4b09bd890f08..a4880867cb0e343ea9f19fa9a18677f141442e64 100644 (file)
@@ -1,7 +1,7 @@
 import errno, metadata, os, stat, struct, tempfile
 
 from bup import xstat
-from bup._helpers import UINT_MAX
+from bup._helpers import UINT_MAX, bytescmp
 from bup.helpers import (add_error, log, merge_iter, mmap_readwrite,
                          progress, qprogress, resolve_parent, slashappend)
 
@@ -279,10 +279,36 @@ class Entry:
     def is_fake(self):
         return not self.ctime
 
-    def __cmp__(a, b):
-        return (cmp(b.name, a.name)
-                or cmp(a.is_valid(), b.is_valid())
-                or cmp(a.is_fake(), b.is_fake()))
+    def _cmp(self, other):
+        # Note reversed name ordering
+        bc = bytescmp(other.name, self.name)
+        if bc != 0:
+            return bc
+        vc = self.is_valid() - other.is_valid()
+        if vc != 0:
+            return vc
+        fc = self.is_fake() - other.is_fake()
+        if fc != 0:
+            return fc
+        return 0
+
+    def __eq__(self, other):
+        return self._cmp(other) == 0
+
+    def __ne__():
+        return self._cmp(other) != 0
+
+    def __lt__(self, other):
+        return self._cmp(other) < 0
+
+    def __gt__(self, other):
+        return self._cmp(other) > 0
+
+    def __le__():
+        return self._cmp(other) <= 0
+
+    def __ge__():
+        return self._cmp(other) >= 0
 
     def write(self, f):
         f.write(self.basename + '\0' + self.packed())