Skip to content

Commit 6e48f7f

Browse files
committed
py: Allow 'complex()' to take a string as first argument.
1 parent c06ea7a commit 6e48f7f

File tree

5 files changed

+19
-9
lines changed

5 files changed

+19
-9
lines changed

py/objcomplex.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "mpconfig.h"
77
#include "qstr.h"
88
#include "obj.h"
9+
#include "parsenum.h"
910
#include "runtime0.h"
1011
#include "map.h"
1112

@@ -36,15 +37,20 @@ STATIC mp_obj_t complex_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const
3637
return mp_obj_new_complex(0, 0);
3738

3839
case 1:
39-
// TODO allow string as first arg and parse it
40-
if (MP_OBJ_IS_TYPE(args[0], &mp_type_complex)) {
40+
if (MP_OBJ_IS_STR(args[0])) {
41+
// a string, parse it
42+
uint l;
43+
const char *s = mp_obj_str_get_data(args[0], &l);
44+
return mp_parse_num_decimal(s, l, true, true);
45+
} else if (MP_OBJ_IS_TYPE(args[0], &mp_type_complex)) {
46+
// a complex, just return it
4147
return args[0];
4248
} else {
49+
// something else, try to cast it to a complex
4350
return mp_obj_new_complex(mp_obj_get_float(args[0]), 0);
4451
}
4552

46-
case 2:
47-
{
53+
case 2: {
4854
mp_float_t real, imag;
4955
if (MP_OBJ_IS_TYPE(args[0], &mp_type_complex)) {
5056
mp_obj_complex_get(args[0], &real, &imag);

py/objfloat.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ STATIC mp_obj_t float_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m
3838
// a string, parse it
3939
uint l;
4040
const char *s = mp_obj_str_get_data(args[0], &l);
41-
return mp_parse_num_decimal(s, l, false);
41+
return mp_parse_num_decimal(s, l, false, false);
4242
} else if (MP_OBJ_IS_TYPE(args[0], &mp_type_float)) {
43+
// a float, just return it
4344
return args[0];
4445
} else {
46+
// something else, try to cast it to a float
4547
return mp_obj_new_float(mp_obj_get_float(args[0]));
4648
}
4749

py/parsenum.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base) {
8888
#define PARSE_DEC_IN_FRAC (2)
8989
#define PARSE_DEC_IN_EXP (3)
9090

91-
mp_obj_t mp_parse_num_decimal(const char *str, uint len, bool allow_imag) {
91+
mp_obj_t mp_parse_num_decimal(const char *str, uint len, bool allow_imag, bool force_complex) {
9292
#if MICROPY_ENABLE_FLOAT
9393
const char *top = str + len;
9494
mp_float_t dec_val = 0;
@@ -129,7 +129,7 @@ mp_obj_t mp_parse_num_decimal(const char *str, uint len, bool allow_imag) {
129129
dec_val = MICROPY_FLOAT_C_FUN(nan)("");
130130
}
131131
} else {
132-
// parse the digits
132+
// string should be a decimal number
133133
int in = PARSE_DEC_IN_INTG;
134134
bool exp_neg = false;
135135
int exp_val = 0;
@@ -198,6 +198,8 @@ mp_obj_t mp_parse_num_decimal(const char *str, uint len, bool allow_imag) {
198198
// return the object
199199
if (imag) {
200200
return mp_obj_new_complex(0, dec_val);
201+
} else if (force_complex) {
202+
return mp_obj_new_complex(dec_val, 0);
201203
} else {
202204
return mp_obj_new_float(dec_val);
203205
}

py/parsenum.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base);
2-
mp_obj_t mp_parse_num_decimal(const char *str, uint len, bool allow_imag);
2+
mp_obj_t mp_parse_num_decimal(const char *str, uint len, bool allow_imag, bool force_complex);

py/runtime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ mp_obj_t rt_load_const_dec(qstr qstr) {
375375
DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
376376
uint len;
377377
const byte* data = qstr_data(qstr, &len);
378-
return mp_parse_num_decimal((const char*)data, len, true);
378+
return mp_parse_num_decimal((const char*)data, len, true, false);
379379
}
380380

381381
mp_obj_t rt_load_const_str(qstr qstr) {

0 commit comments

Comments
 (0)