From 63ce3f770dc26a5c52c4c40414ecc160394b5c24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolfo=20Garc=C3=ADa=20Pe=C3=B1as=20=28kix=29?= Date: Wed, 30 Dec 2020 21:39:17 +0100 Subject: [PATCH] foldersort broken with instance comparison This patch solves a problem about the comparison of mails used in foldersort. When foldersort is used, for example with: lambda x,y: -cmp(x,y) The user gets an error: ERROR: While attempting to sync account 'accountname' '<' not supported between instances of 'K' and 'K' Traceback: File "offlineimap3/offlineimap/accounts.py", line 298, in syncrunner self.__sync() File "offlineimap3/offlineimap/accounts.py", line 374, in __sync remoterepos.getfolders() File "offlineimap3/offlineimap/repository/IMAP.py", line 725, in getfolders retval.sort(key=cmp2key(self.foldersort)) The problem is because in Python 3, we must specify all parameters for total_ordering (see https://docs.python.org/3/library/functools.html in the functools.total_ordering block): Given a class defining one or more rich comparison ordering methods, this class decorator supplies the rest. This simplifies the effort involved in specifying all of the possible rich comparison operations: The class must define one of __lt__(), __le__(), __gt__(), or __ge__(). In addition, the class should supply an __eq__() method. Also, see: https://docs.python.org/3.1/library/stdtypes.html#comparisons Instances of a class cannot be ordered with respect to other instances of the same class, or other types of object, unless the class defines enough of the methods __lt__(), __le__(), __gt__(), and __ge__() (in general, __lt__() and __eq__() are sufficient, if you want the conventional meanings of the comparison operators). This patch implements all methods. Closes #33 --- offlineimap/repository/IMAP.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/offlineimap/repository/IMAP.py b/offlineimap/repository/IMAP.py index 699f0c9..b732243 100644 --- a/offlineimap/repository/IMAP.py +++ b/offlineimap/repository/IMAP.py @@ -720,6 +720,24 @@ class IMAPRepository(BaseRepository): return mycmp(self.obj.getvisiblename(), other.obj.getvisiblename()) + def __lt__(self, other): + return self.__cmp__(other) < 0 + + def __le__(self, other): + return self.__cmp__(other) <= 0 + + def __gt__(self, other): + return self.__cmp__(other) > 0 + + def __ge__(self, other): + return self.__cmp__(other) >= 0 + + def __eq__(self, other): + return self.__cmp__(other) == 0 + + def __ne__(self, other): + return self.__cmp__(other) != 0 + return K retval.sort(key=cmp2key(self.foldersort))