From a23928ce806fb523f5a5aba12b3b844591e88d47 Mon Sep 17 00:00:00 2001 From: Zane Bitter Date: Wed, 15 Jun 2016 14:27:45 +0200 Subject: [PATCH] Implement the correct total ordering for scheduler.Timeout The commit 640abe0c12e63c207fcf67592838f112a29f5b43 implemented rich comparison methods for the scheduler.Timeout class in order to be compatible with Python3. However, it left out most of the checks to ensure that Timeouts are compared only to other timeouts. It also left behind a completely incorrect implmentation of __cmp__, although it didn't matter because that is ignored for objects that implement rich comparisons. This change deletes __cmp__, adds the type checking, and uses functools.total_ordering to cut down on the amount of boilerplate. Change-Id: Idf65d41705cefe9dab8ad5a518b142314077086b --- heat/engine/scheduler.py | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/heat/engine/scheduler.py b/heat/engine/scheduler.py index 927a81fe84..ab767ca304 100644 --- a/heat/engine/scheduler.py +++ b/heat/engine/scheduler.py @@ -11,6 +11,7 @@ # License for the specific language governing permissions and limitations # under the License. +import functools import sys import types @@ -48,6 +49,7 @@ def task_description(task): return encodeutils.safe_decode(repr(task)) +@functools.total_ordering class Timeout(BaseException): """Raised when task has exceeded its allotted (wallclock) running time. @@ -79,28 +81,15 @@ class Timeout(BaseException): return False def __eq__(self, other): - return not self < other and not other < self - - def __ne__(self, other): - return not self.__eq__(other) - - def __gt__(self, other): - return other < self - - def __ge__(self, other): - return not self < other - - def __le__(self, other): - return not other < self + if not isinstance(other, Timeout): + return NotImplemented + return not (self < other or other < self) def __lt__(self, other): if not isinstance(other, Timeout): return NotImplemented return self._duration.endtime() < other._duration.endtime() - def __cmp__(self, other): - return self < other - class TimedCancel(Timeout): def trigger(self, generator):