From d04f72fb31bab43386e12963c1c6dec23ae16143 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Fri, 18 May 2012 14:52:24 +0200 Subject: [PATCH] Got rid of old __cmp__methods replaced by rich comparison. The __cmp__ methods are unsupported in Python 3. _doctest.py has been left untouched because it is likely it will not be migrated to Python 3. --- django/contrib/gis/geos/mutable_list.py | 29 +++++++++++++++------- django/contrib/gis/maps/google/overlays.py | 12 ++++++--- django/contrib/gis/measure.py | 24 +++++++++++++++--- django/dispatch/saferef.py | 10 ++++---- django/utils/functional.py | 23 +++++++++++------ 5 files changed, 69 insertions(+), 29 deletions(-) diff --git a/django/contrib/gis/geos/mutable_list.py b/django/contrib/gis/geos/mutable_list.py index cc28147661..1a9dcf0b5b 100644 --- a/django/contrib/gis/geos/mutable_list.py +++ b/django/contrib/gis/geos/mutable_list.py @@ -8,6 +8,9 @@ See also http://www.aryehleib.com/MutableLists.html Author: Aryeh Leib Taurog. """ +from django.utils.functional import total_ordering + +@total_ordering class ListMixin(object): """ A base class which provides complete list interface. @@ -143,20 +146,28 @@ class ListMixin(object): self.extend(cache) return self - def __cmp__(self, other): - 'cmp' + def __eq__(self, other): + for i in range(len(self)): + try: + c = self[i] == other[i] + except IndexError: + # must be other is shorter + return False + if not c: + return False + return True + + def __lt__(self, other): slen = len(self) for i in range(slen): try: - c = cmp(self[i], other[i]) + c = self[i] < other[i] except IndexError: # must be other is shorter - return 1 - else: - # elements not equal - if c: return c - - return cmp(slen, len(other)) + return False + if c: + return c + return slen < len(other) ### Public list interface Methods ### ## Non-mutating ## diff --git a/django/contrib/gis/maps/google/overlays.py b/django/contrib/gis/maps/google/overlays.py index c2ebb3c992..eaf12dad6d 100644 --- a/django/contrib/gis/maps/google/overlays.py +++ b/django/contrib/gis/maps/google/overlays.py @@ -1,5 +1,7 @@ -from django.utils.safestring import mark_safe from django.contrib.gis.geos import fromstr, Point, LineString, LinearRing, Polygon +from django.utils.functional import total_ordering +from django.utils.safestring import mark_safe + class GEvent(object): """ @@ -166,6 +168,7 @@ class GPolyline(GOverlayBase): return '%s, "%s", %s, %s' % (self.latlngs, self.color, self.weight, self.opacity) +@total_ordering class GIcon(object): """ Creates a GIcon object to pass into a Gmarker object. @@ -231,8 +234,11 @@ class GIcon(object): self.iconanchor = iconanchor self.infowindowanchor = infowindowanchor - def __cmp__(self, other): - return cmp(self.varname, other.varname) + def __eq__(self, other): + return self.varname == other.varname + + def __lt__(self, other): + return self.varname < other.varname def __hash__(self): # XOR with hash of GIcon type so that hash('varname') won't diff --git a/django/contrib/gis/measure.py b/django/contrib/gis/measure.py index a60398bccd..46e198c410 100644 --- a/django/contrib/gis/measure.py +++ b/django/contrib/gis/measure.py @@ -38,6 +38,8 @@ and Geoff Biggs' PhD work on dimensioned units for robotics. __all__ = ['A', 'Area', 'D', 'Distance'] from decimal import Decimal +from django.utils.functional import total_ordering + class MeasureBase(object): def default_units(self, kwargs): """ @@ -84,6 +86,7 @@ class MeasureBase(object): else: raise Exception('Could not find a unit keyword associated with "%s"' % unit_str) +@total_ordering class Distance(MeasureBase): UNITS = { 'chain' : 20.1168, @@ -178,9 +181,15 @@ class Distance(MeasureBase): def __str__(self): return '%s %s' % (getattr(self, self._default_unit), self._default_unit) - def __cmp__(self, other): + def __eq__(self, other): if isinstance(other, Distance): - return cmp(self.m, other.m) + return self.m == other.m + else: + return NotImplemented + + def __lt__(self, other): + if isinstance(other, Distance): + return self.m < other.m else: return NotImplemented @@ -244,6 +253,7 @@ class Distance(MeasureBase): def __nonzero__(self): return bool(self.m) +@total_ordering class Area(MeasureBase): # Getting the square units values and the alias dictionary. UNITS = dict([('sq_%s' % k, v ** 2) for k, v in Distance.UNITS.items()]) @@ -267,9 +277,15 @@ class Area(MeasureBase): def __str__(self): return '%s %s' % (getattr(self, self._default_unit), self._default_unit) - def __cmp__(self, other): + def __eq__(self, other): if isinstance(other, Area): - return cmp(self.sq_m, other.sq_m) + return self.sq_m == other.sq_m + else: + return NotImplemented + + def __lt__(self, other): + if isinstance(other, Area): + return self.sq_m < other.sq_m else: return NotImplemented diff --git a/django/dispatch/saferef.py b/django/dispatch/saferef.py index e060e619f8..2a2c8739fb 100644 --- a/django/dispatch/saferef.py +++ b/django/dispatch/saferef.py @@ -155,12 +155,12 @@ class BoundMethodWeakref(object): def __nonzero__( self ): """Whether we are still a valid reference""" return self() is not None - - def __cmp__( self, other ): + + def __eq__(self, other): """Compare with another reference""" - if not isinstance (other,self.__class__): - return cmp( self.__class__, type(other) ) - return cmp( self.key, other.key) + if not isinstance(other, self.__class__): + return self.__class__ == type(other) + return self.key == other.key def __call__(self): """Return a strong reference to the bound method diff --git a/django/utils/functional.py b/django/utils/functional.py index 5b88c3f3cb..31466ee8b8 100644 --- a/django/utils/functional.py +++ b/django/utils/functional.py @@ -58,6 +58,7 @@ def lazy(func, *resultclasses): function is evaluated on every access. """ + @total_ordering class __proxy__(Promise): """ Encapsulate a function call and act as a proxy for methods that are @@ -124,17 +125,23 @@ def lazy(func, *resultclasses): def __str_cast(self): return str(func(*self.__args, **self.__kw)) - def __cmp__(self, rhs): + def __cast(self): if self._delegate_str: - s = str(func(*self.__args, **self.__kw)) + return self.__str_cast() elif self._delegate_unicode: - s = unicode(func(*self.__args, **self.__kw)) + return self.__unicode_cast() else: - s = func(*self.__args, **self.__kw) - if isinstance(rhs, Promise): - return -cmp(rhs, s) - else: - return cmp(s, rhs) + return func(*self.__args, **self.__kw) + + def __eq__(self, other): + if isinstance(other, Promise): + other = other.__cast() + return self.__cast() == other + + def __lt__(self, other): + if isinstance(other, Promise): + other = other.__cast() + return self.__cast() < other def __mod__(self, rhs): if self._delegate_str: