Skip to content

Commit c1d9bbc

Browse files
committed
Implement __bool__ and __len__ via unary_op virtual method for all types.
__bool__() and __len__() are just the same as __neg__() or __invert__(), and require efficient dispatching implementation (not requiring search/lookup). type->unary_op() is just the right choice for this short of adding standalone virtual method(s) to already big mp_obj_type_t structure.
1 parent cdd2c62 commit c1d9bbc

File tree

13 files changed

+52
-16
lines changed

13 files changed

+52
-16
lines changed

py/obj.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,15 @@ mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
251251
len = seq_len;
252252
} else if (MP_OBJ_IS_TYPE(o_in, &dict_type)) {
253253
len = mp_obj_dict_len(o_in);
254-
} else if (MP_OBJ_IS_TYPE(o_in, &array_type)) {
255-
len = mp_obj_array_len(o_in);
256254
} else {
255+
mp_obj_type_t *type = mp_obj_get_type(o_in);
256+
if (type->unary_op != NULL) {
257+
mp_obj_t result = type->unary_op(RT_UNARY_OP_LEN, o_in);
258+
if (result != MP_OBJ_NULL) {
259+
return result;
260+
}
261+
}
262+
257263
return MP_OBJ_NULL;
258264
}
259265
return MP_OBJ_NEW_SMALL_INT(len);

py/objarray.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,15 @@ static mp_obj_t mp_builtin_bytearray(mp_obj_t arg) {
189189
}
190190
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bytearray_obj, mp_builtin_bytearray);
191191

192+
static mp_obj_t array_unary_op(int op, mp_obj_t o_in) {
193+
mp_obj_array_t *o = o_in;
194+
switch (op) {
195+
case RT_UNARY_OP_BOOL: return MP_BOOL(o->len != 0);
196+
case RT_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(o->len);
197+
default: return MP_OBJ_NULL; // op not supported
198+
}
199+
}
200+
192201
static mp_obj_t array_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
193202
mp_obj_array_t *o = lhs;
194203
switch (op) {
@@ -245,6 +254,7 @@ const mp_obj_type_t array_type = {
245254
.print = array_print,
246255
.make_new = array_make_new,
247256
.getiter = array_iterator_new,
257+
.unary_op = array_unary_op,
248258
.binary_op = array_binary_op,
249259
.store_item = array_store_item,
250260
.methods = array_type_methods,

py/objbool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static mp_obj_t bool_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp
3636
static mp_obj_t bool_unary_op(int op, mp_obj_t o_in) {
3737
machine_int_t value = ((mp_obj_bool_t*)o_in)->value;
3838
switch (op) {
39-
case RT_UNARY_OP_NOT: if (value) { return mp_const_false; } else { return mp_const_true; }
39+
case RT_UNARY_OP_BOOL: return o_in;
4040
case RT_UNARY_OP_POSITIVE: return MP_OBJ_NEW_SMALL_INT(value);
4141
case RT_UNARY_OP_NEGATIVE: return MP_OBJ_NEW_SMALL_INT(-value);
4242
case RT_UNARY_OP_INVERT:

py/objcomplex.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static mp_obj_t complex_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const
7373
static mp_obj_t complex_unary_op(int op, mp_obj_t o_in) {
7474
mp_obj_complex_t *o = o_in;
7575
switch (op) {
76-
case RT_UNARY_OP_NOT: if (o->real != 0 || o->imag != 0) { return mp_const_true;} else { return mp_const_false; }
76+
case RT_UNARY_OP_BOOL: return MP_BOOL(o->real != 0 || o->imag != 0);
7777
case RT_UNARY_OP_POSITIVE: return o_in;
7878
case RT_UNARY_OP_NEGATIVE: return mp_obj_new_complex(-o->real, -o->imag);
7979
default: return MP_OBJ_NULL; // op not supported

py/objdict.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ static mp_obj_t dict_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp
4646
static mp_obj_t dict_unary_op(int op, mp_obj_t self_in) {
4747
mp_obj_dict_t *self = self_in;
4848
switch (op) {
49-
case RT_UNARY_OP_NOT: if (self->map.used == 0) { return mp_const_true; } else { return mp_const_false; }
49+
case RT_UNARY_OP_BOOL: return MP_BOOL(self->map.used != 0);
50+
case RT_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->map.used);
5051
default: return MP_OBJ_NULL; // op not supported for None
5152
}
5253
}

py/objfloat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static mp_obj_t float_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m
4747
static mp_obj_t float_unary_op(int op, mp_obj_t o_in) {
4848
mp_obj_float_t *o = o_in;
4949
switch (op) {
50-
case RT_UNARY_OP_NOT: if (o->value != 0) { return mp_const_true;} else { return mp_const_false; }
50+
case RT_UNARY_OP_BOOL: return MP_BOOL(o->value != 0);
5151
case RT_UNARY_OP_POSITIVE: return o_in;
5252
case RT_UNARY_OP_NEGATIVE: return mp_obj_new_float(-o->value);
5353
default: return NULL; // op not supported

py/objint_longlong.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj
3535
mp_obj_t int_unary_op(int op, mp_obj_t o_in) {
3636
mp_obj_int_t *o = o_in;
3737
switch (op) {
38-
case RT_UNARY_OP_NOT: return MP_BOOL(o->val != 0); // TODO: implements RT_UNARY_OP_BOOL
38+
case RT_UNARY_OP_BOOL: return MP_BOOL(o->val != 0);
3939
case RT_UNARY_OP_POSITIVE: return o_in;
4040
case RT_UNARY_OP_NEGATIVE: return mp_obj_new_int_from_ll(-o->val);
4141
case RT_UNARY_OP_INVERT: return mp_obj_new_int_from_ll(~o->val);

py/objlist.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ static bool list_cmp_helper(int op, mp_obj_t self_in, mp_obj_t another_in) {
125125
static mp_obj_t list_unary_op(int op, mp_obj_t self_in) {
126126
mp_obj_list_t *self = self_in;
127127
switch (op) {
128-
case RT_UNARY_OP_NOT: if (self->len == 0) { return mp_const_true; } else { return mp_const_false; }
128+
case RT_UNARY_OP_BOOL: return MP_BOOL(self->len != 0);
129+
case RT_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->len);
129130
default: return MP_OBJ_NULL; // op not supported for None
130131
}
131132
}

py/objnone.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static void none_print(void (*print)(void *env, const char *fmt, ...), void *env
1818

1919
static mp_obj_t none_unary_op(int op, mp_obj_t o_in) {
2020
switch (op) {
21-
case RT_UNARY_OP_NOT: return mp_const_true;
21+
case RT_UNARY_OP_BOOL: return mp_const_false;
2222
default: return MP_OBJ_NULL; // op not supported for None
2323
}
2424
}

py/objtuple.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ static mp_obj_t tuple_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m
7676
static mp_obj_t tuple_unary_op(int op, mp_obj_t self_in) {
7777
mp_obj_tuple_t *self = self_in;
7878
switch (op) {
79-
case RT_UNARY_OP_NOT: if (self->len == 0) { return mp_const_true; } else { return mp_const_false; }
79+
case RT_UNARY_OP_BOOL: return MP_BOOL(self->len != 0);
80+
case RT_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->len);
8081
default: return MP_OBJ_NULL; // op not supported for None
8182
}
8283
}

py/runtime.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -317,15 +317,20 @@ int rt_is_true(mp_obj_t arg) {
317317
} else if (arg == mp_const_true) {
318318
return 1;
319319
} else {
320+
mp_obj_type_t *type = mp_obj_get_type(arg);
321+
if (type->unary_op != NULL) {
322+
mp_obj_t result = type->unary_op(RT_UNARY_OP_BOOL, arg);
323+
if (result != NULL) {
324+
return result == mp_const_true;
325+
}
326+
}
327+
320328
mp_obj_t len = mp_obj_len_maybe(arg);
321329
if (len != MP_OBJ_NULL) {
322330
// obj has a length, truth determined if len != 0
323331
return len != MP_OBJ_NEW_SMALL_INT(0);
324332
} else {
325-
// TODO check for __bool__ method
326-
// TODO check floats and complex numbers
327-
328-
// any other obj is true (TODO is that correct?)
333+
// any other obj is true per Python semantics
329334
return 1;
330335
}
331336
}
@@ -476,7 +481,7 @@ mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
476481
if (MP_OBJ_IS_SMALL_INT(arg)) {
477482
mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
478483
switch (op) {
479-
case RT_UNARY_OP_NOT: if (val == 0) { return mp_const_true;} else { return mp_const_false; }
484+
case RT_UNARY_OP_BOOL: return MP_BOOL(val != 0);
480485
case RT_UNARY_OP_POSITIVE: break;
481486
case RT_UNARY_OP_NEGATIVE: val = -val; break;
482487
case RT_UNARY_OP_INVERT: val = ~val; break;

py/runtime0.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
typedef enum {
2-
RT_UNARY_OP_NOT, // TODO remove this op since it's no longer needed
2+
RT_UNARY_OP_BOOL, // __bool__
3+
RT_UNARY_OP_LEN, // __len__
34
RT_UNARY_OP_POSITIVE,
45
RT_UNARY_OP_NEGATIVE,
56
RT_UNARY_OP_INVERT,
7+
// Used only for CPython-compatible codegeneration
8+
RT_UNARY_OP_NOT,
69
} rt_unary_op_t;
710

811
typedef enum {

tests/basics/true-value.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@
33
if not False:
44
print("False")
55

6+
if not None:
7+
print("None")
8+
69
if not 0:
710
print("0")
811

12+
if not 0.0:
13+
print("float 0")
14+
15+
if not 0+0j:
16+
print("complex 0")
17+
918
if not "":
1019
print("Empty string")
1120
if "foo":

0 commit comments

Comments
 (0)