[toward-py3k] rewrite __cmp__ (closes #2715115)
authorNicolas Chauvat <nicolas.chauvat@logilab.fr>
Mon, 22 Apr 2013 14:54:22 +0200
changeset 8892 80783605d270
parent 8891 ada2f065f279
child 8907 ddeb02b02222
[toward-py3k] rewrite __cmp__ (closes #2715115) In Python 3k `__cmp__` special method is no longer supported. So we split it in `__lt__` and `__eq__`. details on Python3k behavior here: http://docs.python.org/3.0/whatsnew/3.0.html#ordering-comparisons
entity.py
schema.py
server/edition.py
server/sources/__init__.py
--- a/entity.py	Mon Apr 08 17:33:49 2013 +0200
+++ b/entity.py	Mon Apr 22 14:54:22 2013 +0200
@@ -554,7 +554,10 @@
         return '<Entity %s %s %s at %s>' % (
             self.e_schema, self.eid, list(self.cw_attr_cache), id(self))
 
-    def __cmp__(self, other):
+    def __lt__(self, other):
+        raise NotImplementedError('comparison not implemented for %s' % self.__class__)
+
+    def __eq__(self, other):
         raise NotImplementedError('comparison not implemented for %s' % self.__class__)
 
     def _cw_update_attr_cache(self, attrcache):
--- a/schema.py	Mon Apr 08 17:33:49 2013 +0200
+++ b/schema.py	Mon Apr 22 14:54:22 2013 +0200
@@ -700,10 +700,15 @@
     def __repr__(self):
         return '%s(%s)' % (self.__class__.__name__, self.full_rql)
 
-    def __cmp__(self, other):
+    def __lt__(self, other):
         if hasattr(other, 'expression'):
-            return cmp(other.expression, self.expression)
-        return -1
+            return self.expression < other.expression
+        return True
+
+    def __eq__(self, other):
+        if hasattr(other, 'expression'):
+            return self.expression == other.expression
+        return False
 
     def __deepcopy__(self, memo):
         return self.__class__(self.expression, self.mainvars)
--- a/server/edition.py	Mon Apr 08 17:33:49 2013 +0200
+++ b/server/edition.py	Mon Apr 22 14:54:22 2013 +0200
@@ -48,9 +48,12 @@
         # dict|set keyable
         return hash(id(self))
 
-    def __cmp__(self, other):
+    def __lt__(self, other):
         # we don't want comparison by value inherited from dict
-        return cmp(id(self), id(other))
+        return id(self) < id(other)
+
+    def __eq__(self, other):
+        return id(self) == id(other)
 
     def __setitem__(self, attr, value):
         assert attr != 'eid'
--- a/server/sources/__init__.py	Mon Apr 08 17:33:49 2013 +0200
+++ b/server/sources/__init__.py	Mon Apr 22 14:54:22 2013 +0200
@@ -140,17 +140,20 @@
         return '<%s %s source %s @%#x>' % (self.uri, self.__class__.__name__,
                                            self.eid, id(self))
 
-    def __cmp__(self, other):
+    def __lt__(self, other):
         """simple comparison function to get predictable source order, with the
         system source at last
         """
         if self.uri == other.uri:
-            return 0
+            return False
         if self.uri == 'system':
-            return 1
+            return False
         if other.uri == 'system':
-            return -1
-        return cmp(self.uri, other.uri)
+            return True
+        return self.uri < other.uri
+
+    def __eq__(self, other):
+        return self.uri == other.uri
 
     def backup(self, backupfile, confirm, format='native'):
         """method called to create a backup of source's data"""