Skip to content

Commit 66eaf84

Browse files
committed
py: Replace mp_const_stop_iteration object with MP_OBJ_NULL.
1 parent 688e220 commit 66eaf84

23 files changed

+171
-64
lines changed

py/builtin.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_abs_obj, mp_builtin_abs);
103103
STATIC mp_obj_t mp_builtin_all(mp_obj_t o_in) {
104104
mp_obj_t iterable = rt_getiter(o_in);
105105
mp_obj_t item;
106-
while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
106+
while ((item = rt_iternext(iterable)) != MP_OBJ_NULL) {
107107
if (!rt_is_true(item)) {
108108
return mp_const_false;
109109
}
@@ -116,7 +116,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_all_obj, mp_builtin_all);
116116
STATIC mp_obj_t mp_builtin_any(mp_obj_t o_in) {
117117
mp_obj_t iterable = rt_getiter(o_in);
118118
mp_obj_t item;
119-
while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
119+
while ((item = rt_iternext(iterable)) != MP_OBJ_NULL) {
120120
if (rt_is_true(item)) {
121121
return mp_const_true;
122122
}
@@ -232,7 +232,7 @@ STATIC mp_obj_t mp_builtin_max(uint n_args, const mp_obj_t *args) {
232232
mp_obj_t iterable = rt_getiter(args[0]);
233233
mp_obj_t max_obj = NULL;
234234
mp_obj_t item;
235-
while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
235+
while ((item = rt_iternext(iterable)) != MP_OBJ_NULL) {
236236
if (max_obj == NULL || mp_obj_less(max_obj, item)) {
237237
max_obj = item;
238238
}
@@ -261,7 +261,7 @@ STATIC mp_obj_t mp_builtin_min(uint n_args, const mp_obj_t *args) {
261261
mp_obj_t iterable = rt_getiter(args[0]);
262262
mp_obj_t min_obj = NULL;
263263
mp_obj_t item;
264-
while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
264+
while ((item = rt_iternext(iterable)) != MP_OBJ_NULL) {
265265
if (min_obj == NULL || mp_obj_less(item, min_obj)) {
266266
min_obj = item;
267267
}
@@ -285,8 +285,8 @@ STATIC mp_obj_t mp_builtin_min(uint n_args, const mp_obj_t *args) {
285285
MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_min_obj, 1, mp_builtin_min);
286286

287287
STATIC mp_obj_t mp_builtin_next(mp_obj_t o) {
288-
mp_obj_t ret = rt_iternext(o);
289-
if (ret == mp_const_stop_iteration) {
288+
mp_obj_t ret = rt_iternext_allow_raise(o);
289+
if (ret == MP_OBJ_NULL) {
290290
nlr_jump(mp_obj_new_exception(&mp_type_StopIteration));
291291
} else {
292292
return ret;
@@ -362,7 +362,7 @@ STATIC mp_obj_t mp_builtin_sum(uint n_args, const mp_obj_t *args) {
362362
}
363363
mp_obj_t iterable = rt_getiter(args[0]);
364364
mp_obj_t item;
365-
while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
365+
while ((item = rt_iternext(iterable)) != MP_OBJ_NULL) {
366366
value = rt_binary_op(RT_BINARY_OP_ADD, value, item);
367367
}
368368
return value;

py/emitnative.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ STATIC void emit_native_for_iter(emit_t *emit, int label) {
987987
emit_access_stack(emit, 1, &vtype, REG_ARG_1);
988988
assert(vtype == VTYPE_PYOBJ);
989989
emit_call(emit, RT_F_ITERNEXT, rt_iternext);
990-
ASM_MOV_IMM_TO_REG((machine_uint_t)mp_const_stop_iteration, REG_TEMP1);
990+
ASM_MOV_IMM_TO_REG((machine_uint_t)MP_OBJ_NULL, REG_TEMP1);
991991
#if N_X64
992992
asm_x64_cmp_r64_with_r64(emit->as, REG_RET, REG_TEMP1);
993993
asm_x64_jcc_label(emit->as, JCC_JE, label);

py/obj.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ struct _mp_obj_type_t {
152152
mp_store_item_fun_t store_item;
153153

154154
mp_fun_1_t getiter;
155-
mp_fun_1_t iternext;
155+
mp_fun_1_t iternext; // may return MP_OBJ_NULL as an optimisation instead of raising StopIteration() (with no args)
156156

157157
// Alternatively, pointer(s) to interfaces to save space
158158
// in mp_obj_type_t at the expense of extra pointer and extra dereference
@@ -221,7 +221,6 @@ extern const mp_obj_t mp_const_false;
221221
extern const mp_obj_t mp_const_true;
222222
extern const mp_obj_t mp_const_empty_tuple;
223223
extern const mp_obj_t mp_const_ellipsis;
224-
extern const mp_obj_t mp_const_stop_iteration; // special object indicating end of iteration (not StopIteration exception!)
225224

226225
// General API for objects
227226

py/objarray.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
6464
mp_obj_t iterable = rt_getiter(initializer);
6565
mp_obj_t item;
6666
int i = 0;
67-
while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
67+
while ((item = rt_iternext(iterable)) != MP_OBJ_NULL) {
6868
if (len == 0) {
6969
array_append(array, item);
7070
} else {
@@ -212,7 +212,7 @@ STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) {
212212
if (self->cur < self->array->len) {
213213
return mp_binary_get_val(self->array->typecode, self->array->items, self->cur++);
214214
} else {
215-
return mp_const_stop_iteration;
215+
return MP_OBJ_NULL;
216216
}
217217
}
218218

py/objdict.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ mp_obj_t dict_it_iternext(mp_obj_t self_in) {
106106
if (next != NULL) {
107107
return next->key;
108108
} else {
109-
return mp_const_stop_iteration;
109+
return MP_OBJ_NULL;
110110
}
111111
}
112112

@@ -171,7 +171,7 @@ STATIC mp_obj_t dict_fromkeys(uint n_args, const mp_obj_t *args) {
171171
self = mp_obj_new_dict(MP_OBJ_SMALL_INT_VALUE(len));
172172
}
173173

174-
while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
174+
while ((next = rt_iternext(iter)) != MP_OBJ_NULL) {
175175
mp_map_lookup(&self->map, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
176176
}
177177

@@ -266,14 +266,14 @@ STATIC mp_obj_t dict_update(mp_obj_t self_in, mp_obj_t iterable) {
266266
/* TODO: check for the "keys" method */
267267
mp_obj_t iter = rt_getiter(iterable);
268268
mp_obj_t next = NULL;
269-
while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
269+
while ((next = rt_iternext(iter)) != MP_OBJ_NULL) {
270270
mp_obj_t inneriter = rt_getiter(next);
271271
mp_obj_t key = rt_iternext(inneriter);
272272
mp_obj_t value = rt_iternext(inneriter);
273273
mp_obj_t stop = rt_iternext(inneriter);
274-
if (key == mp_const_stop_iteration
275-
|| value == mp_const_stop_iteration
276-
|| stop != mp_const_stop_iteration) {
274+
if (key == MP_OBJ_NULL
275+
|| value == MP_OBJ_NULL
276+
|| stop != MP_OBJ_NULL) {
277277
nlr_jump(mp_obj_new_exception_msg(
278278
&mp_type_ValueError,
279279
"dictionary update sequence has the wrong length"));
@@ -335,7 +335,7 @@ STATIC mp_obj_t dict_view_it_iternext(mp_obj_t self_in) {
335335
return mp_const_none;
336336
}
337337
} else {
338-
return mp_const_stop_iteration;
338+
return MP_OBJ_NULL;
339339
}
340340
}
341341

@@ -364,7 +364,7 @@ STATIC void dict_view_print(void (*print)(void *env, const char *fmt, ...), void
364364
print(env, "([");
365365
mp_obj_t *self_iter = dict_view_getiter(self);
366366
mp_obj_t *next = NULL;
367-
while ((next = dict_view_it_iternext(self_iter)) != mp_const_stop_iteration) {
367+
while ((next = dict_view_it_iternext(self_iter)) != MP_OBJ_NULL) {
368368
if (!first) {
369369
print(env, ", ");
370370
}

py/objenumerate.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <stdlib.h>
12
#include <assert.h>
23

34
#include "misc.h"
@@ -37,8 +38,8 @@ STATIC mp_obj_t enumerate_iternext(mp_obj_t self_in) {
3738
assert(MP_OBJ_IS_TYPE(self_in, &enumerate_type));
3839
mp_obj_enumerate_t *self = self_in;
3940
mp_obj_t next = rt_iternext(self->iter);
40-
if (next == mp_const_stop_iteration) {
41-
return mp_const_stop_iteration;
41+
if (next == MP_OBJ_NULL) {
42+
return MP_OBJ_NULL;
4243
} else {
4344
mp_obj_t items[] = {MP_OBJ_NEW_SMALL_INT(self->cur++), next};
4445
return mp_obj_new_tuple(2, items);

py/objfilter.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <stdlib.h>
12
#include <assert.h>
23

34
#include "nlr.h"
@@ -29,7 +30,7 @@ STATIC mp_obj_t filter_iternext(mp_obj_t self_in) {
2930
assert(MP_OBJ_IS_TYPE(self_in, &filter_type));
3031
mp_obj_filter_t *self = self_in;
3132
mp_obj_t next;
32-
while ((next = rt_iternext(self->iter)) != mp_const_stop_iteration) {
33+
while ((next = rt_iternext(self->iter)) != MP_OBJ_NULL) {
3334
mp_obj_t val;
3435
if (self->fun != mp_const_none) {
3536
val = rt_call_function_n_kw(self->fun, 1, 0, &next);
@@ -40,7 +41,7 @@ STATIC mp_obj_t filter_iternext(mp_obj_t self_in) {
4041
return next;
4142
}
4243
}
43-
return mp_const_stop_iteration;
44+
return MP_OBJ_NULL;
4445
}
4546

4647
const mp_obj_type_t filter_type = {

py/objgenerator.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ mp_obj_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw
7878
mp_obj_gen_instance_t *self = self_in;
7979
if (self->ip == 0) {
8080
*ret_kind = MP_VM_RETURN_NORMAL;
81-
return mp_const_stop_iteration;
81+
return MP_OBJ_NULL;
8282
}
8383
if (self->sp == self->state - 1) {
8484
if (send_value != mp_const_none) {
@@ -122,7 +122,7 @@ STATIC mp_obj_t gen_resume_and_raise(mp_obj_t self_in, mp_obj_t send_value, mp_o
122122
case MP_VM_RETURN_NORMAL:
123123
// Optimize return w/o value in case generator is used in for loop
124124
if (ret == mp_const_none) {
125-
return mp_const_stop_iteration;
125+
return MP_OBJ_NULL;
126126
} else {
127127
nlr_jump(mp_obj_new_exception_args(&mp_type_StopIteration, 1, &ret));
128128
}
@@ -145,7 +145,7 @@ mp_obj_t gen_instance_iternext(mp_obj_t self_in) {
145145

146146
STATIC mp_obj_t gen_instance_send(mp_obj_t self_in, mp_obj_t send_value) {
147147
mp_obj_t ret = gen_resume_and_raise(self_in, send_value, MP_OBJ_NULL);
148-
if (ret == mp_const_stop_iteration) {
148+
if (ret == MP_OBJ_NULL) {
149149
nlr_jump(mp_obj_new_exception(&mp_type_StopIteration));
150150
} else {
151151
return ret;
@@ -156,7 +156,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(gen_instance_send_obj, gen_instance_send);
156156

157157
STATIC mp_obj_t gen_instance_throw(uint n_args, const mp_obj_t *args) {
158158
mp_obj_t ret = gen_resume_and_raise(args[0], mp_const_none, n_args == 2 ? args[1] : args[2]);
159-
if (ret == mp_const_stop_iteration) {
159+
if (ret == MP_OBJ_NULL) {
160160
nlr_jump(mp_obj_new_exception(&mp_type_StopIteration));
161161
} else {
162162
return ret;

py/objgetitemiter.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <stdlib.h>
12

23
#include "nlr.h"
34
#include "misc.h"
@@ -25,8 +26,8 @@ STATIC mp_obj_t it_iternext(mp_obj_t self_in) {
2526
} else {
2627
// an exception was raised
2728
if (mp_obj_get_type(nlr.ret_val) == &mp_type_StopIteration) {
28-
// return mp_const_stop_iteration instead of raising StopIteration
29-
return mp_const_stop_iteration;
29+
// return MP_OBJ_NULL instead of raising StopIteration
30+
return MP_OBJ_NULL;
3031
} else {
3132
// re-raise exception
3233
nlr_jump(nlr.ret_val);

py/objlist.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ STATIC mp_obj_t list_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp
5353
mp_obj_t iterable = rt_getiter(args[0]);
5454
mp_obj_t list = mp_obj_new_list(0, NULL);
5555
mp_obj_t item;
56-
while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
56+
while ((item = rt_iternext(iterable)) != MP_OBJ_NULL) {
5757
mp_obj_list_append(list, item);
5858
}
5959
return list;
@@ -401,7 +401,7 @@ mp_obj_t list_it_iternext(mp_obj_t self_in) {
401401
self->cur += 1;
402402
return o_out;
403403
} else {
404-
return mp_const_stop_iteration;
404+
return MP_OBJ_NULL;
405405
}
406406
}
407407

0 commit comments

Comments
 (0)