File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff line change 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" )
You can’t perform that action at this time.
0 commit comments