diff options
author | Damien George <damien.p.george@gmail.com> | 2014-03-21 11:45:46 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-03-21 11:45:46 +0000 |
commit | 6e48f7fa856e4acaf085dfc8876c4e3772d979c2 (patch) | |
tree | 46c65f64490eae2818fbf9bf334a558453be9e88 /py/objcomplex.c | |
parent | c06ea7abf249765bf93595fc42656eed585d7a47 (diff) |
py: Allow 'complex()' to take a string as first argument.
Diffstat (limited to 'py/objcomplex.c')
-rw-r--r-- | py/objcomplex.c | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/py/objcomplex.c b/py/objcomplex.c index 65957cbf6..2ba522615 100644 --- a/py/objcomplex.c +++ b/py/objcomplex.c @@ -6,6 +6,7 @@ #include "mpconfig.h" #include "qstr.h" #include "obj.h" +#include "parsenum.h" #include "runtime0.h" #include "map.h" @@ -36,15 +37,20 @@ STATIC mp_obj_t complex_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const return mp_obj_new_complex(0, 0); case 1: - // TODO allow string as first arg and parse it - if (MP_OBJ_IS_TYPE(args[0], &mp_type_complex)) { + if (MP_OBJ_IS_STR(args[0])) { + // a string, parse it + uint l; + const char *s = mp_obj_str_get_data(args[0], &l); + return mp_parse_num_decimal(s, l, true, true); + } else if (MP_OBJ_IS_TYPE(args[0], &mp_type_complex)) { + // a complex, just return it return args[0]; } else { + // something else, try to cast it to a complex return mp_obj_new_complex(mp_obj_get_float(args[0]), 0); } - case 2: - { + case 2: { mp_float_t real, imag; if (MP_OBJ_IS_TYPE(args[0], &mp_type_complex)) { mp_obj_complex_get(args[0], &real, &imag); |