summaryrefslogtreecommitdiff
path: root/giscanner/message.py
diff options
context:
space:
mode:
authorSimon Feltman <sfeltman@src.gnome.org>2014-04-30 17:08:29 -0700
committerColin Walters <walters@verbum.org>2015-09-29 23:16:32 -0400
commit10175a1b54fff30291bbb5ff849dc3a048a4b8dc (patch)
treee1e58f59728a2abac60e579cea3c929bd46b2ea8 /giscanner/message.py
parent3dca44e3f1aba172b8a71d3860d55167dc35a5dc (diff)
downloadgobject-introspection-10175a1b54fff30291bbb5ff849dc3a048a4b8dc.tar.gz
giscanner: Use rich comparison methods for Python 3 compatibility
Add lt, le, gt, ge, eq, ne, and hash dunder methods to all classes that implement custom comparisons with __cmp__. This is needed to support Python 3 compatible sorting of instances of these classes. Avoid using @functools.total_ordering which does not work for some of these classes and also is not available in Python 2.6. https://bugzilla.gnome.org/show_bug.cgi?id=679438
Diffstat (limited to 'giscanner/message.py')
-rw-r--r--giscanner/message.py31
1 files changed, 27 insertions, 4 deletions
diff --git a/giscanner/message.py b/giscanner/message.py
index d474ae9d..f36bcbc1 100644
--- a/giscanner/message.py
+++ b/giscanner/message.py
@@ -27,6 +27,7 @@ from __future__ import unicode_literals
import os
import sys
+import operator
from . import utils
@@ -48,12 +49,34 @@ class Position(object):
self.line = line
self.column = column
- def __cmp__(self, other):
- return cmp((self.filename, self.line, self.column),
- (other.filename, other.line, other.column))
+ def _compare(self, other, op):
+ return op((self.filename, self.line, self.column),
+ (other.filename, other.line, other.column))
+
+ def __lt__(self, other):
+ return self._compare(other, operator.lt)
+
+ def __gt__(self, other):
+ return self._compare(other, operator.gt)
+
+ def __ge__(self, other):
+ return self._compare(other, operator.ge)
+
+ def __le__(self, other):
+ return self._compare(other, operator.le)
+
+ def __eq__(self, other):
+ return self._compare(other, operator.eq)
+
+ def __ne__(self, other):
+ return self._compare(other, operator.ne)
+
+ def __hash__(self):
+ return hash((self.filename, self.line, self.column))
def __repr__(self):
- return '<Position %s:%d:%d>' % (os.path.basename(self.filename), self.line or -1,
+ return '<Position %s:%d:%d>' % (os.path.basename(self.filename),
+ self.line or -1,
self.column or -1)
def format(self, cwd):