Skip to content

Commit 4e8dc8c

Browse files
committed
py: Add unary op not for NoneType, bool, tuple, list, dict; fix for int.
1 parent addf60b commit 4e8dc8c

6 files changed

Lines changed: 50 additions & 2 deletions

File tree

py/objdict.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ static mp_obj_t dict_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp
4343
return rt_build_map(0);
4444
}
4545

46+
static mp_obj_t dict_unary_op(int op, mp_obj_t self_in) {
47+
mp_obj_dict_t *self = self_in;
48+
switch (op) {
49+
case RT_UNARY_OP_NOT: if (self->map.used == 0) { return mp_const_true; } else { return mp_const_false; }
50+
default: return MP_OBJ_NULL; // op not supported for None
51+
}
52+
}
53+
4654
static mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
4755
mp_obj_dict_t *o = lhs_in;
4856
switch (op) {
@@ -436,6 +444,7 @@ const mp_obj_type_t dict_type = {
436444
"dict",
437445
.print = dict_print,
438446
.make_new = dict_make_new,
447+
.unary_op = dict_unary_op,
439448
.binary_op = dict_binary_op,
440449
.getiter = dict_getiter,
441450
.methods = dict_type_methods,

py/objlist.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ static bool list_cmp_helper(int op, mp_obj_t self_in, mp_obj_t another_in) {
119119
return true;
120120
}
121121

122+
static mp_obj_t list_unary_op(int op, mp_obj_t self_in) {
123+
mp_obj_list_t *self = self_in;
124+
switch (op) {
125+
case RT_UNARY_OP_NOT: if (self->len == 0) { return mp_const_true; } else { return mp_const_false; }
126+
default: return MP_OBJ_NULL; // op not supported for None
127+
}
128+
}
129+
122130
static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
123131
mp_obj_list_t *o = lhs;
124132
switch (op) {
@@ -395,6 +403,7 @@ const mp_obj_type_t list_type = {
395403
"list",
396404
.print = list_print,
397405
.make_new = list_make_new,
406+
.unary_op = list_unary_op,
398407
.binary_op = list_binary_op,
399408
.getiter = list_getiter,
400409
.methods = list_type_methods,

py/objnone.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,28 @@
66
#include "mpconfig.h"
77
#include "qstr.h"
88
#include "obj.h"
9+
#include "runtime0.h"
910

1011
typedef struct _mp_obj_none_t {
1112
mp_obj_base_t base;
1213
} mp_obj_none_t;
1314

14-
void none_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
15+
static void none_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
1516
print(env, "None");
1617
}
1718

19+
static mp_obj_t none_unary_op(int op, mp_obj_t o_in) {
20+
switch (op) {
21+
case RT_UNARY_OP_NOT: return mp_const_true;
22+
default: return MP_OBJ_NULL; // op not supported for None
23+
}
24+
}
25+
1826
const mp_obj_type_t none_type = {
1927
{ &mp_const_type },
2028
"NoneType",
2129
.print = none_print,
30+
.unary_op = none_unary_op,
2231
};
2332

2433
static const mp_obj_none_t none_obj = {{&none_type}};

py/objtuple.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ static mp_obj_t tuple_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m
7373
}
7474
}
7575

76+
static mp_obj_t tuple_unary_op(int op, mp_obj_t self_in) {
77+
mp_obj_tuple_t *self = self_in;
78+
switch (op) {
79+
case RT_UNARY_OP_NOT: if (self->len == 0) { return mp_const_true; } else { return mp_const_false; }
80+
default: return MP_OBJ_NULL; // op not supported for None
81+
}
82+
}
83+
7684
static mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
7785
mp_obj_tuple_t *o = lhs;
7886
switch (op) {
@@ -97,6 +105,7 @@ const mp_obj_type_t tuple_type = {
97105
"tuple",
98106
.print = tuple_print,
99107
.make_new = tuple_make_new,
108+
.unary_op = tuple_unary_op,
100109
.binary_op = tuple_binary_op,
101110
.getiter = tuple_getiter,
102111
};

py/runtime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
481481
if (MP_OBJ_IS_SMALL_INT(arg)) {
482482
mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
483483
switch (op) {
484-
case RT_UNARY_OP_NOT: if (val != 0) { return mp_const_true;} else { return mp_const_false; }
484+
case RT_UNARY_OP_NOT: if (val == 0) { return mp_const_true;} else { return mp_const_false; }
485485
case RT_UNARY_OP_POSITIVE: break;
486486
case RT_UNARY_OP_NEGATIVE: val = -val; break;
487487
case RT_UNARY_OP_INVERT: val = ~val; break;

tests/basics/unary_op.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
print(not None)
2+
print(not False)
3+
print(not True)
4+
print(not 0)
5+
print(not 1)
6+
print(not -1)
7+
print(not ())
8+
print(not (1,))
9+
print(not [])
10+
print(not [1,])
11+
print(not {})
12+
print(not {1:1})

0 commit comments

Comments
 (0)