From 6ca328ba94d2dcd2e1d3d0b7adc41b8d3881aa45 Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone Date: Mon, 18 Feb 2019 19:24:30 -0500 Subject: [PATCH] Fix comparison of NummedObj against non-NummedObj Previously this would explode with AttributeError. --- src/allmydata/util/nummedobj.py | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/allmydata/util/nummedobj.py b/src/allmydata/util/nummedobj.py index 1d338ec45..50d7c6454 100644 --- a/src/allmydata/util/nummedobj.py +++ b/src/allmydata/util/nummedobj.py @@ -1,7 +1,9 @@ -import collections, itertools +import collections, itertools, functools objnums = collections.defaultdict(itertools.count) + +@functools.total_ordering class NummedObj(object): """ This is useful for nicer debug printouts. Instead of objects of the same class being @@ -27,22 +29,14 @@ class NummedObj(object): return "<%s #%d>" % (self._classname, self._objid,) def __lt__(self, other): - return (self._objid, self._classname,) < (other._objid, other._classname,) - - def __le__(self, other): - return (self._objid, self._classname,) <= (other._objid, other._classname,) + if isinstance(other, NummedObj): + return (self._objid, self._classname,) < (other._objid, other._classname,) + return NotImplemented def __eq__(self, other): - return (self._objid, self._classname,) == (other._objid, other._classname,) - - def __ne__(self, other): - return (self._objid, self._classname,) != (other._objid, other._classname,) - - def __gt__(self, other): - return (self._objid, self._classname,) > (other._objid, other._classname,) - - def __ge__(self, other): - return (self._objid, self._classname,) >= (other._objid, other._classname,) + if isinstance(other, NummedObj): + return (self._objid, self._classname,) == (other._objid, other._classname,) + return NotImplemented def __hash__(self): return id(self)