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

py: Tidy up returning NULL which should be MP_OBJ_NOT_SUPPORTED.

parent 2bb179e1
...@@ -255,8 +255,8 @@ struct _mp_obj_type_t { ...@@ -255,8 +255,8 @@ struct _mp_obj_type_t {
mp_make_new_fun_t make_new; // to make an instance of the type mp_make_new_fun_t make_new; // to make an instance of the type
mp_call_fun_t call; mp_call_fun_t call;
mp_unary_op_fun_t unary_op; // can return NULL if op not supported mp_unary_op_fun_t unary_op; // can return MP_OBJ_NOT_SUPPORTED if op not supported
mp_binary_op_fun_t binary_op; // can return NULL if op not supported mp_binary_op_fun_t binary_op; // can return MP_OBJ_NOT_SUPPORTED if op not supported
mp_load_attr_fun_t load_attr; mp_load_attr_fun_t load_attr;
mp_store_attr_fun_t store_attr; // if value is MP_OBJ_NULL, then delete that attribute mp_store_attr_fun_t store_attr; // if value is MP_OBJ_NULL, then delete that attribute
...@@ -266,7 +266,7 @@ struct _mp_obj_type_t { ...@@ -266,7 +266,7 @@ struct _mp_obj_type_t {
// can return MP_OBJ_NOT_SUPPORTED // can return MP_OBJ_NOT_SUPPORTED
mp_fun_1_t getiter; mp_fun_1_t getiter;
mp_fun_1_t iternext; // may return MP_OBJ_NULL as an optimisation instead of raising StopIteration() (with no args) mp_fun_1_t iternext; // may return MP_OBJ_STOP_ITERATION as an optimisation instead of raising StopIteration() (with no args)
mp_buffer_p_t buffer_p; mp_buffer_p_t buffer_p;
const mp_stream_p_t *stream_p; const mp_stream_p_t *stream_p;
...@@ -479,11 +479,11 @@ typedef struct _mp_obj_float_t { ...@@ -479,11 +479,11 @@ typedef struct _mp_obj_float_t {
mp_float_t value; mp_float_t value;
} mp_obj_float_t; } mp_obj_float_t;
mp_float_t mp_obj_float_get(mp_obj_t self_in); mp_float_t mp_obj_float_get(mp_obj_t self_in);
mp_obj_t mp_obj_float_binary_op(int op, mp_float_t lhs_val, mp_obj_t rhs); // can return MP_OBJ_NULL mp_obj_t mp_obj_float_binary_op(int op, mp_float_t lhs_val, mp_obj_t rhs); // can return MP_OBJ_NOT_SUPPORTED
// complex // complex
void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag); void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
mp_obj_t mp_obj_complex_binary_op(int op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in); // can return MP_OBJ_NULL mp_obj_t mp_obj_complex_binary_op(int op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in); // can return MP_OBJ_NOT_SUPPORTED
#endif #endif
// tuple // tuple
......
...@@ -465,12 +465,15 @@ STATIC void dict_view_print(void (*print)(void *env, const char *fmt, ...), void ...@@ -465,12 +465,15 @@ STATIC void dict_view_print(void (*print)(void *env, const char *fmt, ...), void
STATIC mp_obj_t dict_view_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { STATIC mp_obj_t dict_view_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
/* only supported for the 'keys' kind until sets and dicts are refactored */ /* only supported for the 'keys' kind until sets and dicts are refactored */
mp_obj_dict_view_t *o = lhs_in; mp_obj_dict_view_t *o = lhs_in;
if (o->kind != MP_DICT_VIEW_KEYS) return NULL; if (o->kind != MP_DICT_VIEW_KEYS) {
if (op != MP_BINARY_OP_IN) return NULL; return MP_OBJ_NOT_SUPPORTED;
}
if (op != MP_BINARY_OP_IN) {
return MP_OBJ_NOT_SUPPORTED;
}
return dict_binary_op(op, o->dict, rhs_in); return dict_binary_op(op, o->dict, rhs_in);
} }
STATIC const mp_obj_type_t dict_view_type = { STATIC const mp_obj_type_t dict_view_type = {
{ &mp_type_type }, { &mp_type_type },
.name = MP_QSTR_dict_view, .name = MP_QSTR_dict_view,
......
...@@ -97,7 +97,7 @@ STATIC mp_obj_t float_unary_op(int op, mp_obj_t o_in) { ...@@ -97,7 +97,7 @@ STATIC mp_obj_t float_unary_op(int op, mp_obj_t o_in) {
case MP_UNARY_OP_BOOL: return MP_BOOL(o->value != 0); case MP_UNARY_OP_BOOL: return MP_BOOL(o->value != 0);
case MP_UNARY_OP_POSITIVE: return o_in; case MP_UNARY_OP_POSITIVE: return o_in;
case MP_UNARY_OP_NEGATIVE: return mp_obj_new_float(-o->value); case MP_UNARY_OP_NEGATIVE: return mp_obj_new_float(-o->value);
default: return NULL; // op not supported default: return MP_OBJ_NOT_SUPPORTED;
} }
} }
......
...@@ -58,7 +58,7 @@ STATIC mp_obj_t fun_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { ...@@ -58,7 +58,7 @@ STATIC mp_obj_t fun_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
// we don't even need to check for 2nd arg type. // we don't even need to check for 2nd arg type.
return MP_BOOL(lhs_in == rhs_in); return MP_BOOL(lhs_in == rhs_in);
} }
return NULL; return MP_OBJ_NOT_SUPPORTED;
} }
STATIC mp_obj_t fun_native_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) { STATIC mp_obj_t fun_native_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
......
...@@ -90,7 +90,7 @@ mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in) { ...@@ -90,7 +90,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_POSITIVE: return o_in;
case MP_UNARY_OP_NEGATIVE: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_neg_inpl(&o2->mpz, &o->mpz); return o2; } case MP_UNARY_OP_NEGATIVE: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_neg_inpl(&o2->mpz, &o->mpz); return o2; }
case MP_UNARY_OP_INVERT: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_not_inpl(&o2->mpz, &o->mpz); return o2; } case MP_UNARY_OP_INVERT: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_not_inpl(&o2->mpz, &o->mpz); return o2; }
default: return NULL; // op not supported default: return MP_OBJ_NOT_SUPPORTED;
} }
} }
......
...@@ -87,7 +87,6 @@ STATIC mp_obj_t list_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp ...@@ -87,7 +87,6 @@ STATIC mp_obj_t list_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp
default: default:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "list takes at most 1 argument, %d given", n_args)); nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "list takes at most 1 argument, %d given", n_args));
} }
return NULL;
} }
// Don't pass MP_BINARY_OP_NOT_EQUAL here // Don't pass MP_BINARY_OP_NOT_EQUAL here
...@@ -116,7 +115,7 @@ STATIC mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { ...@@ -116,7 +115,7 @@ STATIC mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
switch (op) { switch (op) {
case MP_BINARY_OP_ADD: { case MP_BINARY_OP_ADD: {
if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) { if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) {
return NULL; return MP_OBJ_NOT_SUPPORTED;
} }
mp_obj_list_t *p = rhs; mp_obj_list_t *p = rhs;
mp_obj_list_t *s = list_new(o->len + p->len); mp_obj_list_t *s = list_new(o->len + p->len);
...@@ -125,7 +124,7 @@ STATIC mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { ...@@ -125,7 +124,7 @@ STATIC mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
} }
case MP_BINARY_OP_INPLACE_ADD: { case MP_BINARY_OP_INPLACE_ADD: {
if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) { if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) {
return NULL; return MP_OBJ_NOT_SUPPORTED;
} }
list_extend(lhs, rhs); list_extend(lhs, rhs);
return o; return o;
...@@ -133,7 +132,7 @@ STATIC mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { ...@@ -133,7 +132,7 @@ STATIC mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
case MP_BINARY_OP_MULTIPLY: { case MP_BINARY_OP_MULTIPLY: {
machine_int_t n; machine_int_t n;
if (!mp_obj_get_int_maybe(rhs, &n)) { if (!mp_obj_get_int_maybe(rhs, &n)) {
return NULL; return MP_OBJ_NOT_SUPPORTED;
} }
mp_obj_list_t *s = list_new(o->len * n); mp_obj_list_t *s = list_new(o->len * n);
mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items); mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items);
......
...@@ -412,41 +412,39 @@ STATIC mp_obj_t set_unary_op(int op, mp_obj_t self_in) { ...@@ -412,41 +412,39 @@ STATIC mp_obj_t set_unary_op(int op, mp_obj_t self_in) {
STATIC mp_obj_t set_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { STATIC mp_obj_t set_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
mp_obj_t args[] = {lhs, rhs}; mp_obj_t args[] = {lhs, rhs};
switch (op) { switch (op) {
case MP_BINARY_OP_OR: case MP_BINARY_OP_OR:
return set_union(lhs, rhs); return set_union(lhs, rhs);
case MP_BINARY_OP_XOR: case MP_BINARY_OP_XOR:
return set_symmetric_difference(lhs, rhs); return set_symmetric_difference(lhs, rhs);
case MP_BINARY_OP_AND: case MP_BINARY_OP_AND:
return set_intersect(lhs, rhs); return set_intersect(lhs, rhs);
case MP_BINARY_OP_SUBTRACT: case MP_BINARY_OP_SUBTRACT:
return set_diff(2, args); return set_diff(2, args);
case MP_BINARY_OP_INPLACE_OR: case MP_BINARY_OP_INPLACE_OR:
return set_union(lhs, rhs); return set_union(lhs, rhs);
case MP_BINARY_OP_INPLACE_XOR: case MP_BINARY_OP_INPLACE_XOR:
return set_symmetric_difference(lhs, rhs); return set_symmetric_difference(lhs, rhs);
case MP_BINARY_OP_INPLACE_AND: case MP_BINARY_OP_INPLACE_AND:
return set_intersect(lhs, rhs); return set_intersect(lhs, rhs);
case MP_BINARY_OP_INPLACE_SUBTRACT: case MP_BINARY_OP_INPLACE_SUBTRACT:
return set_diff(2, args); return set_diff(2, args);
case MP_BINARY_OP_LESS: case MP_BINARY_OP_LESS:
return set_issubset_proper(lhs, rhs); return set_issubset_proper(lhs, rhs);
case MP_BINARY_OP_MORE: case MP_BINARY_OP_MORE:
return set_issuperset_proper(lhs, rhs); return set_issuperset_proper(lhs, rhs);
case MP_BINARY_OP_EQUAL: case MP_BINARY_OP_EQUAL:
return set_equal(lhs, rhs); return set_equal(lhs, rhs);
case MP_BINARY_OP_LESS_EQUAL: case MP_BINARY_OP_LESS_EQUAL:
return set_issubset(lhs, rhs); return set_issubset(lhs, rhs);
case MP_BINARY_OP_MORE_EQUAL: case MP_BINARY_OP_MORE_EQUAL:
return set_issuperset(lhs, rhs); return set_issuperset(lhs, rhs);
case MP_BINARY_OP_IN: case MP_BINARY_OP_IN: {
{ mp_obj_set_t *o = lhs;
mp_obj_set_t *o = lhs; mp_obj_t elem = mp_set_lookup(&o->set, rhs, MP_MAP_LOOKUP);
mp_obj_t elem = mp_set_lookup(&o->set, rhs, MP_MAP_LOOKUP); return MP_BOOL(elem != NULL);
return MP_BOOL(elem != NULL); }
} default:
default: return MP_OBJ_NOT_SUPPORTED;
// op not supported
return NULL;
} }
} }
......
...@@ -287,10 +287,9 @@ STATIC mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { ...@@ -287,10 +287,9 @@ STATIC mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
} }
break; break;
case MP_BINARY_OP_MULTIPLY: case MP_BINARY_OP_MULTIPLY: {
{
if (!MP_OBJ_IS_SMALL_INT(rhs_in)) { if (!MP_OBJ_IS_SMALL_INT(rhs_in)) {
return NULL; return MP_OBJ_NOT_SUPPORTED;
} }
int n = MP_OBJ_SMALL_INT_VALUE(rhs_in); int n = MP_OBJ_SMALL_INT_VALUE(rhs_in);
byte *data; byte *data;
......
...@@ -121,20 +121,18 @@ mp_obj_t tuple_unary_op(int op, mp_obj_t self_in) { ...@@ -121,20 +121,18 @@ mp_obj_t tuple_unary_op(int op, mp_obj_t self_in) {
mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
mp_obj_tuple_t *o = lhs; mp_obj_tuple_t *o = lhs;
switch (op) { switch (op) {
case MP_BINARY_OP_ADD: case MP_BINARY_OP_ADD: {
{
if (!mp_obj_is_subclass_fast(mp_obj_get_type(rhs), (mp_obj_t)&mp_type_tuple)) { if (!mp_obj_is_subclass_fast(mp_obj_get_type(rhs), (mp_obj_t)&mp_type_tuple)) {
return NULL; return MP_OBJ_NOT_SUPPORTED;
} }
mp_obj_tuple_t *p = rhs; mp_obj_tuple_t *p = rhs;
mp_obj_tuple_t *s = mp_obj_new_tuple(o->len + p->len, NULL); mp_obj_tuple_t *s = mp_obj_new_tuple(o->len + p->len, NULL);
m_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t); m_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t);
return s; return s;
} }
case MP_BINARY_OP_MULTIPLY: case MP_BINARY_OP_MULTIPLY: {
{
if (!MP_OBJ_IS_SMALL_INT(rhs)) { if (!MP_OBJ_IS_SMALL_INT(rhs)) {
return NULL; return MP_OBJ_NOT_SUPPORTED;
} }
int n = MP_OBJ_SMALL_INT_VALUE(rhs); int n = MP_OBJ_SMALL_INT_VALUE(rhs);
mp_obj_tuple_t *s = mp_obj_new_tuple(o->len * n, NULL); mp_obj_tuple_t *s = mp_obj_new_tuple(o->len * n, NULL);
...@@ -150,7 +148,7 @@ mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { ...@@ -150,7 +148,7 @@ mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
default: default:
// op not supported // op not supported
return NULL; return MP_OBJ_NOT_SUPPORTED;
} }
} }
......
...@@ -596,8 +596,10 @@ STATIC mp_obj_t type_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { ...@@ -596,8 +596,10 @@ STATIC mp_obj_t type_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
// Types can be equal only if it's the same type structure, // Types can be equal only if it's the same type structure,
// we don't even need to check for 2nd arg type. // we don't even need to check for 2nd arg type.
return MP_BOOL(lhs_in == rhs_in); return MP_BOOL(lhs_in == rhs_in);
default:
return MP_OBJ_NOT_SUPPORTED;
} }
return NULL;
} }
const mp_obj_type_t mp_type_type = { const mp_obj_type_t mp_type_type = {
......
Supports Markdown
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