Skip to content

objrange: Allow return of non-small ints. #17685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy