aboutsummaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
Diffstat (limited to 'py')
-rw-r--r--py/builtin.c90
-rw-r--r--py/builtin.h36
-rw-r--r--py/builtinimport.c2
-rw-r--r--py/lexer.c2
-rw-r--r--py/misc.h7
-rw-r--r--py/mpconfig.h2
-rw-r--r--py/mpqstrraw.h2
-rw-r--r--py/obj.c10
-rw-r--r--py/obj.h8
-rw-r--r--py/objdict.c16
-rw-r--r--py/objexcept.c111
-rw-r--r--py/objfun.c80
-rw-r--r--py/objint.c15
-rw-r--r--py/objint.h9
-rw-r--r--py/objlist.c5
-rw-r--r--py/objset.c8
-rw-r--r--py/objstr.c24
-rw-r--r--py/objtuple.c29
-rw-r--r--py/objtuple.h7
-rw-r--r--py/objzip.c57
-rw-r--r--py/py.mk1
-rw-r--r--py/runtime.c102
-rw-r--r--py/runtime.h6
-rw-r--r--py/stream.c42
-rw-r--r--py/stream.h1
-rw-r--r--py/vm.c15
-rw-r--r--py/vstr.c45
27 files changed, 508 insertions, 224 deletions
diff --git a/py/builtin.c b/py/builtin.c
index 078f4b49c..8f93e843b 100644
--- a/py/builtin.c
+++ b/py/builtin.c
@@ -18,7 +18,7 @@
// args[0] is function from class body
// args[1] is class name
// args[2:] are base objects
-mp_obj_t mp_builtin___build_class__(int n_args, const mp_obj_t *args) {
+static mp_obj_t mp_builtin___build_class__(int n_args, const mp_obj_t *args) {
assert(2 <= n_args);
// we differ from CPython: we set the new __locals__ object here
@@ -62,7 +62,7 @@ mp_obj_t mp_builtin___build_class__(int n_args, const mp_obj_t *args) {
MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj, 2, mp_builtin___build_class__);
-mp_obj_t mp_builtin___repl_print__(mp_obj_t o) {
+static mp_obj_t mp_builtin___repl_print__(mp_obj_t o) {
if (o != mp_const_none) {
mp_obj_print(o);
printf("\n");
@@ -70,6 +70,8 @@ mp_obj_t mp_builtin___repl_print__(mp_obj_t o) {
return mp_const_none;
}
+MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin___repl_print___obj, mp_builtin___repl_print__);
+
mp_obj_t mp_builtin_abs(mp_obj_t o_in) {
if (MP_OBJ_IS_SMALL_INT(o_in)) {
mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(o_in);
@@ -97,7 +99,9 @@ mp_obj_t mp_builtin_abs(mp_obj_t o_in) {
}
}
-mp_obj_t mp_builtin_all(mp_obj_t o_in) {
+MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_abs_obj, mp_builtin_abs);
+
+static mp_obj_t mp_builtin_all(mp_obj_t o_in) {
mp_obj_t iterable = rt_getiter(o_in);
mp_obj_t item;
while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
@@ -108,7 +112,9 @@ mp_obj_t mp_builtin_all(mp_obj_t o_in) {
return mp_const_true;
}
-mp_obj_t mp_builtin_any(mp_obj_t o_in) {
+MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_all_obj, mp_builtin_all);
+
+static mp_obj_t mp_builtin_any(mp_obj_t o_in) {
mp_obj_t iterable = rt_getiter(o_in);
mp_obj_t item;
while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
@@ -119,7 +125,9 @@ mp_obj_t mp_builtin_any(mp_obj_t o_in) {
return mp_const_false;
}
-mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
+MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_any_obj, mp_builtin_any);
+
+static mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
if (mp_obj_is_callable(o_in)) {
return mp_const_true;
} else {
@@ -127,7 +135,9 @@ mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
}
}
-mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
+MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_callable_obj, mp_builtin_callable);
+
+static mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
int ord = mp_obj_get_int(o_in);
if (0 <= ord && ord <= 0x10ffff) {
char *str = m_new(char, 2);
@@ -139,7 +149,9 @@ mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
}
}
-mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
+MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_chr_obj, mp_builtin_chr);
+
+static mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
if (MP_OBJ_IS_SMALL_INT(o1_in) && MP_OBJ_IS_SMALL_INT(o2_in)) {
mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1_in);
mp_small_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2_in);
@@ -152,6 +164,8 @@ mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
}
}
+MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_divmod_obj, mp_builtin_divmod);
+
static mp_obj_t mp_builtin_hash(mp_obj_t o_in) {
// TODO hash will generally overflow small integer; can we safely truncate it?
return mp_obj_new_int(mp_obj_hash(o_in));
@@ -165,7 +179,7 @@ static mp_obj_t mp_builtin_iter(mp_obj_t o_in) {
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_iter_obj, mp_builtin_iter);
-mp_obj_t mp_builtin_len(mp_obj_t o_in) {
+static mp_obj_t mp_builtin_len(mp_obj_t o_in) {
mp_obj_t len = mp_obj_len_maybe(o_in);
if (len == NULL) {
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "object of type '%s' has no len()", mp_obj_get_type_str(o_in)));
@@ -174,7 +188,9 @@ mp_obj_t mp_builtin_len(mp_obj_t o_in) {
}
}
-mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args) {
+MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_len_obj, mp_builtin_len);
+
+static mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args) {
if (n_args == 1) {
// given an iterable
mp_obj_t iterable = rt_getiter(args[0]);
@@ -201,7 +217,9 @@ mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args) {
}
}
-mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args) {
+MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_max_obj, 1, mp_builtin_max);
+
+static mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args) {
if (n_args == 1) {
// given an iterable
mp_obj_t iterable = rt_getiter(args[0]);
@@ -228,6 +246,8 @@ mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args) {
}
}
+MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_min_obj, 1, mp_builtin_min);
+
static mp_obj_t mp_builtin_next(mp_obj_t o) {
mp_obj_t ret = rt_iternext(o);
if (ret == mp_const_stop_iteration) {
@@ -239,7 +259,7 @@ static mp_obj_t mp_builtin_next(mp_obj_t o) {
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_next_obj, mp_builtin_next);
-mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
+static mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
const char *str = qstr_str(mp_obj_get_qstr(o_in));
if (strlen(str) == 1) {
return mp_obj_new_int(str[0]);
@@ -248,15 +268,19 @@ mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
}
}
-mp_obj_t mp_builtin_pow(int n_args, const mp_obj_t *args) {
+MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_ord_obj, mp_builtin_ord);
+
+static mp_obj_t mp_builtin_pow(int n_args, const mp_obj_t *args) {
+ assert(2 <= n_args && n_args <= 3);
switch (n_args) {
case 2: return rt_binary_op(RT_BINARY_OP_POWER, args[0], args[1]);
- case 3: return rt_binary_op(RT_BINARY_OP_MODULO, rt_binary_op(RT_BINARY_OP_POWER, args[0], args[1]), args[2]); // TODO optimise...
- default: nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "pow expected at most 3 arguments, got %d", n_args));
+ default: return rt_binary_op(RT_BINARY_OP_MODULO, rt_binary_op(RT_BINARY_OP_POWER, args[0], args[1]), args[2]); // TODO optimise...
}
}
-mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args) {
+MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_pow_obj, 2, 3, mp_builtin_pow);
+
+static mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args) {
for (int i = 0; i < n_args; i++) {
if (i > 0) {
printf(" ");
@@ -273,21 +297,25 @@ mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args) {
return mp_const_none;
}
-mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args) {
+MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin_print_obj, 0, mp_builtin_print);
+
+static mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args) {
+ assert(1 <= n_args && n_args <= 3);
switch (n_args) {
case 1: return mp_obj_new_range(0, mp_obj_get_int(args[0]), 1);
case 2: return mp_obj_new_range(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), 1);
- case 3: return mp_obj_new_range(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), mp_obj_get_int(args[2]));
- default: nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "range expected at most 3 arguments, got %d", n_args));
+ default: return mp_obj_new_range(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), mp_obj_get_int(args[2]));
}
}
-mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) {
+MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_range_obj, 1, 3, mp_builtin_range);
+
+static mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) {
+ assert(1 <= n_args && n_args <= 2);
mp_obj_t value;
switch (n_args) {
case 1: value = mp_obj_new_int(0); break;
- case 2: value = args[1]; break;
- default: nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "sum expected at most 2 arguments, got %d", n_args));
+ default: value = args[1]; break;
}
mp_obj_t iterable = rt_getiter(args[0]);
mp_obj_t item;
@@ -296,3 +324,23 @@ mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) {
}
return value;
}
+MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum);
+
+static mp_obj_t mp_builtin_sorted(mp_obj_t args, mp_map_t *kwargs) {
+ mp_obj_t *args_items = NULL;
+ uint args_len = 0;
+
+ assert(MP_OBJ_IS_TYPE(args, &tuple_type));
+ mp_obj_tuple_get(args, &args_len, &args_items);
+ assert(args_len >= 1);
+ if (args_len > 1) {
+ nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError,
+ "must use keyword argument for key function"));
+ }
+ mp_obj_t self = list_type.make_new((mp_obj_t)&list_type, 1, args_items);
+ mp_obj_t new_args = rt_build_tuple(1, &self);
+ mp_obj_list_sort(new_args, kwargs);
+
+ return self;
+}
+MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted);
diff --git a/py/builtin.h b/py/builtin.h
index db7d517a0..3401528a6 100644
--- a/py/builtin.h
+++ b/py/builtin.h
@@ -1,25 +1,25 @@
-// TODO convert all these to objects using MP_DECLARE and MP_DEFINE
+mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin___build_class___obj);
-mp_obj_t mp_builtin___import__(int n, mp_obj_t *args);
-mp_obj_t mp_builtin___repl_print__(mp_obj_t o);
-mp_obj_t mp_builtin_abs(mp_obj_t o_in);
-mp_obj_t mp_builtin_all(mp_obj_t o_in);
-mp_obj_t mp_builtin_any(mp_obj_t o_in);
-mp_obj_t mp_builtin_callable(mp_obj_t o_in);
-mp_obj_t mp_builtin_chr(mp_obj_t o_in);
-mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin___repl_print___obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_abs_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_all_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_any_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_callable_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_chr_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_divmod_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_hash_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_isinstance_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_issubclass_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_iter_obj);
-mp_obj_t mp_builtin_len(mp_obj_t o_in);
-mp_obj_t mp_builtin_list(int n_args, const mp_obj_t *args);
-mp_obj_t mp_builtin_max(int n_args, const mp_obj_t *args);
-mp_obj_t mp_builtin_min(int n_args, const mp_obj_t *args);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_len_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_list_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_max_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_min_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_next_obj);
-mp_obj_t mp_builtin_ord(mp_obj_t o_in);
-mp_obj_t mp_builtin_pow(int n_args, const mp_obj_t *args);
-mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args);
-mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args);
-mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_ord_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_pow_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_print_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_range_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sorted_obj);
+MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sum_obj);
diff --git a/py/builtinimport.c b/py/builtinimport.c
index 90a0fc339..33576e3f0 100644
--- a/py/builtinimport.c
+++ b/py/builtinimport.c
@@ -18,7 +18,7 @@
#include "map.h"
#include "builtin.h"
-mp_obj_t mp_builtin___import__(int n, mp_obj_t *args) {
+mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args) {
/*
printf("import:\n");
for (int i = 0; i < n; i++) {
diff --git a/py/lexer.c b/py/lexer.c
index f7f9c631f..da8967b16 100644
--- a/py/lexer.c
+++ b/py/lexer.c
@@ -614,7 +614,7 @@ mp_lexer_t *mp_lexer_new(const char *src_name, void *stream_data, mp_lexer_strea
lex->num_indent_level = 1;
lex->indent_level = m_new(uint16_t, lex->alloc_indent_level);
lex->indent_level[0] = 0;
- vstr_init(&lex->vstr);
+ vstr_init(&lex->vstr, 32);
// preload characters
lex->chr0 = stream_next_char(stream_data);
diff --git a/py/misc.h b/py/misc.h
index 1bf4d8f29..e985bc525 100644
--- a/py/misc.h
+++ b/py/misc.h
@@ -21,6 +21,7 @@ typedef unsigned int uint;
#define m_renew(type, ptr, old_num, new_num) ((type*)(m_realloc((ptr), sizeof(type) * (old_num), sizeof(type) * (new_num))))
#define m_del(type, ptr, num) m_free(ptr, sizeof(type) * (num))
#define m_del_obj(type, ptr) (m_del(type, ptr, 1))
+#define m_del_var(obj_type, var_type, var_num, ptr) (m_free(ptr, sizeof(obj_type) + sizeof(var_type) * (var_num)))
void *m_malloc(int num_bytes);
void *m_malloc0(int num_bytes);
@@ -58,15 +59,19 @@ typedef struct _vstr_t {
bool had_error;
} vstr_t;
-void vstr_init(vstr_t *vstr);
+void vstr_init(vstr_t *vstr, int alloc);
void vstr_clear(vstr_t *vstr);
vstr_t *vstr_new(void);
+vstr_t *vstr_new_size(int alloc);
void vstr_free(vstr_t *vstr);
void vstr_reset(vstr_t *vstr);
bool vstr_had_error(vstr_t *vstr);
char *vstr_str(vstr_t *vstr);
int vstr_len(vstr_t *vstr);
void vstr_hint_size(vstr_t *vstr, int size);
+char *vstr_extend(vstr_t *vstr, int size);
+bool vstr_set_size(vstr_t *vstr, int size);
+bool vstr_shrink(vstr_t *vstr);
char *vstr_add_len(vstr_t *vstr, int len);
void vstr_add_byte(vstr_t *vstr, byte v);
void vstr_add_char(vstr_t *vstr, unichar chr);
diff --git a/py/mpconfig.h b/py/mpconfig.h
index ada4aa2ea..5d8c57692 100644
--- a/py/mpconfig.h
+++ b/py/mpconfig.h
@@ -91,7 +91,7 @@ typedef long long mp_longint_impl_t;
#define BITS_PER_BYTE (8)
#define BITS_PER_WORD (BITS_PER_BYTE * BYTES_PER_WORD)
// machine_int_t value with most significant bit set
-#define WORD_MSBIT_HIGH (1 << (BYTES_PER_WORD * 8 - 1))
+#define WORD_MSBIT_HIGH (((machine_uint_t)1) << (BYTES_PER_WORD * 8 - 1))
// printf format spec to use for machine_int_t and friends
#ifndef INT_FMT
diff --git a/py/mpqstrraw.h b/py/mpqstrraw.h
index e4404a36f..c3cda84b4 100644
--- a/py/mpqstrraw.h
+++ b/py/mpqstrraw.h
@@ -57,9 +57,11 @@ Q(pow)
Q(print)
Q(range)
Q(set)
+Q(sorted)
Q(sum)
Q(tuple)
Q(type)
+Q(zip)
Q(append)
Q(pop)
diff --git a/py/obj.c b/py/obj.c
index 2759437fd..206bf7a24 100644
--- a/py/obj.c
+++ b/py/obj.c
@@ -108,9 +108,15 @@ bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
return val == 0;
} else if (o2 == mp_const_true) {
return val == 1;
- } else {
- return false;
+ } else if (MP_OBJ_IS_TYPE(o2, &int_type)) {
+ // If o2 is long int, dispatch to its virtual methods
+ mp_obj_base_t *o = o2;
+ if (o->type->binary_op != NULL) {
+ mp_obj_t r = o->type->binary_op(RT_COMPARE_OP_EQUAL, o2, o1);
+ return r == mp_const_true ? true : false;
+ }
}
+ return false;
}
} else if (MP_OBJ_IS_QSTR(o1) || MP_OBJ_IS_QSTR(o2)) {
return false;
diff --git a/py/obj.h b/py/obj.h
index a9a5827ee..6dcc6827e 100644
--- a/py/obj.h
+++ b/py/obj.h
@@ -59,7 +59,7 @@ typedef struct _mp_obj_base_t mp_obj_base_t;
#define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 3, 3, (mp_fun_3_t)fun_name)
#define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, n_args_min, (~((machine_uint_t)0)), (mp_fun_var_t)fun_name)
#define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, n_args_min, n_args_max, (mp_fun_var_t)fun_name)
-#define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, true, 0, (~((machine_uint_t)0)), (mp_fun_kw_t)fun_name)
+#define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, n_args_min, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, true, n_args_min, (~((machine_uint_t)0)), (mp_fun_kw_t)fun_name)
// These macros are used to declare and define constant staticmethond and classmethod objects
// You can put "static" in front of the definitions to make them local
@@ -285,12 +285,14 @@ mp_obj_t mp_obj_complex_binary_op(int op, mp_float_t lhs_real, mp_float_t lhs_im
// tuple
extern const mp_obj_type_t tuple_type;
void mp_obj_tuple_get(mp_obj_t self_in, uint *len, mp_obj_t **items);
+void mp_obj_tuple_del(mp_obj_t self_in);
// list
extern const mp_obj_type_t list_type;
mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg);
void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items);
void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
+mp_obj_t mp_obj_list_sort(mp_obj_t args, struct _mp_map_t *kwargs);
// dict
extern const mp_obj_type_t dict_type;
@@ -306,6 +308,10 @@ void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item);
extern const mp_obj_type_t slice_type;
void mp_obj_slice_get(mp_obj_t self_in, machine_int_t *start, machine_int_t *stop, machine_int_t *step);
+// zip
+extern const mp_obj_type_t zip_type;
+
+
// functions
typedef struct _mp_obj_fun_native_t { // need this so we can define const objects (to go in ROM)
mp_obj_base_t base;
diff --git a/py/objdict.c b/py/objdict.c
index 6dbb1f316..e164ed74c 100644
--- a/py/objdict.c
+++ b/py/objdict.c
@@ -57,6 +57,12 @@ static mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
return elem->value;
}
}
+ case RT_COMPARE_OP_IN:
+ case RT_COMPARE_OP_NOT_IN:
+ {
+ mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP);
+ return MP_BOOL((op == RT_COMPARE_OP_IN) ^ (elem == NULL));
+ }
default:
// op not supported
return NULL;
@@ -362,10 +368,20 @@ static void dict_view_print(void (*print)(void *env, const char *fmt, ...), void
print(env, "])");
}
+static mp_obj_t dict_view_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
+ /* only supported for the 'keys' kind until sets and dicts are refactored */
+ mp_obj_dict_view_t *o = lhs_in;
+ if (o->kind != MP_DICT_VIEW_KEYS) return NULL;
+ if (op != RT_COMPARE_OP_IN && op != RT_COMPARE_OP_NOT_IN) return NULL;
+ return dict_binary_op(op, o->dict, rhs_in);
+}
+
+
static const mp_obj_type_t dict_view_type = {
{ &mp_const_type },
"dict_view",
.print = dict_view_print,
+ .binary_op = dict_view_binary_op,
.getiter = dict_view_getiter,
};
diff --git a/py/objexcept.c b/py/objexcept.c
index 4708a27bf..f083e61e5 100644
--- a/py/objexcept.c
+++ b/py/objexcept.c
@@ -8,105 +8,86 @@
#include "misc.h"
#include "mpconfig.h"
#include "obj.h"
+#include "objtuple.h"
+// This is unified class for C-level and Python-level exceptions
+// Python-level exception have empty ->msg and all arguments are in
+// args tuple. C-level excepttion likely have ->msg, and may as well
+// have args tuple (or otherwise have it as NULL).
typedef struct mp_obj_exception_t {
mp_obj_base_t base;
qstr id;
- int n_args;
- const void *args[];
+ qstr msg;
+ mp_obj_tuple_t args;
} mp_obj_exception_t;
void exception_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
mp_obj_exception_t *o = o_in;
- switch (o->n_args) {
- case 0:
- print(env, "%s", qstr_str(o->id));
- break;
- case 1:
- print(env, "%s: %s", qstr_str(o->id), (const char*)o->args[0]);
- break;
- case 2:
- print(env, "%s: ", qstr_str(o->id));
- print(env, (const char*)o->args[0], o->args[1]);
- break;
- default: // here we just assume at least 3 args, but only use first 3
- print(env, "%s: ", qstr_str(o->id));
- print(env, (const char*)o->args[0], o->args[1], o->args[2]);
- break;
+ if (o->msg != 0) {
+ print(env, "%s: %s", qstr_str(o->id), qstr_str(o->msg));
+ } else {
+ print(env, "%s", qstr_str(o->id));
+ tuple_print(print, env, &o->args);
}
}
+// args in reversed order
+static mp_obj_t exception_call(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
+ mp_obj_exception_t *base = self_in;
+ mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, mp_obj_t*, n_args);
+ o->base.type = &exception_type;
+ o->id = base->id;
+ o->msg = 0;
+ o->args.len = n_args;
+
+ // TODO: factor out as reusable copy_reversed()
+ int j = 0;
+ for (int i = n_args - 1; i >= 0; i--) {
+ o->args.items[i] = args[j++];
+ }
+ return o;
+}
+
const mp_obj_type_t exception_type = {
{ &mp_const_type },
"exception",
.print = exception_print,
+ .call_n = exception_call,
};
mp_obj_t mp_obj_new_exception(qstr id) {
- mp_obj_exception_t *o = m_new_obj(mp_obj_exception_t);
- o->base.type = &exception_type;
- o->id = id;
- o->n_args = 0;
- return o;
+ return mp_obj_new_exception_msg_varg(id, NULL);
}
mp_obj_t mp_obj_new_exception_msg(qstr id, const char *msg) {
- mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, void*, 1);
- o->base.type = &exception_type;
- o->id = id;
- o->n_args = 1;
- o->args[0] = msg;
- return o;
+ return mp_obj_new_exception_msg_varg(id, msg);
}
mp_obj_t mp_obj_new_exception_msg_1_arg(qstr id, const char *fmt, const char *a1) {
- mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, void*, 2);
- o->base.type = &exception_type;
- o->id = id;
- o->n_args = 2;
- o->args[0] = fmt;
- o->args[1] = a1;
- return o;
+ return mp_obj_new_exception_msg_varg(id, fmt, a1);
}
mp_obj_t mp_obj_new_exception_msg_2_args(qstr id, const char *fmt, const char *a1, const char *a2) {
- mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, void*, 3);
- o->base.type = &exception_type;
- o->id = id;
- o->n_args = 3;
- o->args[0] = fmt;
- o->args[1] = a1;
- o->args[2] = a2;
- return o;
+ return mp_obj_new_exception_msg_varg(id, fmt, a1, a2);
}
mp_obj_t mp_obj_new_exception_msg_varg(qstr id, const char *fmt, ...) {
- // count number of arguments by number of % signs, excluding %%
- int n_args = 1; // count fmt
- for (const char *s = fmt; *s; s++) {
- if (*s == '%') {
- if (s[1] == '%') {
- s += 1;
- } else {
- n_args += 1;
- }
- }
- }
-
// make exception object
- mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, void*, n_args);
+ mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, mp_obj_t*, 0);
o->base.type = &exception_type;
o->id = id;
- o->n_args = n_args;
- o->args[0] = fmt;
-
- // extract args and store them
- va_list ap;
- va_start(ap, fmt);
- for (int i = 1; i < n_args; i++) {
- o->args[i] = va_arg(ap, void*);
+ o->args.len = 0;
+ if (fmt == NULL) {
+ o->msg = 0;
+ } else {
+ // render exception message
+ vstr_t *vstr = vstr_new();
+ va_list ap;
+ va_start(ap, fmt);
+ vstr_vprintf(vstr, fmt, ap);
+ va_end(ap);
+ o->msg = qstr_from_str_take(vstr->buf, vstr->alloc);
}
- va_end(ap);
return o;
}
diff --git a/py/objfun.c b/py/objfun.c
index afac3889f..c624cf2d2 100644
--- a/py/objfun.c
+++ b/py/objfun.c
@@ -17,21 +17,44 @@
// mp_obj_fun_native_t defined in obj.h
+void check_nargs(mp_obj_fun_native_t *self, int n_args, int n_kw) {
+ if (n_kw && !self->is_kw) {
+ nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError,
+ "function does not take keyword arguments"));
+ }
+
+ if (self->n_args_min == self->n_args_max) {
+ if (n_args != self->n_args_min) {
+ nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_TypeError,
+ "function takes %d positional arguments but %d were given",
+ (const char*)(machine_int_t)self->n_args_min,
+ (const char*)(machine_int_t)n_args));
+ }
+ } else {
+ if (n_args < self->n_args_min) {
+ nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError,
+ "<fun name>() missing %d required positional arguments: <list of names of params>",
+ (const char*)(machine_int_t)(self->n_args_min - n_args)));
+ } else if (n_args > self->n_args_max) {
+ nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_TypeError,
+ "<fun name> expected at most %d arguments, got %d",
+ (void*)(machine_int_t)self->n_args_max, (void*)(machine_int_t)n_args));
+ }
+ }
+}
+
mp_obj_t fun_native_call_n_kw(mp_obj_t self_in, int n_args, int n_kw, const mp_obj_t *args);
// args are in reverse order in the array
mp_obj_t fun_native_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
mp_obj_fun_native_t *self = self_in;
+ // check number of arguments
+ check_nargs(self, n_args, 0);
if (self->is_kw) {
return fun_native_call_n_kw(self_in, n_args, 0, args);
}
if (self->n_args_min == self->n_args_max) {
// function requires a fixed number of arguments
- // check number of arguments
- if (n_args != self->n_args_min) {
- nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_TypeError, "function takes %d positional arguments but %d were given", (const char*)(machine_int_t)self->n_args_min, (const char*)(machine_int_t)n_args));
- }
-
// dispatch function call
switch (self->n_args_min) {
case 0:
@@ -54,12 +77,6 @@ mp_obj_t fun_native_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
} else {
// function takes a variable number of arguments
- if (n_args < self->n_args_min) {
- nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "<fun name>() missing %d required positional arguments: <list of names of params>", (const char*)(machine_int_t)(self->n_args_min - n_args)));
- } else if (n_args > self->n_args_max) {
- nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_TypeError, "<fun name> expected at most %d arguments, got %d", (void*)(machine_int_t)self->n_args_max, (void*)(machine_int_t)n_args));
- }
-
// TODO really the args need to be passed in as a Python tuple, as the form f(*[1,2]) can be used to pass var args
mp_obj_t *args_ordered = m_new(mp_obj_t, n_args);
for (int i = 0; i < n_args; i++) {
@@ -76,9 +93,7 @@ mp_obj_t fun_native_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
mp_obj_t fun_native_call_n_kw(mp_obj_t self_in, int n_args, int n_kw, const mp_obj_t *args) {
mp_obj_fun_native_t *self = self_in;
- if (!self->is_kw) {
- nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "function does not take keyword arguments"));
- }
+ check_nargs(self, n_args, n_kw);
mp_obj_t *vargs = mp_obj_new_tuple_reverse(n_args, args + 2*n_kw);
mp_map_t *kw_args = mp_map_new(n_kw);
@@ -98,38 +113,13 @@ const mp_obj_type_t fun_native_type = {
.call_n_kw = fun_native_call_n_kw,
};
-mp_obj_t rt_make_function_0(mp_fun_0_t fun) {
- mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
- o->base.type = &fun_native_type;
- o->n_args_min = 0;
- o->n_args_max = 0;
- o->fun = fun;
- return o;
-}
-
-mp_obj_t rt_make_function_1(mp_fun_1_t fun) {
- mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
- o->base.type = &fun_native_type;
- o->n_args_min = 1;
- o->n_args_max = 1;
- o->fun = fun;
- return o;
-}
-
-mp_obj_t rt_make_function_2(mp_fun_2_t fun) {
- mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
- o->base.type = &fun_native_type;
- o->n_args_min = 2;
- o->n_args_max = 2;
- o->fun = fun;
- return o;
-}
-
-mp_obj_t rt_make_function_3(mp_fun_3_t fun) {
+// fun must have the correct signature for n_args fixed arguments
+mp_obj_t rt_make_function_n(int n_args, void *fun) {
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
o->base.type = &fun_native_type;
- o->n_args_min = 3;
- o->n_args_max = 3;
+ o->is_kw = false;
+ o->n_args_min = n_args;
+ o->n_args_max = n_args;
o->fun = fun;
return o;
}
@@ -137,6 +127,7 @@ mp_obj_t rt_make_function_3(mp_fun_3_t fun) {
mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun) {
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
o->base.type = &fun_native_type;
+ o->is_kw = false;
o->n_args_min = n_args_min;
o->n_args_max = ~((machine_uint_t)0);
o->fun = fun;
@@ -147,6 +138,7 @@ mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun) {
mp_obj_t rt_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var_t fun) {
mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
o->base.type = &fun_native_type;
+ o->is_kw = false;
o->n_args_min = n_args_min;
o->n_args_max = n_args_max;
o->fun = fun;
diff --git a/py/objint.c b/py/objint.c
index 26d3c0e33..c0bf756f1 100644
--- a/py/objint.c
+++ b/py/objint.c
@@ -8,16 +8,7 @@
#include "mpconfig.h"
#include "mpqstr.h"
#include "obj.h"
-
-typedef struct _mp_obj_int_t {
- mp_obj_base_t base;
-#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
- mp_longint_impl_t val;
-#endif
-} mp_obj_int_t;
-
-void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in);
-mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in);
+#include "objint.h"
// This dispatcher function is expected to be independent of the implementation
// of long int
@@ -54,11 +45,13 @@ void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj
// This is called only for non-SMALL_INT
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
assert(0);
+ return mp_const_none;
}
// This is called only with strings whose value doesn't fit in SMALL_INT
mp_obj_t mp_obj_new_int_from_long_str(const char *s) {
assert(0);
+ return mp_const_none;
}
mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
@@ -69,6 +62,7 @@ mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
}
// TODO: Raise exception
assert(0);
+ return mp_const_none;
}
mp_obj_t mp_obj_new_int(machine_int_t value) {
@@ -77,5 +71,6 @@ mp_obj_t mp_obj_new_int(machine_int_t value) {
}
// TODO: Raise exception
assert(0);
+ return mp_const_none;
}
#endif
diff --git a/py/objint.h b/py/objint.h
new file mode 100644
index 000000000..14cf977be
--- /dev/null
+++ b/py/objint.h
@@ -0,0 +1,9 @@
+typedef struct _mp_obj_int_t {
+ mp_obj_base_t base;
+#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
+ mp_longint_impl_t val;
+#endif
+} mp_obj_int_t;
+
+void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in);
+mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in);
diff --git a/py/objlist.c b/py/objlist.c
index fa8ec67d0..100a02f3c 100644
--- a/py/objlist.c
+++ b/py/objlist.c
@@ -248,13 +248,14 @@ static void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn, bool r
}
}
-static mp_obj_t list_sort(mp_obj_t args, mp_map_t *kwargs) {
+mp_obj_t mp_obj_list_sort(mp_obj_t args, mp_map_t *kwargs) {
mp_obj_t *args_items = NULL;
uint args_len = 0;
assert(MP_OBJ_IS_TYPE(args, &tuple_type));
mp_obj_tuple_get(args, &args_len, &args_items);
assert(args_len >= 1);
+ assert(MP_OBJ_IS_TYPE(args_items[0], &list_type));
if (args_len > 1) {
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError,
"list.sort takes no positional arguments"));
@@ -380,7 +381,7 @@ static MP_DEFINE_CONST_FUN_OBJ_3(list_insert_obj, list_insert);
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
static MP_DEFINE_CONST_FUN_OBJ_2(list_remove_obj, list_remove);
static MP_DEFINE_CONST_FUN_OBJ_1(list_reverse_obj, list_reverse);
-static MP_DEFINE_CONST_FUN_OBJ_KW(list_sort_obj, list_sort);
+static MP_DEFINE_CONST_FUN_OBJ_KW(list_sort_obj, 0, mp_obj_list_sort);
static const mp_method_t list_type_methods[] = {
{ "append", &list_append_obj },
diff --git a/py/objset.c b/py/objset.c
index e41f2c47f..ba083ada8 100644
--- a/py/objset.c
+++ b/py/objset.c
@@ -45,6 +45,7 @@ void set_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj
print(env, "}");
}
+
static mp_obj_t set_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
switch (n_args) {
case 0:
@@ -405,6 +406,13 @@ static mp_obj_t set_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
return set_issuperset(lhs, rhs);
case RT_COMPARE_OP_NOT_EQUAL:
return MP_BOOL(set_equal(lhs, rhs) == mp_const_false);
+ case RT_COMPARE_OP_IN:
+ case RT_COMPARE_OP_NOT_IN:
+ {
+ mp_obj_set_t *o = lhs;
+ mp_obj_t elem = mp_set_lookup(&o->set, rhs, MP_MAP_LOOKUP);
+ return MP_BOOL((op == RT_COMPARE_OP_IN) ^ (elem == NULL));
+ }
default:
// op not supported
return NULL;
diff --git a/py/objstr.c b/py/objstr.c
index be1f00e68..8b7ab9692 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -85,6 +85,15 @@ mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
return mp_obj_new_str(qstr_from_str_take(val, alloc_len));
}
break;
+ case RT_COMPARE_OP_IN:
+ case RT_COMPARE_OP_NOT_IN:
+ /* NOTE `a in b` is `b.__contains__(a)` */
+ if (MP_OBJ_IS_TYPE(rhs_in, &str_type)) {
+ const char *rhs_str = qstr_str(((mp_obj_str_t*)rhs_in)->qstr);
+ /* FIXME \0 in strs */
+ return MP_BOOL((op == RT_COMPARE_OP_IN) ^ (strstr(lhs_str, rhs_str) == NULL));
+ }
+ break;
}
return MP_OBJ_NULL; // op not supported
@@ -169,8 +178,8 @@ static mp_obj_t str_find(int n_args, const mp_obj_t *args) {
const char* haystack = qstr_str(((mp_obj_str_t*)args[0])->qstr);
const char* needle = qstr_str(((mp_obj_str_t*)args[1])->qstr);
- ssize_t haystack_len = strlen(haystack);
- ssize_t needle_len = strlen(needle);
+ size_t haystack_len = strlen(haystack);
+ size_t needle_len = strlen(needle);
size_t start = 0;
size_t end = haystack_len;
@@ -183,14 +192,17 @@ static mp_obj_t str_find(int n_args, const mp_obj_t *args) {
}
char *p = strstr(haystack + start, needle);
- ssize_t pos = -1;
- if (p) {
- pos = p - haystack;
+ if (p == NULL) {
+ // not found
+ return MP_OBJ_NEW_SMALL_INT(-1);
+ } else {
+ // found
+ machine_int_t pos = p - haystack;
if (pos + needle_len > end) {
pos = -1;
}
+ return MP_OBJ_NEW_SMALL_INT(pos);
}
- return MP_OBJ_NEW_SMALL_INT(pos);
}
mp_obj_t str_strip(int n_args, const mp_obj_t *args) {
diff --git a/py/objtuple.c b/py/objtuple.c
index 7685cc449..15e74636f 100644
--- a/py/objtuple.c
+++ b/py/objtuple.c
@@ -9,19 +9,14 @@
#include "obj.h"
#include "runtime0.h"
#include "runtime.h"
-
-typedef struct _mp_obj_tuple_t {
- mp_obj_base_t base;
- machine_uint_t len;
- mp_obj_t items[];
-} mp_obj_tuple_t;
+#include "objtuple.h"
static mp_obj_t mp_obj_new_tuple_iterator(mp_obj_tuple_t *tuple, int cur);
/******************************************************************************/
/* tuple */
-static void tuple_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
+void tuple_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
mp_obj_tuple_t *o = o_in;
print(env, "(");
for (int i = 0; i < o->len; i++) {
@@ -116,8 +111,10 @@ mp_obj_t mp_obj_new_tuple(uint n, const mp_obj_t *items) {
mp_obj_tuple_t *o = m_new_obj_var(mp_obj_tuple_t, mp_obj_t, n);
o->base.type = &tuple_type;
o->len = n;
- for (int i = 0; i < n; i++) {
- o->items[i] = items[i];
+ if (items) {
+ for (int i = 0; i < n; i++) {
+ o->items[i] = items[i];
+ }
}
return o;
}
@@ -138,8 +135,18 @@ mp_obj_t mp_obj_new_tuple_reverse(uint n, const mp_obj_t *items) {
void mp_obj_tuple_get(mp_obj_t self_in, uint *len, mp_obj_t **items) {
assert(MP_OBJ_IS_TYPE(self_in, &tuple_type));
mp_obj_tuple_t *self = self_in;
- *len = self->len;
- *items = &self->items[0];
+ if (len) {
+ *len = self->len;
+ }
+ if (items) {
+ *items = &self->items[0];
+ }
+}
+
+void mp_obj_tuple_del(mp_obj_t self_in) {
+ assert(MP_OBJ_IS_TYPE(self_in, &tuple_type));
+ mp_obj_tuple_t *self = self_in;
+ m_del_var(mp_obj_tuple_t, mp_obj_t, self->len, self);
}
/******************************************************************************/
diff --git a/py/objtuple.h b/py/objtuple.h
new file mode 100644
index 000000000..9c672fe3f
--- /dev/null
+++ b/py/objtuple.h
@@ -0,0 +1,7 @@
+typedef struct _mp_obj_tuple_t {
+ mp_obj_base_t base;
+ machine_uint_t len;
+ mp_obj_t items[];
+} mp_obj_tuple_t;
+
+void tuple_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in);
diff --git a/py/objzip.c b/py/objzip.c
new file mode 100644
index 000000000..a552ff588
--- /dev/null
+++ b/py/objzip.c
@@ -0,0 +1,57 @@
+#include <stdlib.h>
+#include <assert.h>
+
+#include "misc.h"
+#include "mpconfig.h"
+#include "obj.h"
+#include "runtime.h"
+
+typedef struct _mp_obj_zip_t {
+ mp_obj_base_t base;
+ int n_iters;
+ mp_obj_t iters[];
+} mp_obj_zip_t;
+
+static mp_obj_t zip_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
+ /* NOTE: args are backwards */
+ mp_obj_zip_t *o = m_new_obj_var(mp_obj_zip_t, mp_obj_t, n_args);
+ o->base.type = &zip_type;
+ o->n_iters = n_args;
+ for (int i = 0; i < n_args; i++) {
+ o->iters[i] = rt_getiter(args[n_args-i-1]);
+ }
+ return o;
+}
+
+static mp_obj_t zip_getiter(mp_obj_t self_in) {
+ return self_in;
+}
+
+static mp_obj_t zip_iternext(mp_obj_t self_in) {
+ assert(MP_OBJ_IS_TYPE(self_in, &zip_type));
+ mp_obj_zip_t *self = self_in;
+ mp_obj_t *items;
+ if (self->n_iters == 0) {
+ return mp_const_stop_iteration;
+ }
+ mp_obj_t o = mp_obj_new_tuple(self->n_iters, NULL);
+ mp_obj_tuple_get(o, NULL, &items);
+
+ for (int i = 0; i < self->n_iters; i++) {
+ mp_obj_t next = rt_iternext(self->iters[i]);
+ if (next == mp_const_stop_iteration) {
+ mp_obj_tuple_del(o);
+ return mp_const_stop_iteration;
+ }
+ items[i] = next;
+ }
+ return o;
+}
+
+const mp_obj_type_t zip_type = {
+ { &mp_const_type },
+ "zip",
+ .make_new = zip_make_new,
+ .getiter = zip_getiter,
+ .iternext = zip_iternext,
+};
diff --git a/py/py.mk b/py/py.mk
index a44a8bad0..95f9c0767 100644
--- a/py/py.mk
+++ b/py/py.mk
@@ -97,6 +97,7 @@ PY_O_BASENAME = \
vm.o \
showbc.o \
repl.o \
+ objzip.o \
# prepend the build destination prefix to the py object files
diff --git a/py/runtime.c b/py/runtime.c
index 2af86b6ab..3d8f0c9a7 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -97,7 +97,7 @@ void rt_init(void) {
// built-in core functions
mp_map_add_qstr(&map_builtins, MP_QSTR___build_class__, (mp_obj_t)&mp_builtin___build_class___obj);
- mp_map_add_qstr(&map_builtins, MP_QSTR___repl_print__, rt_make_function_1(mp_builtin___repl_print__));
+ mp_map_add_qstr(&map_builtins, MP_QSTR___repl_print__, (mp_obj_t)&mp_builtin___repl_print___obj);
// built-in types
mp_map_add_qstr(&map_builtins, MP_QSTR_bool, (mp_obj_t)&bool_type);
@@ -113,27 +113,29 @@ void rt_init(void) {
mp_map_add_qstr(&map_builtins, MP_QSTR_set, (mp_obj_t)&set_type);
mp_map_add_qstr(&map_builtins, MP_QSTR_tuple, (mp_obj_t)&tuple_type);
mp_map_add_qstr(&map_builtins, MP_QSTR_type, (mp_obj_t)&mp_const_type);
-
- // built-in user functions; TODO covert all to &mp_builtin_xxx's
- mp_map_add_qstr(&map_builtins, MP_QSTR_abs, rt_make_function_1(mp_builtin_abs));
- mp_map_add_qstr(&map_builtins, MP_QSTR_all, rt_make_function_1(mp_builtin_all));
- mp_map_add_qstr(&map_builtins, MP_QSTR_any, rt_make_function_1(mp_builtin_any));
- mp_map_add_qstr(&map_builtins, MP_QSTR_callable, rt_make_function_1(mp_builtin_callable));
- mp_map_add_qstr(&map_builtins, MP_QSTR_chr, rt_make_function_1(mp_builtin_chr));
- mp_map_add_qstr(&map_builtins, MP_QSTR_divmod, rt_make_function_2(mp_builtin_divmod));
+ mp_map_add_qstr(&map_builtins, MP_QSTR_zip, (mp_obj_t)&zip_type);
+
+ // built-in user functions
+ mp_map_add_qstr(&map_builtins, MP_QSTR_abs, (mp_obj_t)&mp_builtin_abs_obj);
+ mp_map_add_qstr(&map_builtins, MP_QSTR_all, (mp_obj_t)&mp_builtin_all_obj);
+ mp_map_add_qstr(&map_builtins, MP_QSTR_any, (mp_obj_t)&mp_builtin_any_obj);
+ mp_map_add_qstr(&map_builtins, MP_QSTR_callable, (mp_obj_t)&mp_builtin_callable_obj);
+ mp_map_add_qstr(&map_builtins, MP_QSTR_chr, (mp_obj_t)&mp_builtin_chr_obj);
+ mp_map_add_qstr(&map_builtins, MP_QSTR_divmod, (mp_obj_t)&mp_builtin_divmod_obj);
mp_map_add_qstr(&map_builtins, MP_QSTR_hash, (mp_obj_t)&mp_builtin_hash_obj);
mp_map_add_qstr(&map_builtins, MP_QSTR_isinstance, (mp_obj_t)&mp_builtin_isinstance_obj);
mp_map_add_qstr(&map_builtins, MP_QSTR_issubclass, (mp_obj_t)&mp_builtin_issubclass_obj);
mp_map_add_qstr(&map_builtins, MP_QSTR_iter, (mp_obj_t)&mp_builtin_iter_obj);
- mp_map_add_qstr(&map_builtins, MP_QSTR_len, rt_make_function_1(mp_builtin_len));
- mp_map_add_qstr(&map_builtins, MP_QSTR_max, rt_make_function_var(1, mp_builtin_max));
- mp_map_add_qstr(&map_builtins, MP_QSTR_min, rt_make_function_var(1, mp_builtin_min));
+ mp_map_add_qstr(&map_builtins, MP_QSTR_len, (mp_obj_t)&mp_builtin_len_obj);
+ mp_map_add_qstr(&map_builtins, MP_QSTR_max, (mp_obj_t)&mp_builtin_max_obj);
+ mp_map_add_qstr(&map_builtins, MP_QSTR_min, (mp_obj_t)&mp_builtin_min_obj);
mp_map_add_qstr(&map_builtins, MP_QSTR_next, (mp_obj_t)&mp_builtin_next_obj);
- mp_map_add_qstr(&map_builtins, MP_QSTR_ord, rt_make_function_1(mp_builtin_ord));
- mp_map_add_qstr(&map_builtins, MP_QSTR_pow, rt_make_function_var(2, mp_builtin_pow));
- mp_map_add_qstr(&map_builtins, MP_QSTR_print, rt_make_function_var(0, mp_builtin_print));
- mp_map_add_qstr(&map_builtins, MP_QSTR_range, rt_make_function_var(1, mp_builtin_range));
- mp_map_add_qstr(&map_builtins, MP_QSTR_sum, rt_make_function_var(1, mp_builtin_sum));
+ mp_map_add_qstr(&map_builtins, MP_QSTR_ord, (mp_obj_t)&mp_builtin_ord_obj);
+ mp_map_add_qstr(&map_builtins, MP_QSTR_pow, (mp_obj_t)&mp_builtin_pow_obj);
+ mp_map_add_qstr(&map_builtins, MP_QSTR_print, (mp_obj_t)&mp_builtin_print_obj);
+ mp_map_add_qstr(&map_builtins, MP_QSTR_range, (mp_obj_t)&mp_builtin_range_obj);
+ mp_map_add_qstr(&map_builtins, MP_QSTR_sorted, (mp_obj_t)&mp_builtin_sorted_obj);
+ mp_map_add_qstr(&map_builtins, MP_QSTR_sum, (mp_obj_t)&mp_builtin_sum_obj);
next_unique_code_id = 1; // 0 indicates "no code"
unique_codes_alloc = 0;
@@ -461,6 +463,18 @@ mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
// then fail
// note that list does not implement + or +=, so that inplace_concat is reached first for +=
+ // deal with is, is not
+ if (op == RT_COMPARE_OP_IS) {
+ // TODO: may need to handle strings specially, CPython appears to
+ // assume all strings are interned (so "is" == "==" for strings)
+ return MP_BOOL(lhs == rhs);
+ }
+ if (op == RT_COMPARE_OP_IS_NOT) {
+ // TODO: may need to handle strings specially, CPython appears to
+ // assume all strings are interned (so "is" == "==" for strings)
+ return MP_BOOL(lhs != rhs);
+ }
+
// deal with == and != for all types
if (op == RT_COMPARE_OP_EQUAL || op == RT_COMPARE_OP_NOT_EQUAL) {
if (mp_obj_equal(lhs, rhs)) {
@@ -554,22 +568,57 @@ mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
} else if (MP_OBJ_IS_TYPE(rhs, &complex_type)) {
return mp_obj_complex_binary_op(op, lhs_val, 0, rhs);
}
- } else {
- if (MP_OBJ_IS_OBJ(lhs)) {
- mp_obj_base_t *o = lhs;
+ }
+
+ /* deal with `in` and `not in`
+ *
+ * NOTE `a in b` is `b.__contains__(a)`, hence why the generic dispatch
+ * needs to go below
+ */
+ if (op == RT_COMPARE_OP_IN || op == RT_COMPARE_OP_NOT_IN) {
+ if (!MP_OBJ_IS_SMALL_INT(rhs)) {
+ mp_obj_base_t *o = rhs;
if (o->type->binary_op != NULL) {
- mp_obj_t result = o->type->binary_op(op, lhs, rhs);
- if (result != NULL) {
- return result;
+ mp_obj_t res = o->type->binary_op(op, rhs, lhs);
+ if (res != NULL) {
+ return res;
+ }
+ }
+ if (o->type->getiter != NULL) {
+ /* second attempt, walk the iterator */
+ mp_obj_t next = NULL;
+ mp_obj_t iter = rt_getiter(rhs);
+ while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
+ if (mp_obj_equal(next, lhs)) {
+ return MP_BOOL(op == RT_COMPARE_OP_IN);
+ }
}
+ return MP_BOOL(op != RT_COMPARE_OP_IN);
}
}
+
+ nlr_jump(mp_obj_new_exception_msg_varg(
+ MP_QSTR_TypeError, "'%s' object is not iterable",
+ mp_obj_get_type_str(rhs)));
+ return mp_const_none;
+ }
+
+ if (MP_OBJ_IS_OBJ(lhs)) {
+ mp_obj_base_t *o = lhs;
+ if (o->type->binary_op != NULL) {
+ mp_obj_t result = o->type->binary_op(op, lhs, rhs);
+ if (result != NULL) {
+ return result;
+ }
+ }
+ // TODO implement dispatch for reverse binary ops
}
// TODO specify in error message what the operator is
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError,
"unsupported operand types for binary operator: '%s', '%s'",
mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
+ return mp_const_none;
}
mp_obj_t rt_make_function_from_id(int unique_code_id) {
@@ -587,12 +636,7 @@ mp_obj_t rt_make_function_from_id(int unique_code_id) {
fun = mp_obj_new_fun_bc(c->n_args, c->n_locals + c->n_stack, c->u_byte.code);
break;
case MP_CODE_NATIVE:
- switch (c->n_args) {
- case 0: fun = rt_make_function_0(c->u_native.fun); break;
- case 1: fun = rt_make_function_1((mp_fun_1_t)c->u_native.fun); break;
- case 2: fun = rt_make_function_2((mp_fun_2_t)c->u_native.fun); break;
- default: assert(0); fun = mp_const_none;
- }
+ fun = rt_make_function_n(c->n_args, c->u_native.fun);
break;
case MP_CODE_INLINE_ASM:
fun = mp_obj_new_fun_asm(c->n_args, c->u_inline_asm.fun);
diff --git a/py/runtime.h b/py/runtime.h
index ac53e1411..32cb47684 100644
--- a/py/runtime.h
+++ b/py/runtime.h
@@ -12,10 +12,8 @@ void rt_store_global(qstr qstr, mp_obj_t obj);
mp_obj_t rt_unary_op(int op, mp_obj_t arg);
mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs);
mp_obj_t rt_make_function_from_id(int unique_code_id);
-mp_obj_t rt_make_function_0(mp_fun_0_t f);
-mp_obj_t rt_make_function_1(mp_fun_1_t f);
-mp_obj_t rt_make_function_2(mp_fun_2_t f);
-mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t f);
+mp_obj_t rt_make_function_n(int n_args, void *fun); // fun must have the correct signature for n_args fixed arguments
+mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun);
mp_obj_t rt_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var_t fun); // min and max are inclusive
mp_obj_t rt_make_closure_from_id(int unique_code_id, mp_obj_t closure_tuple);
mp_obj_t rt_call_function_0(mp_obj_t fun);
diff --git a/py/stream.c b/py/stream.c
index f883efcd2..48cd0bcd6 100644
--- a/py/stream.c
+++ b/py/stream.c
@@ -51,5 +51,47 @@ static mp_obj_t stream_write(mp_obj_t self_in, mp_obj_t arg) {
}
}
+// TODO: should be in mpconfig.h
+#define READ_SIZE 256
+static mp_obj_t stream_readall(mp_obj_t self_in) {
+ struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in;
+ if (o->type->stream_p.read == NULL) {
+ // CPython: io.UnsupportedOperation, OSError subclass
+ nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OSError, "Operation not supported"));
+ }
+
+ int total_size = 0;
+ vstr_t *vstr = vstr_new_size(READ_SIZE);
+ char *buf = vstr_str(vstr);
+ char *p = buf;
+ int error;
+ int current_read = READ_SIZE;
+ while (true) {
+ machine_int_t out_sz = o->type->stream_p.read(self_in, p, current_read, &error);
+ if (out_sz == -1) {
+ nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "[Errno %d]", error));
+ }
+ if (out_sz == 0) {
+ break;
+ }
+ total_size += out_sz;
+ if (out_sz < current_read) {
+ current_read -= out_sz;
+ p += out_sz;
+ } else {
+ current_read = READ_SIZE;
+ p = vstr_extend(vstr, current_read);
+ if (p == NULL) {
+ // TODO
+ nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError/*MP_QSTR_RuntimeError*/, "Out of memory"));
+ }
+ }
+ }
+ vstr_set_size(vstr, total_size + 1); // TODO: for \0
+ buf[total_size] = 0;
+ return mp_obj_new_str(qstr_from_str_take(buf, total_size + 1));
+}
+
MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_read_obj, stream_read);
+MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall);
MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write);
diff --git a/py/stream.h b/py/stream.h
index 8a579b762..b1c3c7278 100644
--- a/py/stream.h
+++ b/py/stream.h
@@ -1,2 +1,3 @@
extern const mp_obj_fun_native_t mp_stream_read_obj;
+extern const mp_obj_fun_native_t mp_stream_readall_obj;
extern const mp_obj_fun_native_t mp_stream_write_obj;
diff --git a/py/vm.c b/py/vm.c
index baef6988c..5c74b43ca 100644
--- a/py/vm.c
+++ b/py/vm.c
@@ -440,10 +440,17 @@ bool mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t **
case MP_BC_CALL_FUNCTION:
DECODE_UINT;
- assert((unum & 0xff00) == 0); // n_keyword
- unum &= 0xff; // n_positional
- sp += unum;
- *sp = rt_call_function_n(*sp, unum, sp - unum);
+ if ((unum & 0xff00) == 0) {
+ // no keywords
+ unum &= 0xff; // n_positional
+ sp += unum;
+ *sp = rt_call_function_n(*sp, unum, sp - unum);
+ } else {
+ // keywords
+ int argsize = (unum & 0xff) + ((unum >> 7) & 0x1fe);
+ sp += argsize;
+ *sp = rt_call_function_n_kw(*sp, unum & 0xff, (unum >> 8) & 0xff, sp - argsize);
+ }
break;
case MP_BC_CALL_METHOD:
diff --git a/py/vstr.c b/py/vstr.c
index 80841b24c..18e6d2d0a 100644
--- a/py/vstr.c
+++ b/py/vstr.c
@@ -6,8 +6,8 @@
// returned value is always at least 1 greater than argument
#define ROUND_ALLOC(a) (((a) & ((~0) - 7)) + 8)
-void vstr_init(vstr_t *vstr) {
- vstr->alloc = 32;
+void vstr_init(vstr_t *vstr, int alloc) {
+ vstr->alloc = alloc;
vstr->len = 0;
vstr->buf = m_new(char, vstr->alloc);
if (vstr->buf == NULL) {
@@ -28,7 +28,16 @@ vstr_t *vstr_new(void) {
if (vstr == NULL) {
return NULL;
}
- vstr_init(vstr);
+ vstr_init(vstr, 32);
+ return vstr;
+}
+
+vstr_t *vstr_new_size(int alloc) {
+ vstr_t *vstr = m_new(vstr_t, 1);
+ if (vstr == NULL) {
+ return NULL;
+ }
+ vstr_init(vstr, alloc);
return vstr;
}
@@ -63,6 +72,36 @@ int vstr_len(vstr_t *vstr) {
return vstr->len;
}
+// Extend vstr strictly to by requested size, return pointer to newly added chunk
+char *vstr_extend(vstr_t *vstr, int size) {
+ char *new_buf = m_renew(char, vstr->buf, vstr->alloc, vstr->alloc + size);
+ if (new_buf == NULL) {
+ vstr->had_error = true;
+ return NULL;
+ }
+ char *p = new_buf + vstr->alloc;
+ vstr->alloc += size;
+ vstr->buf = new_buf;
+ return p;
+}
+
+// Shrink vstr to be given size
+bool vstr_set_size(vstr_t *vstr, int size) {
+ char *new_buf = m_renew(char, vstr->buf, vstr->alloc, size);
+ if (new_buf == NULL) {
+ vstr->had_error = true;
+ return false;
+ }
+ vstr->buf = new_buf;
+ vstr->alloc = vstr->len = size;
+ return true;
+}
+
+// Shrink vstr allocation to its actual length
+bool vstr_shrink(vstr_t *vstr) {
+ return vstr_set_size(vstr, vstr->len);
+}
+
bool vstr_ensure_extra(vstr_t *vstr, int size) {
if (vstr->len + size + 1 > vstr->alloc) {
int new_alloc = ROUND_ALLOC((vstr->len + size + 1) * 2);