diff options
Diffstat (limited to 'Objects/object.c')
-rw-r--r-- | Objects/object.c | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/Objects/object.c b/Objects/object.c index 9a8d4b391b..e28158e59b 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -213,6 +213,29 @@ setattr(v, name, w) } } +/* Test a value used as condition, e.g., in a for or if statement. + Return -1 if an error occurred */ + +int +testbool(v) + object *v; +{ + int res; + if (v == None) + res = 0; + else if (v->ob_type->tp_as_number != NULL) + res = (*v->ob_type->tp_as_number->nb_nonzero)(v); + else if (v->ob_type->tp_as_mapping != NULL) + res = (*v->ob_type->tp_as_mapping->mp_length)(v); + else if (v->ob_type->tp_as_sequence != NULL) + res = (*v->ob_type->tp_as_sequence->sq_length)(v); + else + res = 1; + if (res > 0) + res = 1; + return res; +} + /* NoObject is usable as a non-NULL undefined value, used by the macro None. |