Skip to content

Commit 6b1039f

Browse files
committed
py: Fix range to support negative step
1 parent f7ea0a4 commit 6b1039f

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

py/range.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ func (it *RangeIterator) M__iter__() (Object, error) {
9090
// Range iterator next
9191
func (it *RangeIterator) M__next__() (Object, error) {
9292
r := it.Index
93-
if r >= it.Stop {
93+
if it.Step >= 0 && r >= it.Stop {
94+
return nil, StopIteration
95+
}
96+
97+
if it.Step < 0 && r <= it.Stop {
9498
return nil, StopIteration
9599
}
96100
it.Index += it.Step

py/tests/range.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2018 The go-python Authors. All rights reserved.
2+
# Use of this source code is governed by a BSD-style
3+
# license that can be found in the LICENSE file.
4+
5+
doc="range"
6+
a = range(255)
7+
b = [e for e in a]
8+
assert len(b) == 255
9+
a = range(100, 0, -1)
10+
b = [e for e in a]
11+
assert len(b) == 100
12+
13+
doc="finished"

0 commit comments

Comments
 (0)