comparison roundup/password.py @ 5414:3fa026621f69

Python 3 preparation: comparisons. Python 3 no longer has the cmp function, or cmp= arguments to sorting functions / methods (key= must be used instead), and requires rich comparison methods such as __lt__ to be defined instead of using __cmp__. All of the comparison mechanisms supported in Python 3 are also supported in Python 2. This patch makes the corresponding changes in Roundup to use key functions and rich comparison methods. In the case of the JournalPassword and Permission classes, only __eq__ and __ne__ are defined as I don't see ordered comparisons as useful there (and for Permission, the old __cmp__ function didn't try to provide a valid ordering). In the case of the Date class, I kept the __cmp__ method and implemented the others in terms of it, to avoid excess repetitiveness in duplicating implementation code for all six rich comparison methods. In roundup/admin.py, help_commands_html used operator.attrgetter to produce the second argument of sorted() - which would be reasonable for a key function, but the second argument is the cmp function in Python 2, not a key function (and the key function must be a named argument not a positional argument in Python 3). That function appears to be completely unused, so I expect that code never worked. This patch adds the missing key= to that sorted() call, but it would also be reasonable to remove the unused function completely instead.
author Joseph Myers <jsm@polyomino.org.uk>
date Wed, 25 Jul 2018 00:39:37 +0000
parents a391a071d045
children 2d6a92c3e212
comparison
equal deleted inserted replaced
5413:7a41dd2abbbd 5414:3fa026621f69
221 """ 221 """
222 return "{%s}*encrypted*" % (self.scheme,) 222 return "{%s}*encrypted*" % (self.scheme,)
223 223
224 __str__ = dummystr 224 __str__ = dummystr
225 225
226 def __cmp__(self, other): 226 def __eq__(self, other):
227 """Compare this password against another password.""" 227 """Compare this password against another password."""
228 # check to see if we're comparing instances 228 # check to see if we're comparing instances
229 if isinstance(other, self.__class__): 229 if isinstance(other, self.__class__):
230 if self.scheme != other.scheme: 230 if self.scheme != other.scheme:
231 return cmp(self.scheme, other.scheme) 231 return False
232 return cmp(self.password, other.password) 232 return self.password == other.password
233 233
234 # assume password is plaintext 234 # assume password is plaintext
235 if self.password is None: 235 if self.password is None:
236 raise ValueError('Password not set') 236 raise ValueError('Password not set')
237 return cmp(self.password, encodePassword(other, self.scheme, 237 return self.password == encodePassword(other, self.scheme,
238 self.password or None)) 238 self.password or None)
239
240 def __ne__(self, other):
241 return not self.__eq__(other)
239 242
240 class Password(JournalPassword): 243 class Password(JournalPassword):
241 """The class encapsulates a Password property type value in the database. 244 """The class encapsulates a Password property type value in the database.
242 245
243 The encoding of the password is one if None, 'SHA', 'MD5' or 'plaintext'. 246 The encoding of the password is one if None, 'SHA', 'MD5' or 'plaintext'.

Roundup Issue Tracker: http://roundup-tracker.org/