From 3ff7455dd474533a6ceaef6f7b9e49c8059b07a2 Mon Sep 17 00:00:00 2001 From: Rob Browning Date: Mon, 13 Mar 2017 01:08:39 -0500 Subject: [PATCH] Don't use cmp() Python 3 dropped it. Signed-off-by: Rob Browning Tested-by: Rob Browning --- cmd/midx-cmd.py | 2 +- lib/bup/_helpers.c | 24 ++++++++++++++++++++++++ lib/bup/git.py | 2 +- lib/bup/helpers.py | 1 - lib/bup/index.py | 36 +++++++++++++++++++++++++++++++----- 5 files changed, 57 insertions(+), 8 deletions(-) diff --git a/cmd/midx-cmd.py b/cmd/midx-cmd.py index 0224ab4..7b4a2ad 100755 --- a/cmd/midx-cmd.py +++ b/cmd/midx-cmd.py @@ -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 '' diff --git a/lib/bup/_helpers.c b/lib/bup/_helpers.c index 8085cfc..04a5cb6 100644 --- a/lib/bup/_helpers.c +++ b/lib/bup/_helpers.c @@ -15,6 +15,7 @@ #include #include #include +#include #ifdef HAVE_SYS_MMAN_H #include @@ -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)" diff --git a/lib/bup/git.py b/lib/bup/git.py index 798bea2..8150f7c 100644 --- a/lib/bup/git.py +++ b/lib/bup/git.py @@ -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: diff --git a/lib/bup/helpers.py b/lib/bup/helpers.py index a4dc337..26e96c8 100644 --- a/lib/bup/helpers.py +++ b/lib/bup/helpers.py @@ -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: diff --git a/lib/bup/index.py b/lib/bup/index.py index 5f6c366..a488086 100644 --- a/lib/bup/index.py +++ b/lib/bup/index.py @@ -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()) -- 2.39.2