Implement the correct total ordering for scheduler.Timeout
The commit 640abe0c12
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
This commit is contained in:
parent
ec36a7886d
commit
a23928ce80
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue