Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions py/objrange.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ typedef struct _mp_obj_range_it_t {
static mp_obj_t range_it_iternext(mp_obj_t o_in) {
mp_obj_range_it_t *o = MP_OBJ_TO_PTR(o_in);
if ((o->step > 0 && o->cur < o->stop) || (o->step < 0 && o->cur > o->stop)) {
mp_obj_t o_out = MP_OBJ_NEW_SMALL_INT(o->cur);
mp_obj_t o_out = mp_obj_new_int(o->cur);
o->cur += o->step;
return o_out;
} else {
Expand Down Expand Up @@ -132,7 +132,7 @@ static mp_obj_t range_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
case MP_UNARY_OP_BOOL:
return mp_obj_new_bool(len > 0);
case MP_UNARY_OP_LEN:
return MP_OBJ_NEW_SMALL_INT(len);
return mp_obj_new_int(len);
default:
return MP_OBJ_NULL; // op not supported
}
Expand Down Expand Up @@ -173,7 +173,7 @@ static mp_obj_t range_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
}
#endif
size_t index_val = mp_get_index(self->base.type, len, index, false);
return MP_OBJ_NEW_SMALL_INT(self->start + index_val * self->step);
return mp_obj_new_int(self->start + index_val * self->step);
} else {
return MP_OBJ_NULL; // op not supported
}
Expand Down
26 changes: 26 additions & 0 deletions tests/basics/builtin_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,32 @@
print(range(1, 100, -5)[5:15:-3])
print(range(1, 100, -5)[15:5:3])

for i in range(24, 67):
k = (1 << i) - 2
try:
if k == 65:
raise OverflowError("fake overflow error to stop cpython")
k0 = range(k, k+1)[0]
if k0 != k:
print(f"Range element {k} vs {k0}")
break
k0 = len(range(0, k))
if k0 != k:
print(f"Range length {k} vs {k0}")
break
k0 = len(range(-k, 0))
if k0 != k:
print(f"Range length {k} vs {k0}")
break
k = -k
k0 = range(k, k+1)[0]
if k0 != k:
print(f"Range element {k} vs {k0}")
break
except OverflowError:
print("range element/length test OK")
break

# for this case uPy gives a different stop value but the listed elements are still correct
print(list(range(7, -2, -4)[2:-2:]))

Expand Down
Loading