make filetools tests pass with Python 3#2729
Conversation
…string obtained from .read()
… test/framework/filetools.py and test/framework/utilities.py
…ybuild.tools.generaloption
… list comprehensions are local in Python 3)
…Python 3.x when providing None value rather than a string)
… with equal number of changes
…nning module commands
| diff = False | ||
| for i in range(len(self.base_lines)): | ||
| lines = filter(None, self.get_line(i)) | ||
| lines = [line for line in self.get_line(i) if line] |
There was a problem hiding this comment.
is this exactly the same? Shouldn't you use if line is not None?
There was a problem hiding this comment.
No, with None as first element to function this is exactly equivalent, it's even mentioned explicitly to be so in the documentation: https://docs.python.org/2/library/functions.html#filter and https://docs.python.org/3/library/functions.html#filter
| # sort lines with most changes last; | ||
| # sort lines with equal number of changes alphabetically to ensure consistent output; | ||
| # limit number to MAX_DIFF_GROUPS | ||
| lines = sorted(lines, key=lambda line: (len(changes_dict[line]), line))[:MAX_DIFF_GROUPS] |
There was a problem hiding this comment.
I don't understand this? Your lambda returns a tuple? What does happen then by default if you compare tuples?
There was a problem hiding this comment.
Tuples are sorted by 1st element, and 2nd element is taken into account if the 1st elements are equal:
>>> (1,2) < (2,3)
True
>>> (2,2) < (2,3)
True
>>> (2,4) < (2,3)
False
So here, primary sorting is done based on # changes, secondary sorting (for lines with equal number of changes) is done alphabetically, which guarantees consistent ordering.
For some reason this was not a problem with Python 2.x (or Python 3.6 according to Travis), but with Python 3.7 I was getting different ordering sometimes for lines with equal number of changes... Not entirely sure why that happening, but this change fixes it.
No description provided.