summaryrefslogtreecommitdiff
path: root/py/objint.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-01-15 22:58:39 +0000
committerDamien George <damien.p.george@gmail.com>2014-01-15 22:58:39 +0000
commit5573f9f150d513ed3f996722e2b57b58cfb24f3b (patch)
tree44a46d84515c9c8943ab20aa89fce0acce0fa5a5 /py/objint.c
parent4899ff9470734f0593bbe1f5272bc3d23facf7f7 (diff)
parent3e4ed25138b7ffc295843dfc11e44bccfb22ab9c (diff)
Merge branch 'str2int' of github.com:xyb/micropython into xyb-str2int
Conflicts: py/objint.c unix-cpy/Makefile unix/Makefile
Diffstat (limited to 'py/objint.c')
-rw-r--r--py/objint.c14
1 files changed, 10 insertions, 4 deletions
diff --git a/py/objint.c b/py/objint.c
index efec60030..937bff7ae 100644
--- a/py/objint.c
+++ b/py/objint.c
@@ -18,11 +18,17 @@ static mp_obj_t int_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args)
return MP_OBJ_NEW_SMALL_INT(0);
case 1:
- // TODO allow string as arg and parse it
- return mp_obj_new_int(mp_obj_get_int(args[0]));
+ if (MP_OBJ_IS_TYPE(args[0], &str_type)) {
+ // a string, parse it
+ return MP_OBJ_NEW_SMALL_INT(strtonum(qstr_str(mp_obj_get_qstr(args[0])), 0));
+ } else {
+ return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
+ }
- //case 2:
- // TODO, parse with given base
+ case 2:
+ // should be a string, parse it
+ // TODO proper error checking of argument types
+ return MP_OBJ_NEW_SMALL_INT(strtonum(qstr_str(mp_obj_get_qstr(args[1])), mp_obj_get_int(args[0])));
default:
nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "int takes at most 2 arguments, %d given", (void*)(machine_int_t)n_args));