Commit ea8d06c3 authored by Damien George's avatar Damien George
Browse files

py: Add MP_OBJ_STOP_ITERATION and make good use of it.

Also make consistent use of MP_OBJ_NOT_SUPPORTED and MP_OBJ_NULL.
This helps a lot in debugging and understanding of function API.
parent 1e935d86
......@@ -103,7 +103,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_abs_obj, mp_builtin_abs);
STATIC mp_obj_t mp_builtin_all(mp_obj_t o_in) {
mp_obj_t iterable = mp_getiter(o_in);
mp_obj_t item;
while ((item = mp_iternext(iterable)) != MP_OBJ_NULL) {
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
if (!mp_obj_is_true(item)) {
return mp_const_false;
}
......@@ -116,7 +116,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_all_obj, mp_builtin_all);
STATIC mp_obj_t mp_builtin_any(mp_obj_t o_in) {
mp_obj_t iterable = mp_getiter(o_in);
mp_obj_t item;
while ((item = mp_iternext(iterable)) != MP_OBJ_NULL) {
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
if (mp_obj_is_true(item)) {
return mp_const_true;
}
......@@ -244,7 +244,7 @@ STATIC mp_obj_t mp_builtin_max(uint n_args, const mp_obj_t *args) {
mp_obj_t iterable = mp_getiter(args[0]);
mp_obj_t max_obj = NULL;
mp_obj_t item;
while ((item = mp_iternext(iterable)) != MP_OBJ_NULL) {
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
if (max_obj == NULL || mp_binary_op(MP_BINARY_OP_LESS, max_obj, item)) {
max_obj = item;
}
......@@ -273,7 +273,7 @@ STATIC mp_obj_t mp_builtin_min(uint n_args, const mp_obj_t *args) {
mp_obj_t iterable = mp_getiter(args[0]);
mp_obj_t min_obj = NULL;
mp_obj_t item;
while ((item = mp_iternext(iterable)) != MP_OBJ_NULL) {
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
if (min_obj == NULL || mp_binary_op(MP_BINARY_OP_LESS, item, min_obj)) {
min_obj = item;
}
......@@ -298,7 +298,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_min_obj, 1, mp_builtin_min);
STATIC mp_obj_t mp_builtin_next(mp_obj_t o) {
mp_obj_t ret = mp_iternext_allow_raise(o);
if (ret == MP_OBJ_NULL) {
if (ret == MP_OBJ_STOP_ITERATION) {
nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
} else {
return ret;
......@@ -381,7 +381,7 @@ STATIC mp_obj_t mp_builtin_sum(uint n_args, const mp_obj_t *args) {
}
mp_obj_t iterable = mp_getiter(args[0]);
mp_obj_t item;
while ((item = mp_iternext(iterable)) != MP_OBJ_NULL) {
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
value = mp_binary_op(MP_BINARY_OP_ADD, value, item);
}
return value;
......
......@@ -90,7 +90,7 @@ int mp_obj_is_true(mp_obj_t arg) {
mp_obj_type_t *type = mp_obj_get_type(arg);
if (type->unary_op != NULL) {
mp_obj_t result = type->unary_op(MP_UNARY_OP_BOOL, arg);
if (result != MP_OBJ_NULL) {
if (result != MP_OBJ_NOT_SUPPORTED) {
return result == mp_const_true;
}
}
......@@ -180,7 +180,7 @@ bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
mp_obj_type_t *type = mp_obj_get_type(o1);
if (type->binary_op != NULL) {
mp_obj_t r = type->binary_op(MP_BINARY_OP_EQUAL, o1, o2);
if (r != MP_OBJ_NULL) {
if (r != MP_OBJ_NOT_SUPPORTED) {
return r == mp_const_true ? true : false;
}
}
......
// A Micro Python object is a machine word having the following form:
// - xxxx...xxx1 : a small int, bits 1 and above are the value
// - xxxx...xx10 : a qstr, bits 2 and above are the value
// - xxxx...xx00 : a pointer to an mp_obj_base_t (unless a fake object)
// All Micro Python objects are at least this type
// It must be of pointer size
......@@ -10,7 +15,7 @@ typedef machine_const_ptr_t mp_const_obj_t;
typedef machine_int_t mp_small_int_t;
// Anything that wants to be a Micro Python object must have
// mp_obj_base_t as its first member (except NULL and small ints)
// mp_obj_base_t as its first member (except small ints and qstrs)
struct _mp_obj_type_t;
struct _mp_obj_base_t {
......@@ -18,24 +23,31 @@ struct _mp_obj_base_t {
};
typedef struct _mp_obj_base_t mp_obj_base_t;
// The NULL object is used to indicate the absence of an object
// It *cannot* be used when an mp_obj_t is expected, except where explicitly allowed
#define MP_OBJ_NULL ((mp_obj_t)0)
// The SENTINEL object is used for various internal purposes where one needs
// an object which is unique from all other objects, including MP_OBJ_NULL.
#define MP_OBJ_SENTINEL ((mp_obj_t)8)
// The NOT_SUPPORTED object is a return value that indicates an unsupported operation.
#define MP_OBJ_NOT_SUPPORTED ((mp_obj_t)16)
// These fake objects are used to indicate certain things in arguments or return
// values, and should only be used when explicitly allowed.
//
// - MP_OBJ_NULL : used to indicate the absence of an object.
// - MP_OBJ_NOT_SUPPORTED : a return value that indicates an unsupported operation.
// - MP_OBJ_STOP_ITERATION : used instead of throwing a StopIteration, for efficiency.
// - MP_OBJ_SENTINEL : used for various internal purposes where one needs
// an object which is unique from all other objects, including MP_OBJ_NULL.
//
// For debugging purposes they are all different. For non-debug mode, we alias
// as many as we can to MP_OBJ_NULL because it's cheaper to load/compare 0.
#if NDEBUG
#define MP_OBJ_NULL ((mp_obj_t)0)
#define MP_OBJ_NOT_SUPPORTED ((mp_obj_t)0)
#define MP_OBJ_STOP_ITERATION ((mp_obj_t)0)
#define MP_OBJ_SENTINEL ((mp_obj_t)4)
#else
#define MP_OBJ_NULL ((mp_obj_t)0)
#define MP_OBJ_NOT_SUPPORTED ((mp_obj_t)4)
#define MP_OBJ_STOP_ITERATION ((mp_obj_t)8)
#define MP_OBJ_SENTINEL ((mp_obj_t)12)
#endif
// These macros check for small int, qstr or object, and access small int and qstr values
// - xxxx...xxx1: a small int, bits 1 and above are the value
// - xxxx...xx10: a qstr, bits 2 and above are the value
// - xxxx...xx00: a pointer to an mp_obj_base_t
// In SMALL_INT, next-to-highest bits is used as sign, so both must match for value in range
#define MP_SMALL_INT_MIN ((mp_small_int_t)(((machine_int_t)WORD_MSBIT_HIGH) >> 1))
......
......@@ -63,7 +63,7 @@ STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
mp_obj_t iterable = mp_getiter(initializer);
mp_obj_t item;
int i = 0;
while ((item = mp_iternext(iterable)) != MP_OBJ_NULL) {
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
if (len == 0) {
array_append(array, item);
} else {
......@@ -108,7 +108,7 @@ STATIC mp_obj_t array_unary_op(int op, mp_obj_t o_in) {
switch (op) {
case MP_UNARY_OP_BOOL: return MP_BOOL(o->len != 0);
case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(o->len);
default: return MP_OBJ_NULL; // op not supported
default: return MP_OBJ_NOT_SUPPORTED;
}
}
......@@ -227,7 +227,7 @@ STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) {
if (self->cur < self->array->len) {
return mp_binary_get_val_array(self->array->typecode, self->array->items, self->cur++);
} else {
return MP_OBJ_NULL;
return MP_OBJ_STOP_ITERATION;
}
}
......
......@@ -48,8 +48,7 @@ STATIC mp_obj_t bool_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
if (MP_BINARY_OP_OR <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
return mp_binary_op(op, MP_OBJ_NEW_SMALL_INT((machine_int_t)mp_obj_is_true(lhs_in)), rhs_in);
}
// operation not supported
return MP_OBJ_NULL;
return MP_OBJ_NOT_SUPPORTED;
}
const mp_obj_type_t mp_type_bool = {
......
......@@ -98,7 +98,7 @@ STATIC mp_obj_t complex_unary_op(int op, mp_obj_t o_in) {
case MP_UNARY_OP_BOOL: return MP_BOOL(o->real != 0 || o->imag != 0);
case MP_UNARY_OP_POSITIVE: return o_in;
case MP_UNARY_OP_NEGATIVE: return mp_obj_new_complex(-o->real, -o->imag);
default: return MP_OBJ_NULL; // op not supported
default: return MP_OBJ_NOT_SUPPORTED;
}
}
......@@ -208,7 +208,7 @@ mp_obj_t mp_obj_complex_binary_op(int op, mp_float_t lhs_real, mp_float_t lhs_im
case MP_BINARY_OP_EQUAL: return MP_BOOL(lhs_real == rhs_real && lhs_imag == rhs_imag);
default:
return MP_OBJ_NULL; // op not supported
return MP_OBJ_NOT_SUPPORTED;
}
return mp_obj_new_complex(lhs_real, lhs_imag);
}
......
......@@ -22,7 +22,7 @@ STATIC void dict_print(void (*print)(void *env, const char *fmt, ...), void *env
print(env, "{");
mp_obj_t *dict_iter = mp_obj_new_dict_iterator(self, 0);
mp_map_elem_t *next = NULL;
while ((next = dict_it_iternext_elem(dict_iter)) != NULL) {
while ((next = dict_it_iternext_elem(dict_iter)) != MP_OBJ_STOP_ITERATION) {
if (!first) {
print(env, ", ");
}
......@@ -52,7 +52,7 @@ STATIC mp_obj_t dict_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp
mp_obj_t dict = mp_obj_new_dict(0);
// TODO: support arbitrary seq as a pair
mp_obj_t item;
while ((item = mp_iternext(iterable)) != MP_OBJ_NULL) {
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
mp_obj_t *sub_items;
mp_obj_get_array_fixed_n(item, 2, &sub_items);
mp_obj_dict_store(dict, sub_items[0], sub_items[1]);
......@@ -77,7 +77,7 @@ STATIC mp_obj_t dict_unary_op(int op, mp_obj_t self_in) {
switch (op) {
case MP_UNARY_OP_BOOL: return MP_BOOL(self->map.used != 0);
case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT((machine_int_t)self->map.used);
default: return MP_OBJ_NULL; // op not supported for None
default: return MP_OBJ_NOT_SUPPORTED;
}
}
......@@ -114,7 +114,7 @@ STATIC mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
}
default:
// op not supported
return NULL;
return MP_OBJ_NOT_SUPPORTED;
}
}
......@@ -160,16 +160,16 @@ STATIC mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in) {
}
}
return NULL;
return MP_OBJ_STOP_ITERATION;
}
mp_obj_t dict_it_iternext(mp_obj_t self_in) {
mp_map_elem_t *next = dict_it_iternext_elem(self_in);
if (next != NULL) {
if (next != MP_OBJ_STOP_ITERATION) {
return next->key;
} else {
return MP_OBJ_NULL;
return MP_OBJ_STOP_ITERATION;
}
}
......@@ -237,7 +237,7 @@ STATIC mp_obj_t dict_fromkeys(uint n_args, const mp_obj_t *args) {
self = mp_obj_new_dict(MP_OBJ_SMALL_INT_VALUE(len));
}
while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
mp_map_lookup(&self->map, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
}
......@@ -328,14 +328,14 @@ STATIC mp_obj_t dict_update(mp_obj_t self_in, mp_obj_t iterable) {
/* TODO: check for the "keys" method */
mp_obj_t iter = mp_getiter(iterable);
mp_obj_t next = NULL;
while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
mp_obj_t inneriter = mp_getiter(next);
mp_obj_t key = mp_iternext(inneriter);
mp_obj_t value = mp_iternext(inneriter);
mp_obj_t stop = mp_iternext(inneriter);
if (key == MP_OBJ_NULL
|| value == MP_OBJ_NULL
|| stop != MP_OBJ_NULL) {
if (key == MP_OBJ_STOP_ITERATION
|| value == MP_OBJ_STOP_ITERATION
|| stop != MP_OBJ_STOP_ITERATION) {
nlr_raise(mp_obj_new_exception_msg(
&mp_type_ValueError,
"dictionary update sequence has the wrong length"));
......@@ -381,7 +381,7 @@ STATIC mp_obj_t dict_view_it_iternext(mp_obj_t self_in) {
mp_obj_dict_view_it_t *self = self_in;
mp_map_elem_t *next = dict_it_iternext_elem(self->iter);
if (next != NULL) {
if (next != MP_OBJ_STOP_ITERATION) {
switch (self->kind) {
case MP_DICT_VIEW_ITEMS:
{
......@@ -397,7 +397,7 @@ STATIC mp_obj_t dict_view_it_iternext(mp_obj_t self_in) {
return mp_const_none;
}
} else {
return MP_OBJ_NULL;
return MP_OBJ_STOP_ITERATION;
}
}
......@@ -426,7 +426,7 @@ STATIC void dict_view_print(void (*print)(void *env, const char *fmt, ...), void
print(env, "([");
mp_obj_t *self_iter = dict_view_getiter(self);
mp_obj_t *next = NULL;
while ((next = dict_view_it_iternext(self_iter)) != MP_OBJ_NULL) {
while ((next = dict_view_it_iternext(self_iter)) != MP_OBJ_STOP_ITERATION) {
if (!first) {
print(env, ", ");
}
......
......@@ -38,8 +38,8 @@ STATIC mp_obj_t enumerate_iternext(mp_obj_t self_in) {
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_enumerate));
mp_obj_enumerate_t *self = self_in;
mp_obj_t next = mp_iternext(self->iter);
if (next == MP_OBJ_NULL) {
return MP_OBJ_NULL;
if (next == MP_OBJ_STOP_ITERATION) {
return MP_OBJ_STOP_ITERATION;
} else {
mp_obj_t items[] = {MP_OBJ_NEW_SMALL_INT(self->cur++), next};
return mp_obj_new_tuple(2, items);
......
......@@ -30,7 +30,7 @@ STATIC mp_obj_t filter_iternext(mp_obj_t self_in) {
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_filter));
mp_obj_filter_t *self = self_in;
mp_obj_t next;
while ((next = mp_iternext(self->iter)) != MP_OBJ_NULL) {
while ((next = mp_iternext(self->iter)) != MP_OBJ_STOP_ITERATION) {
mp_obj_t val;
if (self->fun != mp_const_none) {
val = mp_call_function_n_kw(self->fun, 1, 0, &next);
......@@ -41,7 +41,7 @@ STATIC mp_obj_t filter_iternext(mp_obj_t self_in) {
return next;
}
}
return MP_OBJ_NULL;
return MP_OBJ_STOP_ITERATION;
}
const mp_obj_type_t mp_type_filter = {
......
......@@ -140,7 +140,7 @@ mp_obj_t mp_obj_float_binary_op(int op, mp_float_t lhs_val, mp_obj_t rhs_in) {
case MP_BINARY_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val);
default:
return MP_OBJ_NULL; // op not supported
return MP_OBJ_NOT_SUPPORTED;
}
return mp_obj_new_float(lhs_val);
}
......
......@@ -79,7 +79,7 @@ mp_vm_return_kind_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_gen_instance));
mp_obj_gen_instance_t *self = self_in;
if (self->ip == 0) {
*ret_val = MP_OBJ_NULL;
*ret_val = MP_OBJ_STOP_ITERATION;
return MP_VM_RETURN_NORMAL;
}
if (self->sp == self->state - 1) {
......@@ -130,8 +130,8 @@ STATIC mp_obj_t gen_resume_and_raise(mp_obj_t self_in, mp_obj_t send_value, mp_o
switch (mp_obj_gen_resume(self_in, send_value, throw_value, &ret)) {
case MP_VM_RETURN_NORMAL:
// Optimize return w/o value in case generator is used in for loop
if (ret == mp_const_none || ret == MP_OBJ_NULL) {
return MP_OBJ_NULL;
if (ret == mp_const_none || ret == MP_OBJ_STOP_ITERATION) {
return MP_OBJ_STOP_ITERATION;
} else {
nlr_raise(mp_obj_new_exception_args(&mp_type_StopIteration, 1, &ret));
}
......@@ -143,11 +143,11 @@ STATIC mp_obj_t gen_resume_and_raise(mp_obj_t self_in, mp_obj_t send_value, mp_o
return ret;
case MP_VM_RETURN_EXCEPTION:
// TODO: Optimization of returning MP_OBJ_NULL is really part
// TODO: Optimization of returning MP_OBJ_STOP_ITERATION is really part
// of mp_iternext() protocol, but this function is called by other methods
// too, which may not handled MP_OBJ_NULL.
// too, which may not handled MP_OBJ_STOP_ITERATION.
if (mp_obj_is_subclass_fast(mp_obj_get_type(ret), &mp_type_StopIteration)) {
return MP_OBJ_NULL;
return MP_OBJ_STOP_ITERATION;
} else {
nlr_raise(ret);
}
......@@ -164,7 +164,7 @@ mp_obj_t gen_instance_iternext(mp_obj_t self_in) {
STATIC mp_obj_t gen_instance_send(mp_obj_t self_in, mp_obj_t send_value) {
mp_obj_t ret = gen_resume_and_raise(self_in, send_value, MP_OBJ_NULL);
if (ret == MP_OBJ_NULL) {
if (ret == MP_OBJ_STOP_ITERATION) {
nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
} else {
return ret;
......@@ -179,7 +179,7 @@ STATIC mp_obj_t gen_instance_throw(uint n_args, const mp_obj_t *args) {
exc = mp_make_raise_obj(exc);
mp_obj_t ret = gen_resume_and_raise(args[0], mp_const_none, exc);
if (ret == MP_OBJ_NULL) {
if (ret == MP_OBJ_STOP_ITERATION) {
nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
} else {
return ret;
......
......@@ -26,8 +26,8 @@ STATIC mp_obj_t it_iternext(mp_obj_t self_in) {
} else {
// an exception was raised
if (mp_obj_get_type(nlr.ret_val) == &mp_type_StopIteration) {
// return MP_OBJ_NULL instead of raising StopIteration
return MP_OBJ_NULL;
// return MP_OBJ_STOP_ITERATION instead of raising StopIteration
return MP_OBJ_STOP_ITERATION;
} else {
// re-raise exception
nlr_raise(nlr.ret_val);
......
......@@ -193,7 +193,7 @@ bool mp_obj_int_is_positive(mp_obj_t self_in) {
// This is called for operations on SMALL_INT that are not handled by mp_unary_op
mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in) {
return MP_OBJ_NULL;
return MP_OBJ_NOT_SUPPORTED;
}
// This is called for operations on SMALL_INT that are not handled by mp_binary_op
......@@ -262,7 +262,7 @@ mp_obj_t mp_obj_int_binary_op_extra_cases(int op, mp_obj_t lhs_in, mp_obj_t rhs_
return mp_binary_op(op, rhs_in, lhs_in);
}
}
return MP_OBJ_NULL;
return MP_OBJ_NOT_SUPPORTED;
}
// this is a classmethod
......
......@@ -37,7 +37,7 @@ mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in) {
case MP_UNARY_OP_POSITIVE: return o_in;
case MP_UNARY_OP_NEGATIVE: return mp_obj_new_int_from_ll(-o->val);
case MP_UNARY_OP_INVERT: return mp_obj_new_int_from_ll(~o->val);
default: return NULL; // op not supported
default: return MP_OBJ_NOT_SUPPORTED;
}
}
......@@ -50,7 +50,7 @@ mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
} else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
lhs_val = ((mp_obj_int_t*)lhs_in)->val;
} else {
return MP_OBJ_NULL;
return MP_OBJ_NOT_SUPPORTED;
}
if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
......@@ -108,8 +108,7 @@ mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
return MP_BOOL(lhs_val == rhs_val);
default:
// op not supported
return MP_OBJ_NULL;
return MP_OBJ_NOT_SUPPORTED;
}
}
......
......@@ -82,7 +82,7 @@ mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
zlhs = &((mp_obj_int_t*)lhs_in)->mpz;
} else {
// unsupported type
return MP_OBJ_NULL;
return MP_OBJ_NOT_SUPPORTED;
}
// if rhs is small int, then lhs was not (otherwise mp_binary_op handles it)
......@@ -187,7 +187,7 @@ mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
break;
default:
return MP_OBJ_NULL;
return MP_OBJ_NOT_SUPPORTED;
}
return res;
......@@ -207,7 +207,7 @@ mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
return MP_BOOL(cmp == 0);
default:
return MP_OBJ_NULL;
return MP_OBJ_NOT_SUPPORTED;
}
}
}
......
......@@ -36,7 +36,7 @@ STATIC void list_print(void (*print)(void *env, const char *fmt, ...), void *env
STATIC mp_obj_t list_extend_from_iter(mp_obj_t list, mp_obj_t iterable) {
mp_obj_t iter = mp_getiter(iterable);
mp_obj_t item;
while ((item = mp_iternext(iter)) != MP_OBJ_NULL) {
while ((item = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
mp_obj_list_append(list, item);
}
return list;
......@@ -81,7 +81,7 @@ STATIC mp_obj_t list_unary_op(int op, mp_obj_t self_in) {
switch (op) {
case MP_UNARY_OP_BOOL: return MP_BOOL(self->len != 0);
case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->len);
default: return MP_OBJ_NULL; // op not supported for None
default: return MP_OBJ_NOT_SUPPORTED;
}
}
......@@ -121,8 +121,7 @@ STATIC mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
return MP_BOOL(list_cmp_helper(op, lhs, rhs));
default:
// op not supported
return NULL;
return MP_OBJ_NOT_SUPPORTED;
}
}
......@@ -423,7 +422,7 @@ mp_obj_t list_it_iternext(mp_obj_t self_in) {
self->cur += 1;
return o_out;
} else {
return MP_OBJ_NULL;
return MP_OBJ_STOP_ITERATION;
}
}
......
......@@ -41,9 +41,9 @@ STATIC mp_obj_t map_iternext(mp_obj_t self_in) {
for (int i = 0; i < self->n_iters; i++) {
mp_obj_t next = mp_iternext(self->iters[i]);
if (next == MP_OBJ_NULL) {
if (next == MP_OBJ_STOP_ITERATION) {
m_del(mp_obj_t, nextses, self->n_iters);
return MP_OBJ_NULL;
return MP_OBJ_STOP_ITERATION;
}
nextses[i] = next;
}
......
......@@ -18,7 +18,7 @@ STATIC void none_print(void (*print)(void *env, const char *fmt, ...), void *env
STATIC mp_obj_t none_unary_op(int op, mp_obj_t o_in) {
switch (op) {
case MP_UNARY_OP_BOOL: return mp_const_false;
default: return MP_OBJ_NULL; // op not supported for None
default: return MP_OBJ_NOT_SUPPORTED;
}
}
......
......@@ -25,7 +25,7 @@ STATIC mp_obj_t range_it_iternext(mp_obj_t o_in) {
o->cur += o->step;
return o_out;
} else {
return MP_OBJ_NULL;
return MP_OBJ_STOP_ITERATION;
}
}
......
......@@ -59,7 +59,7 @@ STATIC mp_obj_t set_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_
mp_obj_t set = mp_obj_new_set(0, NULL);
mp_obj_t iterable = mp_getiter(args[0]);
mp_obj_t item;
while ((item = mp_iternext(iterable)) != MP_OBJ_NULL) {
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
mp_obj_set_store(set, item);
}
return set;
......@@ -90,7 +90,7 @@ STATIC mp_obj_t set_it_iternext(mp_obj_t self_in) {
}
}
return MP_OBJ_NULL;
return MP_OBJ_STOP_ITERATION;
}
STATIC mp_obj_t set_getiter(mp_obj_t set_in) {
......@@ -163,7 +163,7 @@ STATIC mp_obj_t set_diff_int(int n_args, const mp_obj_t *args, bool update) {
} else {
mp_obj_t iter = mp_getiter(other);
mp_obj_t next;
while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
set_discard(self, next);
}
}
......@@ -194,7 +194,7 @@ STATIC mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update)
mp_obj_t iter = mp_getiter(other);
mp_obj_t next;
while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
set_add(out, next);
}
......@@ -226,7 +226,7 @@ STATIC mp_obj_t set_isdisjoint(mp_obj_t self_in, mp_obj_t other) {
mp_obj_t iter = mp_getiter(other);
mp_obj_t next;
while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
return mp_const_false;
}
......@@ -259,7 +259,7 @@ STATIC mp_obj_t set_issubset_internal(mp_obj_t self_in, mp_obj_t other_in, bool
} else {
mp_obj_t iter = set_getiter(self);
mp_obj_t next;
while ((next = set_it_iternext(iter)) != MP_OBJ_NULL) {
while ((next = set_it_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
if (!mp_set_lookup(&other->set, next, MP_MAP_LOOKUP)) {
out = false;
break;
......@@ -331,7 +331,7 @@ STATIC mp_obj_t set_symmetric_difference_update(mp_obj_t self_in, mp_obj_t other
mp_obj_set_t *self = self_in;
mp_obj_t iter = mp_getiter(other_in);
mp_obj_t next;
while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_REMOVE_IF_FOUND | MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
}
return mp_const_none;
......@@ -349,7 +349,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_obj, set_symmetric_dif
STATIC void set_update_int(mp_obj_set_t *self, mp_obj_t other_in) {
mp_obj_t iter = mp_getiter(other_in);
mp_obj_t next;
while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
}
}
......@@ -379,7 +379,7 @@ STATIC mp_obj_t set_unary_op(int op, mp_obj_t self_in) {
switch (op) {
case MP_UNARY_OP_BOOL: return MP_BOOL(self->set.used != 0);
case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT((machine_int_t)self->set.used);
default: return MP_OBJ_NULL; // op not supported for None
default: return MP_OBJ_NOT_SUPPORTED;
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment