Skip to content

Commit

Permalink
Make range objects not comparable
Browse files Browse the repository at this point in the history
This is an incompatible change in the implementation, but it's already guarded by a flag.
Also add a few more tests.

#5264

RELNOTES: None.
PiperOrigin-RevId: 218911186
  • Loading branch information
laurentlb authored and Copybara-Service committed Oct 26, 2018
1 parent d8d3776 commit ed49acc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public ComparisonException(String msg) {
public static final Ordering<Object> SKYLARK_COMPARATOR =
new Ordering<Object>() {
private int compareLists(SkylarkList o1, SkylarkList o2) {
if (o1 instanceof RangeList || o2 instanceof RangeList) {
throw new ComparisonException("Cannot compare range objects");
}

for (int i = 0; i < Math.min(o1.size(), o2.size()); i++) {
int cmp = compare(o1.get(i), o2.get(i));
if (cmp != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,9 +547,15 @@ public void testRangeType() throws Exception {
.testStatement("str(range(0, 10, 2)[::-2])", "range(8, -2, -4)")
.testStatement("str(range(5)[1::-1])", "range(1, -1, -1)")
.testIfErrorContains("step cannot be 0", "range(2, 3, 0)")
.testIfErrorContains(
"unsupported operand type(s) for *: 'range' and 'int'", "range(3) * 3");
;
.testIfErrorContains("unsupported operand type(s) for *: 'range' and 'int'", "range(3) * 3")
.testIfErrorContains("Cannot compare range objects", "range(3) < range(5)")
.testIfErrorContains("Cannot compare range objects", "range(4) > [1]")
.testStatement("4 in range(1, 10)", true)
.testStatement("4 in range(1, 3)", false)
.testStatement("4 in range(0, 8, 2)", true)
.testStatement("4 in range(1, 8, 2)", false)
.testStatement("range(0, 5, 10) == range(0, 5, 11)", true)
.testStatement("range(0, 5, 2) == [0, 2, 4]", false);
}

/**
Expand Down

0 comments on commit ed49acc

Please sign in to comment.