Skip to content

Commit 5a87750

Browse files
committed
Merge branch 'master' of github.com:dpgeorge/micropython
2 parents 8161a10 + 10744dd commit 5a87750

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

py/runtime.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,24 @@ int rt_is_true(mp_obj_t arg) {
297297
return 0;
298298
} else if (arg == mp_const_true) {
299299
return 1;
300+
} else if (MP_OBJ_IS_QSTR(arg)) {
301+
// TODO: \0
302+
return *qstr_str(MP_OBJ_QSTR_VALUE(arg)) != 0;
303+
} else if (MP_OBJ_IS_TYPE(arg, &str_type)) {
304+
// TODO: \0
305+
return *qstr_str(mp_obj_str_get(arg)) != 0;
306+
} else if (MP_OBJ_IS_TYPE(arg, &list_type)) {
307+
uint len;
308+
mp_obj_t *dummy;
309+
mp_obj_list_get(arg, &len, &dummy);
310+
return len != 0;
311+
} else if (MP_OBJ_IS_TYPE(arg, &tuple_type)) {
312+
uint len;
313+
mp_obj_t *dummy;
314+
mp_obj_tuple_get(arg, &len, &dummy);
315+
return len != 0;
316+
} else if (MP_OBJ_IS_TYPE(arg, &dict_type)) {
317+
return mp_obj_dict_len(arg) != 0;
300318
} else {
301319
assert(0);
302320
return 0;

tests/basics/tests/true-value.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Test true-ish value handling
2+
3+
if not False:
4+
print("False")
5+
6+
if not 0:
7+
print("0")
8+
9+
if not "":
10+
print("Empty string")
11+
if "foo":
12+
print("Non-empty string")
13+
14+
if not ():
15+
print("Empty tuple")
16+
if ("",):
17+
print("Non-empty tuple")
18+
19+
if not []:
20+
print("Empty list")
21+
if [0]:
22+
print("Non-empty list")
23+
24+
if not {}:
25+
print("Empty dict")
26+
if {0:0}:
27+
print("Non-empty dict")

0 commit comments

Comments
 (0)