Merge branch 'str2int' of github.com:xyb/micropython into xyb-str2int

Conflicts:
	py/objint.c
	unix-cpy/Makefile
	unix/Makefile
diff --git a/py/misc.h b/py/misc.h
index e985bc5..5b012d0 100644
--- a/py/misc.h
+++ b/py/misc.h
@@ -50,6 +50,8 @@
 #define streq(s1, s2) (strcmp((s1), (s2)) == 0)
 */
 
+long strtonum(const char *restrict s, int base);
+
 /** variable string *********************************************/
 
 typedef struct _vstr_t {
diff --git a/py/obj.h b/py/obj.h
index 99d430f..cbd12fe 100644
--- a/py/obj.h
+++ b/py/obj.h
@@ -243,6 +243,7 @@
 bool mp_obj_less(mp_obj_t o1, mp_obj_t o2);
 
 machine_int_t mp_obj_get_int(mp_obj_t arg);
+machine_int_t mp_obj_parse_int(mp_obj_t arg, mp_obj_t base_arg);
 #if MICROPY_ENABLE_FLOAT
 mp_float_t mp_obj_get_float(mp_obj_t self_in);
 void mp_obj_get_complex(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
diff --git a/py/objint.c b/py/objint.c
index efec600..937bff7 100644
--- a/py/objint.c
+++ b/py/objint.c
@@ -18,11 +18,17 @@
             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));
diff --git a/py/py.mk b/py/py.mk
index e51b00f..61f14f7 100644
--- a/py/py.mk
+++ b/py/py.mk
@@ -70,6 +70,7 @@
 	emitinlinethumb.o \
 	runtime.o \
 	map.o \
+	strtonum.o \
 	obj.o \
 	objbool.o \
 	objboundmeth.o \
diff --git a/py/strtonum.c b/py/strtonum.c
new file mode 100644
index 0000000..02c1506
--- /dev/null
+++ b/py/strtonum.c
@@ -0,0 +1,96 @@
+#if defined(UNIX)
+
+#include <ctype.h>
+#include <errno.h>
+#include <stdlib.h>
+
+#include "misc.h"
+#include "mpconfig.h"
+#include "mpqstr.h"
+#include "nlr.h"
+#include "obj.h"
+
+long strtonum(const char *restrict s, int base) {
+    int c, neg = 0;
+    const char *p = s;
+    char *num;
+    long found;
+
+    // check radix base
+    if ((base != 0 && base < 2) || base > 36) {
+        nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "ValueError: int() arg 2 must be >=2 and <= 36"));
+    }
+    // skip surrounded whitespace
+    while (isspace((c = *(p++))));
+    if (c == 0) {
+        goto value_error;
+    }
+    // preced sign
+    if (c == '+' || c == '-') {
+        neg = - (c == '-');
+        c = *(p++);
+    }
+
+    // find real radix base, and strip preced '0x', '0o' and '0b'
+    // TODO somehow merge with similar code in parse.c
+    if ((base == 0 || base == 16) && c == '0') {
+        c = *(p++);
+        if ((c | 32) == 'x') {
+            base = 16;
+        } else if (base == 0 && (c | 32) == 'o') {
+            base = 8;
+        } else if (base == 0 && (c | 32) == 'b') {
+            base = 2;
+        } else {
+            base = 10;
+            p -= 2;
+        }
+    } else if (base == 8 && c == '0') {
+        c = *(p++);
+        if ((c | 32) != 'o') {
+            p -= 2;
+        }
+    } else if (base == 2 && c == '0') {
+        c = *(p++);
+        if ((c | 32) != 'b') {
+            p -= 2;
+        }
+    } else {
+        if (base == 0) base = 10;
+        p--;
+    }
+
+    found = strtol(p, &num, base);
+    if (errno) {
+        goto value_error;
+    } else if (found && *(num) == 0) {
+        goto done;
+    } else if (found || num != p) {
+        goto check_tail_space;
+    } else {
+        goto value_error;
+    }
+
+check_tail_space:
+    if (*(num) != 0) {
+        while (isspace((c = *(num++))));
+        if (c != 0) {
+            goto value_error;
+        }
+    }
+
+done:
+    return (found ^ neg) - neg;
+
+value_error:
+    nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_ValueError, "invalid literal for int() with base %d: '%s'", (void*)(machine_uint_t)base, s));
+}
+
+#else /* defined(UNIX) */
+
+long strtonum(const char *restrict s, int base) {
+    // TODO port strtol to stm
+    return 0;
+}
+
+#endif /* defined(UNIX) */
diff --git a/tests/basics/tests/int1.py b/tests/basics/tests/int1.py
new file mode 100644
index 0000000..4ce0f9e
--- /dev/null
+++ b/tests/basics/tests/int1.py
@@ -0,0 +1,74 @@
+print(int(0))
+print(int(1))
+print(int(+1))
+print(int(-1))
+
+print(int('0'))
+print(int('+0'))
+print(int('-0'))
+print(int('1'))
+print(int('+1'))
+print(int('-1'))
+print(int('01'))
+print(int('9'))
+print(int('10'))
+print(int('+10'))
+print(int('-10'))
+print(int('12'))
+print(int('-12'))
+print(int('99'))
+print(int('100'))
+print(int('314'))
+print(int(' 314'))
+print(int('314 '))
+print(int('  \t\t  314  \t\t  '))
+print(int('  1  '))
+print(int(' -3 '))
+
+print(int('0', 10))
+print(int('1', 10))
+print(int(' \t 1 \t ', 10))
+print(int('11', 10))
+print(int('11', 16))
+print(int('11', 8))
+print(int('11', 2))
+print(int('11', 36))
+print(int('0o123', 0))
+print(int('8388607'))
+print(int('0x123', 16))
+print(int('0X123', 16))
+print(int('0o123', 8))
+print(int('0O123', 8))
+print(int('0123', 8))
+print(int('0b100', 2))
+print(int('0B100', 2))
+print(int('0100', 2))
+print(int(' \t 0o12', 8))
+print(int('0o12  \t  ', 8))
+
+
+def test(value, base):
+    try:
+        print(int(value, base))
+    except ValueError:
+        print('ValueError')
+
+
+test('x', 0)
+test('1x', 0)
+test('  1x', 0)
+test('  1' + chr(2) + '  ', 0)
+test('', 0)
+test(' ', 0)
+test('  \t\t  ', 0)
+test("\u0200", 0)
+test('0x', 16)
+test('0x', 0)
+test('0o', 8)
+test('0o', 0)
+test('0b', 2)
+test('0b', 0)
+test('0b2', 2)
+test('0o8', 8)
+test('0xg', 16)
+test('1 1', 16)
diff --git a/unix-cpy/Makefile b/unix-cpy/Makefile
index e59a06b..4955ea0 100644
--- a/unix-cpy/Makefile
+++ b/unix-cpy/Makefile
@@ -11,7 +11,7 @@
 
 # compiler settings
 CC = gcc
-CFLAGS = -I. -I$(PY_SRC) -Wall -Werror -ansi -std=gnu99
+CFLAGS = -I. -I$(PY_SRC) -Wall -Werror -ansi -std=gnu99 -DUNIX
 LDFLAGS = -lm
 
 #Debugging/Optimization
diff --git a/unix/Makefile b/unix/Makefile
index 2c54cf2..9055909 100644
--- a/unix/Makefile
+++ b/unix/Makefile
@@ -11,7 +11,7 @@
 
 # compiler settings
 CC = gcc
-CFLAGS = -I. -I$(PY_SRC) -Wall -Werror -ansi -std=gnu99
+CFLAGS = -I. -I$(PY_SRC) -Wall -Werror -ansi -std=gnu99 -DUNIX
 LDFLAGS = -lm
 
 #Debugging/Optimization