summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
Diffstat (limited to 'py')
-rw-r--r--py/compile.c2
-rw-r--r--py/emitpass1.c5
-rw-r--r--py/gc.c7
-rw-r--r--py/lexer.c11
-rw-r--r--py/lexer.h27
-rw-r--r--py/map.c67
-rw-r--r--py/map.h10
-rw-r--r--py/mpconfig.h17
-rw-r--r--py/obj.c11
-rw-r--r--py/obj.h28
-rw-r--r--py/objdict.c120
-rw-r--r--py/objint.c44
-rw-r--r--py/objlist.c92
-rw-r--r--py/objset.c388
-rw-r--r--py/objstr.c39
-rw-r--r--py/objtype.c74
-rw-r--r--py/parse.c54
-rw-r--r--py/runtime.c33
18 files changed, 863 insertions, 166 deletions
diff --git a/py/compile.c b/py/compile.c
index 2e22d64262..b948f7aa47 100644
--- a/py/compile.c
+++ b/py/compile.c
@@ -1316,7 +1316,7 @@ void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
int l_end = comp_next_label(comp);
c_if_cond(comp, pns->nodes[0], true, l_end);
- EMIT(load_id, MP_QSTR_AssertionError);
+ EMIT(load_global, MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
// assertion message
compile_node(comp, pns->nodes[1]);
diff --git a/py/emitpass1.c b/py/emitpass1.c
index 2f0ecac86d..60fdd0b825 100644
--- a/py/emitpass1.c
+++ b/py/emitpass1.c
@@ -45,10 +45,7 @@ static void emit_pass1_load_id(emit_t *emit, qstr qstr) {
bool added;
id_info_t *id = scope_find_or_add_id(emit->scope, qstr, &added);
if (added) {
- if (qstr == MP_QSTR_AssertionError) {
- // TODO how much of a hack is this?
- id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
- } else if (strcmp(qstr_str(qstr), "super") == 0 && emit->scope->kind == SCOPE_FUNCTION) {
+ if (strcmp(qstr_str(qstr), "super") == 0 && emit->scope->kind == SCOPE_FUNCTION) {
// special case, super is a global, and also counts as use of __class__
id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
id_info_t *id2 = scope_find_local_in_parent(emit->scope, emit->qstr___class__);
diff --git a/py/gc.c b/py/gc.c
index 054a3cc315..70b071ebc8 100644
--- a/py/gc.c
+++ b/py/gc.c
@@ -8,15 +8,8 @@
#if MICROPY_ENABLE_GC
-// a machine word is big enough to hold a pointer
-/*
-#define BYTES_PER_WORD (8)
-typedef unsigned long machine_uint_t;
-*/
typedef unsigned char byte;
-#define BITS_PER_BYTE (8)
-#define BITS_PER_WORD (BITS_PER_BYTE * BYTES_PER_WORD)
#define WORDS_PER_BLOCK (4)
#define BYTES_PER_BLOCK (WORDS_PER_BLOCK * BYTES_PER_WORD)
#define STACK_SIZE (64) // tunable; minimum is 1
diff --git a/py/lexer.c b/py/lexer.c
index d4205236c3..f7f9c631f3 100644
--- a/py/lexer.c
+++ b/py/lexer.c
@@ -299,8 +299,15 @@ static void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs
// backslash (outside string literals) must appear just before a physical newline
next_char(lex);
if (!is_physical_newline(lex)) {
- // TODO SyntaxError
- assert(0);
+ // SyntaxError: unexpected character after line continuation character
+ tok->src_name = lex->name;
+ tok->src_line = lex->line;
+ tok->src_column = lex->column;
+ tok->kind = MP_TOKEN_BAD_LINE_CONTINUATION;
+ vstr_reset(&lex->vstr);
+ tok->str = vstr_str(&lex->vstr);
+ tok->len = 0;
+ return;
} else {
next_char(lex);
}
diff --git a/py/lexer.h b/py/lexer.h
index 9dfcb128c5..428ff03c5b 100644
--- a/py/lexer.h
+++ b/py/lexer.h
@@ -10,19 +10,20 @@ typedef enum _mp_token_kind_t {
MP_TOKEN_INVALID,
MP_TOKEN_DEDENT_MISMATCH,
MP_TOKEN_LONELY_STRING_OPEN,
+ MP_TOKEN_BAD_LINE_CONTINUATION,
- MP_TOKEN_NEWLINE, // 4
- MP_TOKEN_INDENT, // 5
- MP_TOKEN_DEDENT, // 6
+ MP_TOKEN_NEWLINE, // 5
+ MP_TOKEN_INDENT, // 6
+ MP_TOKEN_DEDENT, // 7
- MP_TOKEN_NAME, // 7
+ MP_TOKEN_NAME, // 8
MP_TOKEN_NUMBER,
MP_TOKEN_STRING,
MP_TOKEN_BYTES,
MP_TOKEN_ELLIPSIS,
- MP_TOKEN_KW_FALSE, // 12
+ MP_TOKEN_KW_FALSE, // 13
MP_TOKEN_KW_NONE,
MP_TOKEN_KW_TRUE,
MP_TOKEN_KW_AND,
@@ -31,7 +32,7 @@ typedef enum _mp_token_kind_t {
MP_TOKEN_KW_BREAK,
MP_TOKEN_KW_CLASS,
MP_TOKEN_KW_CONTINUE,
- MP_TOKEN_KW_DEF, // 21
+ MP_TOKEN_KW_DEF, // 22
MP_TOKEN_KW_DEL,
MP_TOKEN_KW_ELIF,
MP_TOKEN_KW_ELSE,
@@ -41,7 +42,7 @@ typedef enum _mp_token_kind_t {
MP_TOKEN_KW_FROM,
MP_TOKEN_KW_GLOBAL,
MP_TOKEN_KW_IF,
- MP_TOKEN_KW_IMPORT, // 31
+ MP_TOKEN_KW_IMPORT, // 32
MP_TOKEN_KW_IN,
MP_TOKEN_KW_IS,
MP_TOKEN_KW_LAMBDA,
@@ -51,12 +52,12 @@ typedef enum _mp_token_kind_t {
MP_TOKEN_KW_PASS,
MP_TOKEN_KW_RAISE,
MP_TOKEN_KW_RETURN,
- MP_TOKEN_KW_TRY, // 41
+ MP_TOKEN_KW_TRY, // 42
MP_TOKEN_KW_WHILE,
MP_TOKEN_KW_WITH,
MP_TOKEN_KW_YIELD,
- MP_TOKEN_OP_PLUS, // 45
+ MP_TOKEN_OP_PLUS, // 46
MP_TOKEN_OP_MINUS,
MP_TOKEN_OP_STAR,
MP_TOKEN_OP_DBL_STAR,
@@ -66,7 +67,7 @@ typedef enum _mp_token_kind_t {
MP_TOKEN_OP_LESS,
MP_TOKEN_OP_DBL_LESS,
MP_TOKEN_OP_MORE,
- MP_TOKEN_OP_DBL_MORE, // 55
+ MP_TOKEN_OP_DBL_MORE, // 56
MP_TOKEN_OP_AMPERSAND,
MP_TOKEN_OP_PIPE,
MP_TOKEN_OP_CARET,
@@ -76,7 +77,7 @@ typedef enum _mp_token_kind_t {
MP_TOKEN_OP_DBL_EQUAL,
MP_TOKEN_OP_NOT_EQUAL,
- MP_TOKEN_DEL_PAREN_OPEN, // 64
+ MP_TOKEN_DEL_PAREN_OPEN, // 65
MP_TOKEN_DEL_PAREN_CLOSE,
MP_TOKEN_DEL_BRACKET_OPEN,
MP_TOKEN_DEL_BRACKET_CLOSE,
@@ -86,7 +87,7 @@ typedef enum _mp_token_kind_t {
MP_TOKEN_DEL_COLON,
MP_TOKEN_DEL_PERIOD,
MP_TOKEN_DEL_SEMICOLON,
- MP_TOKEN_DEL_AT, // 74
+ MP_TOKEN_DEL_AT, // 75
MP_TOKEN_DEL_EQUAL,
MP_TOKEN_DEL_PLUS_EQUAL,
MP_TOKEN_DEL_MINUS_EQUAL,
@@ -96,7 +97,7 @@ typedef enum _mp_token_kind_t {
MP_TOKEN_DEL_PERCENT_EQUAL,
MP_TOKEN_DEL_AMPERSAND_EQUAL,
MP_TOKEN_DEL_PIPE_EQUAL,
- MP_TOKEN_DEL_CARET_EQUAL, // 84
+ MP_TOKEN_DEL_CARET_EQUAL, // 85
MP_TOKEN_DEL_DBL_MORE_EQUAL,
MP_TOKEN_DEL_DBL_LESS_EQUAL,
MP_TOKEN_DEL_DBL_STAR_EQUAL,
diff --git a/py/map.c b/py/map.c
index e44cf33d26..1ce763ab0e 100644
--- a/py/map.c
+++ b/py/map.c
@@ -132,28 +132,45 @@ void mp_set_init(mp_set_t *set, int n) {
set->table = m_new0(mp_obj_t, set->alloc);
}
-mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, bool add_if_not_found) {
- int hash = mp_obj_hash(index);
- assert(set->alloc); /* FIXME: if alloc is ever 0 when doing a lookup, this'll fail: */
- int pos = hash % set->alloc;
+static void mp_set_rehash(mp_set_t *set) {
+ int old_alloc = set->alloc;
+ mp_obj_t *old_table = set->table;
+ set->alloc = get_doubling_prime_greater_or_equal_to(set->alloc + 1);
+ set->used = 0;
+ set->table = m_new0(mp_obj_t, set->alloc);
+ for (int i = 0; i < old_alloc; i++) {
+ if (old_table[i] != NULL) {
+ mp_set_lookup(set, old_table[i], true);
+ }
+ }
+ m_del(mp_obj_t, old_table, old_alloc);
+}
+
+mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
+ int hash;
+ int pos;
+ if (set->alloc == 0) {
+ if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
+ mp_set_rehash(set);
+ } else {
+ return NULL;
+ }
+ }
+ if (lookup_kind & MP_MAP_LOOKUP_FIRST) {
+ hash = 0;
+ pos = 0;
+ } else {
+ hash = mp_obj_hash(index);;
+ pos = hash % set->alloc;
+ }
for (;;) {
mp_obj_t elem = set->table[pos];
if (elem == MP_OBJ_NULL) {
// not in table
- if (add_if_not_found) {
+ if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
if (set->used + 1 >= set->alloc) {
// not enough room in table, rehash it
- int old_alloc = set->alloc;
- mp_obj_t *old_table = set->table;
- set->alloc = get_doubling_prime_greater_or_equal_to(set->alloc + 1);
- set->used = 0;
- set->table = m_new(mp_obj_t, set->alloc);
- for (int i = 0; i < old_alloc; i++) {
- if (old_table[i] != NULL) {
- mp_set_lookup(set, old_table[i], true);
- }
- }
- m_del(mp_obj_t, old_table, old_alloc);
+ mp_set_rehash(set);
// restart the search for the new element
pos = hash % set->alloc;
} else {
@@ -161,11 +178,17 @@ mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, bool add_if_not_found) {
set->table[pos] = index;
return index;
}
+ } else if (lookup_kind & MP_MAP_LOOKUP_FIRST) {
+ pos++;
} else {
return MP_OBJ_NULL;
}
- } else if (mp_obj_equal(elem, index)) {
+ } else if (lookup_kind & MP_MAP_LOOKUP_FIRST || mp_obj_equal(elem, index)) {
// found it
+ if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
+ set->used--;
+ set->table[pos] = NULL;
+ }
return elem;
} else {
// not yet found, keep searching in this table
@@ -173,3 +196,13 @@ mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, bool add_if_not_found) {
}
}
}
+
+void mp_set_clear(mp_set_t *set) {
+ set->used = 0;
+ machine_uint_t a = set->alloc;
+ set->alloc = 0;
+ set->table = m_renew(mp_obj_t, set->table, a, set->alloc);
+ for (uint i=0; i<set->alloc; i++) {
+ set->table[i] = NULL;
+ }
+}
diff --git a/py/map.h b/py/map.h
index 5ce4e835b6..2db0ac3ebc 100644
--- a/py/map.h
+++ b/py/map.h
@@ -19,9 +19,10 @@ typedef struct _mp_set_t {
} mp_set_t;
typedef enum _mp_map_lookup_kind_t {
- MP_MAP_LOOKUP,
- MP_MAP_LOOKUP_ADD_IF_NOT_FOUND,
- MP_MAP_LOOKUP_REMOVE_IF_FOUND,
+ MP_MAP_LOOKUP, // 0
+ MP_MAP_LOOKUP_ADD_IF_NOT_FOUND, // 1
+ MP_MAP_LOOKUP_REMOVE_IF_FOUND, // 2
+ MP_MAP_LOOKUP_FIRST = 4,
} mp_map_lookup_kind_t;
int get_doubling_prime_greater_or_equal_to(int x);
@@ -31,4 +32,5 @@ mp_map_elem_t* mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t
void mp_map_clear(mp_map_t *map);
void mp_set_init(mp_set_t *set, int n);
-mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, bool add_if_not_found);
+mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
+void mp_set_clear(mp_set_t *set);
diff --git a/py/mpconfig.h b/py/mpconfig.h
index 2017ba366a..ada4aa2ea4 100644
--- a/py/mpconfig.h
+++ b/py/mpconfig.h
@@ -62,6 +62,18 @@
#define MICROPY_ENABLE_LEXER_UNIX (0)
#endif
+// Long int implementation
+#define MICROPY_LONGINT_IMPL_NONE (0)
+#define MICROPY_LONGINT_IMPL_LONGLONG (1)
+
+#ifndef MICROPY_LONGINT_IMPL
+#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_NONE)
+#endif
+
+#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
+typedef long long mp_longint_impl_t;
+#endif
+
// Whether to support float and complex types
#ifndef MICROPY_ENABLE_FLOAT
#define MICROPY_ENABLE_FLOAT (0)
@@ -76,6 +88,11 @@
/*****************************************************************************/
/* Miscellaneous settings */
+#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))
+
// printf format spec to use for machine_int_t and friends
#ifndef INT_FMT
#ifdef __LP64__
diff --git a/py/obj.c b/py/obj.c
index dfb450fb8d..2759437fd7 100644
--- a/py/obj.c
+++ b/py/obj.c
@@ -117,6 +117,13 @@ bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
} else if (MP_OBJ_IS_TYPE(o1, &str_type) && MP_OBJ_IS_TYPE(o2, &str_type)) {
return mp_obj_str_get(o1) == mp_obj_str_get(o2);
} else {
+ mp_obj_base_t *o = o1;
+ if (o->type->binary_op != NULL) {
+ mp_obj_t r = o->type->binary_op(RT_COMPARE_OP_EQUAL, o1, o2);
+ if (r != MP_OBJ_NULL) {
+ return r == mp_const_true ? true : false;
+ }
+ }
// TODO: Debugging helper
printf("Equality for '%s' and '%s' types not yet implemented\n", mp_obj_get_type_str(o1), mp_obj_get_type_str(o2));
assert(0);
@@ -231,7 +238,7 @@ uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index)
}
}
-// may return NULL
+// may return MP_OBJ_NULL
mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
mp_small_int_t len = 0;
if (MP_OBJ_IS_TYPE(o_in, &str_type)) {
@@ -249,7 +256,7 @@ mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
} else if (MP_OBJ_IS_TYPE(o_in, &dict_type)) {
len = mp_obj_dict_len(o_in);
} else {
- return NULL;
+ return MP_OBJ_NULL;
}
return MP_OBJ_NEW_SMALL_INT(len);
}
diff --git a/py/obj.h b/py/obj.h
index 8d236008ee..a9a5827ee7 100644
--- a/py/obj.h
+++ b/py/obj.h
@@ -34,6 +34,8 @@ typedef struct _mp_obj_base_t mp_obj_base_t;
// - xxxx...xx10: a qstr, bits 2 and above are the value
// - xxxx...xx00: a pointer to an mp_obj_base_t
+// In SMALL_INT, next-to-highest bits is used as sign, so both must match for value in range
+#define MP_OBJ_FITS_SMALL_INT(n) ((((n) ^ ((n) << 1)) & WORD_MSBIT_HIGH) == 0)
#define MP_OBJ_IS_SMALL_INT(o) ((((mp_small_int_t)(o)) & 1) != 0)
#define MP_OBJ_IS_QSTR(o) ((((mp_small_int_t)(o)) & 3) == 2)
#define MP_OBJ_IS_OBJ(o) ((((mp_small_int_t)(o)) & 3) == 0)
@@ -59,6 +61,15 @@ typedef struct _mp_obj_base_t mp_obj_base_t;
#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)
+// 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
+
+#define MP_DECLARE_CONST_STATICMETHOD_OBJ(obj_name) extern const mp_obj_staticmethod_t obj_name
+#define MP_DECLARE_CONST_CLASSMETHOD_OBJ(obj_name) extern const mp_obj_classmethod_t obj_name
+
+#define MP_DEFINE_CONST_STATICMETHOD_OBJ(obj_name, fun_name) const mp_obj_staticmethod_t obj_name = {{&mp_type_staticmethod}, fun_name}
+#define MP_DEFINE_CONST_CLASSMETHOD_OBJ(obj_name, fun_name) const mp_obj_classmethod_t obj_name = {{&mp_type_classmethod}, fun_name}
+
// Need to declare this here so we are not dependent on map.h
struct _mp_map_t;
struct _mp_map_elem_t;
@@ -187,6 +198,8 @@ mp_obj_t mp_obj_new_none(void);
mp_obj_t mp_obj_new_bool(bool value);
mp_obj_t mp_obj_new_cell(mp_obj_t obj);
mp_obj_t mp_obj_new_int(machine_int_t value);
+mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value);
+mp_obj_t mp_obj_new_int_from_long_str(const char *s);
mp_obj_t mp_obj_new_str(qstr qstr);
#if MICROPY_ENABLE_FLOAT
mp_obj_t mp_obj_new_float(mp_float_t val);
@@ -316,3 +329,18 @@ extern const mp_obj_type_t gen_instance_type;
extern const mp_obj_type_t module_type;
mp_obj_t mp_obj_new_module(qstr module_name);
struct _mp_map_t *mp_obj_module_get_globals(mp_obj_t self_in);
+
+// staticmethod and classmethod types; defined here so we can make const versions
+
+extern const mp_obj_type_t mp_type_staticmethod;
+extern const mp_obj_type_t mp_type_classmethod;
+
+typedef struct _mp_obj_staticmethod_t {
+ mp_obj_base_t base;
+ mp_obj_t fun;
+} mp_obj_staticmethod_t;
+
+typedef struct _mp_obj_classmethod_t {
+ mp_obj_base_t base;
+ mp_obj_t fun;
+} mp_obj_classmethod_t;
diff --git a/py/objdict.c b/py/objdict.c
index 5f8a04d05e..e164ed74ca 100644
--- a/py/objdict.c
+++ b/py/objdict.c
@@ -145,6 +145,35 @@ static mp_obj_t dict_copy(mp_obj_t self_in) {
}
static MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy);
+// this is a classmethod
+static mp_obj_t dict_fromkeys(int n_args, const mp_obj_t *args) {
+ assert(2 <= n_args && n_args <= 3);
+ mp_obj_t iter = rt_getiter(args[1]);
+ mp_obj_t len = mp_obj_len_maybe(iter);
+ mp_obj_t value = mp_const_none;
+ mp_obj_t next = NULL;
+ mp_obj_dict_t *self = NULL;
+
+ if (n_args > 2) {
+ value = args[2];
+ }
+
+ if (len == MP_OBJ_NULL) {
+ /* object's type doesn't have a __len__ slot */
+ self = mp_obj_new_dict(0);
+ } else {
+ self = mp_obj_new_dict(MP_OBJ_SMALL_INT_VALUE(len));
+ }
+
+ while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
+ mp_map_lookup(&self->map, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
+ }
+
+ return self;
+}
+static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_fromkeys_fun_obj, 2, 3, dict_fromkeys);
+static MP_DEFINE_CONST_CLASSMETHOD_OBJ(dict_fromkeys_obj, (const mp_obj_t)&dict_fromkeys_fun_obj);
+
static mp_obj_t dict_get_helper(mp_map_t *self, mp_obj_t key, mp_obj_t deflt, mp_map_lookup_kind_t lookup_kind) {
mp_map_elem_t *elem = mp_map_lookup(self, key, lookup_kind);
mp_obj_t value;
@@ -286,23 +315,18 @@ static mp_obj_t dict_view_it_iternext(mp_obj_t self_in) {
if (next != NULL) {
switch (self->kind) {
- case MP_DICT_VIEW_ITEMS:
- {
- mp_obj_t items[] = {next->key, next->value};
- return mp_obj_new_tuple(2, items);
- }
- case MP_DICT_VIEW_KEYS:
- {
- return next->key;
- }
- case MP_DICT_VIEW_VALUES:
- {
- return next->value;
- }
- default:
- {
- assert(0); /* can't happen */
- }
+ case MP_DICT_VIEW_ITEMS:
+ {
+ mp_obj_t items[] = {next->key, next->value};
+ return mp_obj_new_tuple(2, items);
+ }
+ case MP_DICT_VIEW_KEYS:
+ return next->key;
+ case MP_DICT_VIEW_VALUES:
+ return next->value;
+ default:
+ assert(0); /* can't happen */
+ return mp_const_none;
}
} else {
return mp_const_stop_iteration;
@@ -326,7 +350,6 @@ static mp_obj_t dict_view_getiter(mp_obj_t view_in) {
return o;
}
-
static void dict_view_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
assert(MP_OBJ_IS_TYPE(self_in, &dict_view_type));
mp_obj_dict_view_t *self = self_in;
@@ -370,7 +393,6 @@ mp_obj_t mp_obj_new_dict_view(mp_obj_dict_t *dict, mp_dict_view_kind_t kind) {
return o;
}
-
static mp_obj_t dict_view(mp_obj_t self_in, mp_dict_view_kind_t kind) {
assert(MP_OBJ_IS_TYPE(self_in, &dict_type));
mp_obj_dict_t *self = self_in;
@@ -392,67 +414,13 @@ static mp_obj_t dict_values(mp_obj_t self_in) {
}
static MP_DEFINE_CONST_FUN_OBJ_1(dict_values_obj, dict_values);
-
-/******************************************************************************/
-/* dict metaclass */
-
-static mp_obj_t dict_fromkeys(int n_args, const mp_obj_t *args) {
- assert(2 <= n_args && n_args <= 3);
- mp_obj_t iter = rt_getiter(args[1]);
- mp_obj_t len = mp_obj_len_maybe(iter);
- mp_obj_t value = mp_const_none;
- mp_obj_t next = NULL;
- mp_obj_dict_t *self = NULL;
-
- if (n_args > 2) {
- value = args[2];
- }
-
- if (len == NULL) {
- /* object's type doesn't have a __len__ slot */
- self = mp_obj_new_dict(0);
- } else {
- self = mp_obj_new_dict(MP_OBJ_SMALL_INT_VALUE(len));
- }
-
- while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
- mp_map_lookup(&self->map, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
- }
-
- return self;
-}
-static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_fromkeys_obj, 2, 3, dict_fromkeys);
-
-static const mp_method_t dict_class_methods[] = {
- { "fromkeys", &dict_fromkeys_obj },
- { NULL, NULL }, // end-of-list sentinel
-};
-
-/* this should be unnecessary when inheritance works */
-static void dict_class_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
- print(env, "<class 'dict'>");
-}
-
-/* this should be unnecessary when inheritance works */
-static mp_obj_t dict_class_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
- return rt_build_map(0);
-}
-
-static const mp_obj_type_t dict_class = {
- { &mp_const_type },
- "dict_class",
- .print = dict_class_print,
- .methods = dict_class_methods,
- .call_n = dict_class_call_n,
-};
-
-
/******************************************************************************/
-/* dict constructors & etc */
+/* dict constructors & public C API */
static const mp_method_t dict_type_methods[] = {
{ "clear", &dict_clear_obj },
{ "copy", &dict_copy_obj },
+ { "fromkeys", &dict_fromkeys_obj },
{ "get", &dict_get_obj },
{ "items", &dict_items_obj },
{ "keys", &dict_keys_obj },
@@ -465,7 +433,7 @@ static const mp_method_t dict_type_methods[] = {
};
const mp_obj_type_t dict_type = {
- { &dict_class },
+ { &mp_const_type },
"dict",
.print = dict_print,
.make_new = dict_make_new,
diff --git a/py/objint.c b/py/objint.c
index 9cd5ebae29..26d3c0e337 100644
--- a/py/objint.c
+++ b/py/objint.c
@@ -11,8 +11,16 @@
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);
+
+// This dispatcher function is expected to be independent of the implementation
+// of long int
static mp_obj_t int_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
switch (n_args) {
case 0:
@@ -20,7 +28,7 @@ static mp_obj_t int_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args)
case 1:
// TODO allow string as arg and parse it
- return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
+ return mp_obj_new_int(mp_obj_get_int(args[0]));
//case 2:
// TODO, parse with given base
@@ -33,9 +41,41 @@ static mp_obj_t int_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args)
const mp_obj_type_t int_type = {
{ &mp_const_type },
"int",
+ .print = int_print,
.make_new = int_make_new,
+ .binary_op = int_binary_op,
};
+#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
+// This is called only for non-SMALL_INT
+void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
+}
+
+// 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);
+}
+
+// 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);
+}
+
+mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
+ // SMALL_INT accepts only signed numbers, of one bit less size
+ // then word size, which totals 2 bits less for unsigned numbers.
+ if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
+ return MP_OBJ_NEW_SMALL_INT(value);
+ }
+ // TODO: Raise exception
+ assert(0);
+}
+
mp_obj_t mp_obj_new_int(machine_int_t value) {
- return MP_OBJ_NEW_SMALL_INT(value);
+ if (MP_OBJ_FITS_SMALL_INT(value)) {
+ return MP_OBJ_NEW_SMALL_INT(value);
+ }
+ // TODO: Raise exception
+ assert(0);
}
+#endif
diff --git a/py/objlist.c b/py/objlist.c
index 7d0402d286..fa8ec67d09 100644
--- a/py/objlist.c
+++ b/py/objlist.c
@@ -21,6 +21,7 @@ typedef struct _mp_obj_list_t {
static mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur);
static mp_obj_list_t *list_new(uint n);
+static mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in);
/******************************************************************************/
/* list */
@@ -61,6 +62,61 @@ static mp_obj_t list_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args
return NULL;
}
+// Don't pass RT_COMPARE_OP_NOT_EQUAL here
+static bool list_cmp_helper(int op, mp_obj_t self_in, mp_obj_t another_in) {
+ assert(MP_OBJ_IS_TYPE(self_in, &list_type));
+ if (!MP_OBJ_IS_TYPE(another_in, &list_type)) {
+ return false;
+ }
+ mp_obj_list_t *self = self_in;
+ mp_obj_list_t *another = another_in;
+ if (op == RT_COMPARE_OP_EQUAL && self->len != another->len) {
+ return false;
+ }
+
+ // Let's deal only with > & >=
+ if (op == RT_COMPARE_OP_LESS || op == RT_COMPARE_OP_LESS_EQUAL) {
+ mp_obj_t t = self;
+ self = another;
+ another = t;
+ if (op == RT_COMPARE_OP_LESS) {
+ op = RT_COMPARE_OP_MORE;
+ } else {
+ op = RT_COMPARE_OP_MORE_EQUAL;
+ }
+ }
+
+ int len = self->len < another->len ? self->len : another->len;
+ bool eq_status = true; // empty lists are equal
+ bool rel_status;
+ for (int i = 0; i < len; i++) {
+ eq_status = mp_obj_equal(self->items[i], another->items[i]);
+ if (op == RT_COMPARE_OP_EQUAL && !eq_status) {
+ return false;
+ }
+ rel_status = (rt_binary_op(op, self->items[i], another->items[i]) == mp_const_true);
+ if (!eq_status && !rel_status) {
+ return false;
+ }
+ }
+
+ // If we had tie in the last element...
+ if (eq_status) {
+ // ... and we have lists of different lengths...
+ if (self->len != another->len) {
+ if (self->len < another->len) {
+ // ... then longer list length wins (we deal only with >)
+ return false;
+ }
+ } else if (op == RT_COMPARE_OP_MORE) {
+ // Otherwise, if we have strict relation, equality means failure
+ return false;
+ }
+ }
+
+ return true;
+}
+
static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
mp_obj_list_t *o = lhs;
switch (op) {
@@ -81,6 +137,14 @@ static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
memcpy(s->items + o->len, p->items, sizeof(mp_obj_t) * p->len);
return s;
}
+ case RT_BINARY_OP_INPLACE_ADD:
+ {
+ if (!MP_OBJ_IS_TYPE(rhs, &list_type)) {
+ return NULL;
+ }
+ list_extend(lhs, rhs);
+ return o;
+ }
case RT_BINARY_OP_MULTIPLY:
{
if (!MP_OBJ_IS_SMALL_INT(rhs)) {
@@ -96,6 +160,15 @@ static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
}
return s;
}
+ case RT_COMPARE_OP_EQUAL:
+ case RT_COMPARE_OP_LESS:
+ case RT_COMPARE_OP_LESS_EQUAL:
+ case RT_COMPARE_OP_MORE:
+ case RT_COMPARE_OP_MORE_EQUAL:
+ return MP_BOOL(list_cmp_helper(op, lhs, rhs));
+ case RT_COMPARE_OP_NOT_EQUAL:
+ return MP_BOOL(!list_cmp_helper(RT_COMPARE_OP_EQUAL, lhs, rhs));
+
default:
// op not supported
return NULL;
@@ -117,6 +190,23 @@ mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) {
return mp_const_none; // return None, as per CPython
}
+static mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in) {
+ assert(MP_OBJ_IS_TYPE(self_in, &list_type));
+ assert(MP_OBJ_IS_TYPE(arg_in, &list_type));
+ mp_obj_list_t *self = self_in;
+ mp_obj_list_t *arg = arg_in;
+
+ if (self->len + arg->len > self->alloc) {
+ // TODO: use alloc policy for "4"
+ self->items = m_renew(mp_obj_t, self->items, self->alloc, self->len + arg->len + 4);
+ self->alloc = self->len + arg->len + 4;
+ }
+
+ memcpy(self->items + self->len, arg->items, sizeof(mp_obj_t) * arg->len);
+ self->len += arg->len;
+ return mp_const_none; // return None, as per CPython
+}
+
static mp_obj_t list_pop(int n_args, const mp_obj_t *args) {
assert(1 <= n_args && n_args <= 2);
assert(MP_OBJ_IS_TYPE(args[0], &list_type));
@@ -281,6 +371,7 @@ static mp_obj_t list_reverse(mp_obj_t self_in) {
}
static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
+static MP_DEFINE_CONST_FUN_OBJ_2(list_extend_obj, list_extend);
static MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
static MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy);
static MP_DEFINE_CONST_FUN_OBJ_2(list_count_obj, list_count);
@@ -296,6 +387,7 @@ static const mp_method_t list_type_methods[] = {
{ "clear", &list_clear_obj },
{ "copy", &list_copy_obj },
{ "count", &list_count_obj },
+ { "extend", &list_extend_obj },
{ "index", &list_index_obj },
{ "insert", &list_insert_obj },
{ "pop", &list_pop_obj },
diff --git a/py/objset.c b/py/objset.c
index 71ed553350..43bb61083b 100644
--- a/py/objset.c
+++ b/py/objset.c
@@ -1,5 +1,6 @@
#include <stdlib.h>
#include <stdint.h>
+#include <string.h>
#include <assert.h>
#include "nlr.h"
@@ -16,8 +17,20 @@ typedef struct _mp_obj_set_t {
mp_set_t set;
} mp_obj_set_t;
+typedef struct _mp_obj_set_it_t {
+ mp_obj_base_t base;
+ mp_obj_set_t *set;
+ machine_uint_t cur;
+} mp_obj_set_it_t;
+
+static mp_obj_t set_it_iternext(mp_obj_t self_in);
+
void set_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
mp_obj_set_t *self = self_in;
+ if (self->set.used == 0) {
+ print(env, "set()");
+ return;
+ }
bool first = true;
print(env, "{");
for (int i = 0; i < self->set.alloc; i++) {
@@ -73,12 +86,383 @@ static mp_obj_t set_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args)
}
}
+const mp_obj_type_t set_it_type = {
+ { &mp_const_type },
+ "set_iterator",
+ .iternext = set_it_iternext,
+};
+
+static mp_obj_t set_it_iternext(mp_obj_t self_in) {
+ assert(MP_OBJ_IS_TYPE(self_in, &set_it_type));
+ mp_obj_set_it_t *self = self_in;
+ machine_uint_t max = self->set->set.alloc;
+ mp_obj_t *table = self->set->set.table;
+
+ for (machine_uint_t i = self->cur; i < max; i++) {
+ if (table[i] != NULL) {
+ self->cur = i + 1;
+ return table[i];
+ }
+ }
+
+ return mp_const_stop_iteration;
+}
+
+static mp_obj_t set_getiter(mp_obj_t set_in) {
+ mp_obj_set_it_t *o = m_new_obj(mp_obj_set_it_t);
+ o->base.type = &set_it_type;
+ o->set = (mp_obj_set_t *)set_in;
+ o->cur = 0;
+ return o;
+}
+
+
+/******************************************************************************/
+/* set methods */
+
+static mp_obj_t set_add(mp_obj_t self_in, mp_obj_t item) {
+ assert(MP_OBJ_IS_TYPE(self_in, &set_type));
+ mp_obj_set_t *self = self_in;
+ mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
+ return mp_const_none;
+}
+static MP_DEFINE_CONST_FUN_OBJ_2(set_add_obj, set_add);
+
+static mp_obj_t set_clear(mp_obj_t self_in) {
+ assert(MP_OBJ_IS_TYPE(self_in, &set_type));
+ mp_obj_set_t *self = self_in;
+
+ mp_set_clear(&self->set);
+
+ return mp_const_none;
+}
+static MP_DEFINE_CONST_FUN_OBJ_1(set_clear_obj, set_clear);
+
+static mp_obj_t set_copy(mp_obj_t self_in) {
+ assert(MP_OBJ_IS_TYPE(self_in, &set_type));
+ mp_obj_set_t *self = self_in;
+
+ mp_obj_set_t *other = m_new_obj(mp_obj_set_t);
+ other->base.type = &set_type;
+ mp_set_init(&other->set, self->set.alloc - 1);
+ other->set.used = self->set.used;
+ memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t));
+
+ return other;
+}
+static MP_DEFINE_CONST_FUN_OBJ_1(set_copy_obj, set_copy);
+
+static mp_obj_t set_discard(mp_obj_t self_in, mp_obj_t item) {
+ assert(MP_OBJ_IS_TYPE(self_in, &set_type));
+ mp_obj_set_t *self = self_in;
+ mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
+ return mp_const_none;
+}
+static MP_DEFINE_CONST_FUN_OBJ_2(set_discard_obj, set_discard);
+
+static mp_obj_t set_diff_int(int n_args, const mp_obj_t *args, bool update) {
+ assert(n_args > 0);
+ assert(MP_OBJ_IS_TYPE(args[0], &set_type));
+ mp_obj_set_t *self;
+ if (update) {
+ self = args[0];
+ } else {
+ self = set_copy(args[0]);
+ }
+
+
+ for (int i = 1; i < n_args; i++) {
+ mp_obj_t other = args[i];
+ if (self == other) {
+ set_clear(self);
+ } else {
+ mp_obj_t iter = rt_getiter(other);
+ mp_obj_t next;
+ while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
+ set_discard(self, next);
+ }
+ }
+ }
+
+ return self;
+}
+
+static mp_obj_t set_diff(int n_args, const mp_obj_t *args) {
+ return set_diff_int(n_args, args, false);
+}
+static MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_obj, 1, set_diff);
+
+static mp_obj_t set_diff_update(int n_args, const mp_obj_t *args) {
+ set_diff_int(n_args, args, true);
+ return mp_const_none;
+}
+static MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_update_obj, 1, set_diff_update);
+
+static mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update) {
+ assert(MP_OBJ_IS_TYPE(self_in, &set_type));
+ if (self_in == other) {
+ return update ? mp_const_none : set_copy(self_in);
+ }
+
+ mp_obj_set_t *self = self_in;
+ mp_obj_set_t *out = mp_obj_new_set(0, NULL);
+
+ mp_obj_t iter = rt_getiter(other);
+ mp_obj_t next;
+ while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
+ if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
+ set_add(out, next);
+ }
+ }
+
+ if (update) {
+ m_del(mp_obj_t, self->set.table, self->set.alloc);
+ self->set.alloc = out->set.alloc;
+ self->set.used = out->set.used;
+ self->set.table = out->set.table;
+ }
+
+ return update ? mp_const_none : out;
+}
+
+static mp_obj_t set_intersect(mp_obj_t self_in, mp_obj_t other) {
+ return set_intersect_int(self_in, other, false);
+}
+static MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_obj, set_intersect);
+
+static mp_obj_t set_intersect_update(mp_obj_t self_in, mp_obj_t other) {
+ return set_intersect_int(self_in, other, true);
+}
+static MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_update_obj, set_intersect_update);
+
+static mp_obj_t set_isdisjoint(mp_obj_t self_in, mp_obj_t other) {
+ assert(MP_OBJ_IS_TYPE(self_in, &set_type));
+ mp_obj_set_t *self = self_in;
+
+ mp_obj_t iter = rt_getiter(other);
+ mp_obj_t next;
+ while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
+ if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
+ return mp_const_false;
+ }
+ }
+ return mp_const_true;
+}
+static MP_DEFINE_CONST_FUN_OBJ_2(set_isdisjoint_obj, set_isdisjoint);
+
+static mp_obj_t set_issubset_internal(mp_obj_t self_in, mp_obj_t other_in, bool proper) {
+ mp_obj_set_t *self;
+ bool cleanup_self = false;
+ if (MP_OBJ_IS_TYPE(self_in, &set_type)) {
+ self = self_in;
+ } else {
+ self = set_make_new(NULL, 1, &self_in);
+ cleanup_self = true;
+ }
+
+ mp_obj_set_t *other;
+ bool cleanup_other = false;
+ if (MP_OBJ_IS_TYPE(other_in, &set_type)) {
+ other = other_in;
+ } else {
+ other = set_make_new(NULL, 1, &other_in);
+ cleanup_other = true;
+ }
+ bool out = true;
+ if (proper && self->set.used == other->set.used) {
+ out = false;
+ } else {
+ mp_obj_t iter = set_getiter(self);
+ mp_obj_t next;
+ while ((next = set_it_iternext(iter)) != mp_const_stop_iteration) {
+ if (!mp_set_lookup(&other->set, next, MP_MAP_LOOKUP)) {
+ out = false;
+ break;
+ }
+ }
+ }
+ if (cleanup_self) {
+ set_clear(self);
+ }
+ if (cleanup_other) {
+ set_clear(other);
+ }
+ return MP_BOOL(out);
+}
+static mp_obj_t set_issubset(mp_obj_t self_in, mp_obj_t other_in) {
+ return set_issubset_internal(self_in, other_in, false);
+}
+static MP_DEFINE_CONST_FUN_OBJ_2(set_issubset_obj, set_issubset);
+
+static mp_obj_t set_issubset_proper(mp_obj_t self_in, mp_obj_t other_in) {
+ return set_issubset_internal(self_in, other_in, true);
+}
+
+static mp_obj_t set_issuperset(mp_obj_t self_in, mp_obj_t other_in) {
+ return set_issubset_internal(other_in, self_in, false);
+}
+static MP_DEFINE_CONST_FUN_OBJ_2(set_issuperset_obj, set_issuperset);
+
+static mp_obj_t set_issuperset_proper(mp_obj_t self_in, mp_obj_t other_in) {
+ return set_issubset_internal(other_in, self_in, true);
+}
+
+static mp_obj_t set_equal(mp_obj_t self_in, mp_obj_t other_in) {
+ assert(MP_OBJ_IS_TYPE(self_in, &set_type));
+ mp_obj_set_t *self = self_in;
+ if (!MP_OBJ_IS_TYPE(other_in, &set_type)) {
+ return mp_const_false;
+ }
+ mp_obj_set_t *other = other_in;
+ if (self->set.used != other->set.used) {
+ return mp_const_false;
+ }
+ return set_issubset(self_in, other_in);
+}
+
+static mp_obj_t set_pop(mp_obj_t self_in) {
+ assert(MP_OBJ_IS_TYPE(self_in, &set_type));
+ mp_obj_set_t *self = self_in;
+
+ if (self->set.used == 0) {
+ nlr_jump(mp_obj_new_exception_msg(MP_QSTR_KeyError, "pop from an empty set"));
+ }
+ mp_obj_t obj = mp_set_lookup(&self->set, NULL,
+ MP_MAP_LOOKUP_REMOVE_IF_FOUND | MP_MAP_LOOKUP_FIRST);
+ return obj;
+}
+static MP_DEFINE_CONST_FUN_OBJ_1(set_pop_obj, set_pop);
+
+static mp_obj_t set_remove(mp_obj_t self_in, mp_obj_t item) {
+ assert(MP_OBJ_IS_TYPE(self_in, &set_type));
+ mp_obj_set_t *self = self_in;
+ if (mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND) == MP_OBJ_NULL) {
+ nlr_jump(mp_obj_new_exception(MP_QSTR_KeyError));
+ }
+ return mp_const_none;
+}
+static MP_DEFINE_CONST_FUN_OBJ_2(set_remove_obj, set_remove);
+
+static mp_obj_t set_symmetric_difference_update(mp_obj_t self_in, mp_obj_t other_in) {
+ assert(MP_OBJ_IS_TYPE(self_in, &set_type));
+ mp_obj_set_t *self = self_in;
+ mp_obj_t iter = rt_getiter(other_in);
+ mp_obj_t next;
+ while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
+ mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_REMOVE_IF_FOUND | MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
+ }
+ return mp_const_none;
+}
+static MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_update_obj, set_symmetric_difference_update);
+
+static mp_obj_t set_symmetric_difference(mp_obj_t self_in, mp_obj_t other_in) {
+ assert(MP_OBJ_IS_TYPE(self_in, &set_type));
+ self_in = set_copy(self_in);
+ set_symmetric_difference_update(self_in, other_in);
+ return self_in;
+}
+static MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_obj, set_symmetric_difference);
+
+static void set_update_int(mp_obj_set_t *self, mp_obj_t other_in) {
+ mp_obj_t iter = rt_getiter(other_in);
+ mp_obj_t next;
+ while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
+ mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
+ }
+}
+
+static mp_obj_t set_update(int n_args, const mp_obj_t *args) {
+ assert(n_args > 0);
+ assert(MP_OBJ_IS_TYPE(args[0], &set_type));
+
+ for (int i = 1; i < n_args; i++) {
+ set_update_int(args[0], args[i]);
+ }
+
+ return mp_const_none;
+}
+static MP_DEFINE_CONST_FUN_OBJ_VAR(set_update_obj, 1, set_update);
+
+static mp_obj_t set_union(mp_obj_t self_in, mp_obj_t other_in) {
+ assert(MP_OBJ_IS_TYPE(self_in, &set_type));
+ mp_obj_set_t *self = set_copy(self_in);
+ set_update_int(self, other_in);
+ return self;
+}
+static MP_DEFINE_CONST_FUN_OBJ_2(set_union_obj, set_union);
+
+
+static mp_obj_t set_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
+ mp_obj_t args[] = {lhs, rhs};
+ switch (op) {
+ case RT_BINARY_OP_OR:
+ return set_union(lhs, rhs);
+ case RT_BINARY_OP_XOR:
+ return set_symmetric_difference(lhs, rhs);
+ case RT_BINARY_OP_AND:
+ return set_intersect(lhs, rhs);
+ case RT_BINARY_OP_SUBTRACT:
+ return set_diff(2, args);
+ case RT_BINARY_OP_INPLACE_OR:
+ return set_union(lhs, rhs);
+ case RT_BINARY_OP_INPLACE_XOR:
+ return set_symmetric_difference(lhs, rhs);
+ case RT_BINARY_OP_INPLACE_AND:
+ return set_intersect(lhs, rhs);
+ case RT_BINARY_OP_INPLACE_SUBTRACT:
+ return set_diff(2, args);
+ case RT_COMPARE_OP_LESS:
+ return set_issubset_proper(lhs, rhs);
+ case RT_COMPARE_OP_MORE:
+ return set_issuperset_proper(lhs, rhs);
+ case RT_COMPARE_OP_EQUAL:
+ return set_equal(lhs, rhs);
+ case RT_COMPARE_OP_LESS_EQUAL:
+ return set_issubset(lhs, rhs);
+ case RT_COMPARE_OP_MORE_EQUAL:
+ return set_issuperset(lhs, rhs);
+ case RT_COMPARE_OP_NOT_EQUAL:
+ return MP_BOOL(set_equal(lhs, rhs) == mp_const_false);
+ default:
+ // op not supported
+ return NULL;
+ }
+}
+
+/******************************************************************************/
+/* set constructors & public C API */
+
+
+static const mp_method_t set_type_methods[] = {
+ { "add", &set_add_obj },
+ { "clear", &set_clear_obj },
+ { "copy", &set_copy_obj },
+ { "discard", &set_discard_obj },
+ { "difference", &set_diff_obj },
+ { "difference_update", &set_diff_update_obj },
+ { "intersection", &set_intersect_obj },
+ { "intersection_update", &set_intersect_update_obj },
+ { "isdisjoint", &set_isdisjoint_obj },
+ { "issubset", &set_issubset_obj },
+ { "issuperset", &set_issuperset_obj },
+ { "pop", &set_pop_obj },
+ { "remove", &set_remove_obj },
+ { "symmetric_difference", &set_symmetric_difference_obj },
+ { "symmetric_difference_update", &set_symmetric_difference_update_obj },
+ { "union", &set_union_obj },
+ { "update", &set_update_obj },
+ { NULL, NULL }, // end-of-list sentinel
+};
+
const mp_obj_type_t set_type = {
{ &mp_const_type },
"set",
.print = set_print,
.binary_op = set_binary_op,
.make_new = set_make_new,
+ .binary_op = set_binary_op,
+ .getiter = set_getiter,
+ .methods = set_type_methods,
};
mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) {
@@ -86,7 +470,7 @@ mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) {
o->base.type = &set_type;
mp_set_init(&o->set, n_args);
for (int i = 0; i < n_args; i++) {
- mp_set_lookup(&o->set, items[i], true);
+ mp_set_lookup(&o->set, items[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
}
return o;
}
@@ -94,5 +478,5 @@ mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) {
void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item) {
assert(MP_OBJ_IS_TYPE(self_in, &set_type));
mp_obj_set_t *self = self_in;
- mp_set_lookup(&self->set, item, true);
+ mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
}
diff --git a/py/objstr.c b/py/objstr.c
index eb8b4c4be1..4151cf007b 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -165,6 +165,43 @@ static bool chr_in_str(const char* const str, const size_t str_len, const char c
return false;
}
+static mp_obj_t str_find(int n_args, const mp_obj_t *args) {
+ assert(2 <= n_args && n_args <= 4);
+ assert(MP_OBJ_IS_TYPE(args[0], &str_type));
+ if (!MP_OBJ_IS_TYPE(args[1], &str_type)) {
+ nlr_jump(mp_obj_new_exception_msg_1_arg(
+ MP_QSTR_TypeError,
+ "Can't convert '%s' object to str implicitly",
+ mp_obj_get_type_str(args[1])));
+ }
+
+ 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 start = 0;
+ size_t end = haystack_len;
+ /* TODO use a non-exception-throwing mp_get_index */
+ if (n_args >= 3 && args[2] != mp_const_none) {
+ start = mp_get_index(&str_type, haystack_len, args[2]);
+ }
+ if (n_args >= 4 && args[3] != mp_const_none) {
+ end = mp_get_index(&str_type, haystack_len, args[3]);
+ }
+
+ char *p = strstr(haystack + start, needle);
+ ssize_t pos = -1;
+ if (p) {
+ pos = p - haystack;
+ if (pos + needle_len > end) {
+ pos = -1;
+ }
+ }
+ return MP_OBJ_NEW_SMALL_INT(pos);
+}
+
mp_obj_t str_strip(int n_args, const mp_obj_t *args) {
assert(1 <= n_args && n_args <= 2);
assert(MP_OBJ_IS_TYPE(args[0], &str_type));
@@ -248,11 +285,13 @@ mp_obj_t str_format(int n_args, const mp_obj_t *args) {
return mp_obj_new_str(qstr_from_str_take(vstr->buf, vstr->alloc));
}
+static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_find_obj, 2, 4, str_find);
static MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join);
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_strip_obj, 1, 2, str_strip);
static MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, str_format);
static const mp_method_t str_type_methods[] = {
+ { "find", &str_find_obj },
{ "join", &str_join_obj },
{ "strip", &str_strip_obj },
{ "format", &str_format_obj },
diff --git a/py/objtype.c b/py/objtype.c
index 6c89c1ff2b..011ee43552 100644
--- a/py/objtype.c
+++ b/py/objtype.c
@@ -27,15 +27,13 @@ static mp_obj_t mp_obj_new_class(mp_obj_t class) {
return o;
}
-static mp_map_elem_t *mp_obj_class_lookup(mp_obj_t self_in, qstr attr, mp_map_lookup_kind_t lookup_kind) {
+static mp_map_elem_t *mp_obj_class_lookup(const mp_obj_type_t *type, qstr attr, mp_map_lookup_kind_t lookup_kind) {
for (;;) {
- assert(MP_OBJ_IS_TYPE(self_in, &mp_const_type));
- mp_obj_type_t *self = self_in;
- if (self->locals_dict == NULL) {
+ if (type->locals_dict == NULL) {
return NULL;
}
- assert(MP_OBJ_IS_TYPE(self->locals_dict, &dict_type)); // Micro Python restriction, for now
- mp_map_t *locals_map = ((void*)self->locals_dict + sizeof(mp_obj_base_t)); // XXX hack to get map object from dict object
+ assert(MP_OBJ_IS_TYPE(type->locals_dict, &dict_type)); // Micro Python restriction, for now
+ mp_map_t *locals_map = ((void*)type->locals_dict + sizeof(mp_obj_base_t)); // XXX hack to get map object from dict object
mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), lookup_kind);
if (elem != NULL) {
return elem;
@@ -44,25 +42,27 @@ static mp_map_elem_t *mp_obj_class_lookup(mp_obj_t self_in, qstr attr, mp_map_lo
// attribute not found, keep searching base classes
// for a const struct, this entry might be NULL
- if (self->bases_tuple == MP_OBJ_NULL) {
+ if (type->bases_tuple == MP_OBJ_NULL) {
return NULL;
}
uint len;
mp_obj_t *items;
- mp_obj_tuple_get(self->bases_tuple, &len, &items);
+ mp_obj_tuple_get(type->bases_tuple, &len, &items);
if (len == 0) {
return NULL;
}
for (uint i = 0; i < len - 1; i++) {
- elem = mp_obj_class_lookup(items[i], attr, lookup_kind);
+ assert(MP_OBJ_IS_TYPE(items[i], &mp_const_type));
+ elem = mp_obj_class_lookup((mp_obj_type_t*)items[i], attr, lookup_kind);
if (elem != NULL) {
return elem;
}
}
// search last base (simple tail recursion elimination)
- self_in = items[len - 1];
+ assert(MP_OBJ_IS_TYPE(items[len - 1], &mp_const_type));
+ type = (mp_obj_type_t*)items[len - 1];
}
}
@@ -73,11 +73,12 @@ static void class_print(void (*print)(void *env, const char *fmt, ...), void *en
// args are reverse in the array
static mp_obj_t class_make_new(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
assert(MP_OBJ_IS_TYPE(self_in, &mp_const_type));
+ mp_obj_type_t *self = self_in;
mp_obj_t o = mp_obj_new_class(self_in);
// look for __init__ function
- mp_map_elem_t *init_fn = mp_obj_class_lookup(self_in, MP_QSTR___init__, MP_MAP_LOOKUP);
+ mp_map_elem_t *init_fn = mp_obj_class_lookup(self, MP_QSTR___init__, MP_MAP_LOOKUP);
if (init_fn != NULL) {
// call __init__ function
@@ -114,7 +115,7 @@ static void class_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
dest[1] = elem->value;
return;
}
- elem = mp_obj_class_lookup((mp_obj_t)self->base.type, attr, MP_MAP_LOOKUP);
+ elem = mp_obj_class_lookup(self->base.type, attr, MP_MAP_LOOKUP);
if (elem != NULL) {
if (mp_obj_is_callable(elem->value)) {
// class member is callable so build a bound method
@@ -132,7 +133,7 @@ static void class_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
static bool class_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) {
// logic: look in class locals (no add) then obj members (add) (TODO check this against CPython)
mp_obj_class_t *self = self_in;
- mp_map_elem_t *elem = mp_obj_class_lookup((mp_obj_t)self->base.type, attr, MP_MAP_LOOKUP);
+ mp_map_elem_t *elem = mp_obj_class_lookup(self->base.type, attr, MP_MAP_LOOKUP);
if (elem != NULL) {
elem->value = value;
} else {
@@ -188,17 +189,47 @@ static mp_obj_t type_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args)
// for fail, do nothing; for attr, dest[1] = value; for method, dest[0] = self, dest[1] = method
static void type_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
- mp_map_elem_t *elem = mp_obj_class_lookup(self_in, attr, MP_MAP_LOOKUP);
+ assert(MP_OBJ_IS_TYPE(self_in, &mp_const_type));
+ mp_obj_type_t *self = self_in;
+ mp_map_elem_t *elem = mp_obj_class_lookup(self, attr, MP_MAP_LOOKUP);
if (elem != NULL) {
dest[1] = elem->value;
return;
}
+
+ // generic method lookup
+ // this is a lookup in the class itself (ie not the classes type or instance)
+ const mp_method_t *meth = self->methods;
+ if (meth != NULL) {
+ for (; meth->name != NULL; meth++) {
+ if (strcmp(meth->name, qstr_str(attr)) == 0) {
+ // check if the methods are functions, static or class methods
+ // see http://docs.python.org/3.3/howto/descriptor.html
+ if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_staticmethod)) {
+ // return just the function
+ dest[1] = ((mp_obj_staticmethod_t*)meth->fun)->fun;
+ } else if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_classmethod)) {
+ // return a bound method, with self being this class
+ dest[1] = ((mp_obj_classmethod_t*)meth->fun)->fun;
+ dest[0] = self_in;
+ } else {
+ // return just the function
+ // TODO need to wrap in a type check for the first argument; eg list.append(1,1) needs to throw an exception
+ dest[1] = (mp_obj_t)meth->fun;
+ }
+ return;
+ }
+ }
+ }
}
static bool type_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) {
+ assert(MP_OBJ_IS_TYPE(self_in, &mp_const_type));
+ mp_obj_type_t *self = self_in;
+
// TODO CPython allows STORE_ATTR to a class, but is this the correct implementation?
- mp_map_elem_t *elem = mp_obj_class_lookup(self_in, attr, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
+ mp_map_elem_t *elem = mp_obj_class_lookup(self, attr, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
if (elem != NULL) {
elem->value = value;
return true;
@@ -284,3 +315,16 @@ static mp_obj_t mp_builtin_isinstance(mp_obj_t object, mp_obj_t classinfo) {
}
MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_isinstance_obj, mp_builtin_isinstance);
+
+/******************************************************************************/
+// staticmethod and classmethod types (probably should go in a different file)
+
+const mp_obj_type_t mp_type_staticmethod = {
+ { &mp_const_type },
+ "staticmethod",
+};
+
+const mp_obj_type_t mp_type_classmethod = {
+ { &mp_const_type },
+ "classmethod",
+};
diff --git a/py/parse.c b/py/parse.c
index d3786ba956..e2c9520736 100644
--- a/py/parse.c
+++ b/py/parse.c
@@ -88,6 +88,7 @@ typedef struct _parser_t {
uint rule_stack_top;
rule_stack_t *rule_stack;
+ uint result_stack_alloc;
uint result_stack_top;
mp_parse_node_t *result_stack;
} parser_t;
@@ -121,7 +122,7 @@ mp_parse_node_t mp_parse_node_new_leaf(machine_int_t kind, machine_int_t arg) {
int num_parse_nodes_allocated = 0;
mp_parse_node_struct_t *parse_node_new_struct(int rule_id, int num_args) {
- mp_parse_node_struct_t *pn = m_malloc(sizeof(mp_parse_node_struct_t) + num_args * sizeof(mp_parse_node_t));
+ mp_parse_node_struct_t *pn = m_new_obj_var(mp_parse_node_struct_t, mp_parse_node_t, num_args);
pn->source = 0; // TODO
pn->kind_num_nodes = (rule_id & 0xff) | (num_args << 8);
num_parse_nodes_allocated += 1;
@@ -180,6 +181,10 @@ static mp_parse_node_t peek_result(parser_t *parser, int pos) {
}
static void push_result_node(parser_t *parser, mp_parse_node_t pn) {
+ if (parser->result_stack_top >= parser->result_stack_alloc) {
+ parser->result_stack = m_renew(mp_parse_node_t, parser->result_stack, parser->result_stack_alloc, parser->result_stack_alloc * 2);
+ parser->result_stack_alloc *= 2;
+ }
parser->result_stack[parser->result_stack_top++] = pn;
}
@@ -191,7 +196,7 @@ static void push_result_token(parser_t *parser, const mp_lexer_t *lex) {
} else if (tok->kind == MP_TOKEN_NUMBER) {
bool dec = false;
bool small_int = true;
- int int_val = 0;
+ machine_int_t int_val = 0;
int len = tok->len;
const char *str = tok->str;
int base = 10;
@@ -211,7 +216,9 @@ static void push_result_token(parser_t *parser, const mp_lexer_t *lex) {
i = 2;
}
}
+ bool overflow = false;
for (; i < len; i++) {
+ machine_int_t old_val = int_val;
if (unichar_isdigit(str[i]) && str[i] - '0' < base) {
int_val = base * int_val + str[i] - '0';
} else if (base == 16 && 'a' <= str[i] && str[i] <= 'f') {
@@ -225,10 +232,17 @@ static void push_result_token(parser_t *parser, const mp_lexer_t *lex) {
small_int = false;
break;
}
+ if (int_val < old_val) {
+ // If new value became less than previous, it's overflow
+ overflow = true;
+ } else if ((old_val ^ int_val) & WORD_MSBIT_HIGH) {
+ // If signed number changed sign - it's overflow
+ overflow = true;
+ }
}
if (dec) {
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_DECIMAL, qstr_from_strn_copy(str, len));
- } else if (small_int && MP_FIT_SMALL_INT(int_val)) {
+ } else if (small_int && !overflow && MP_FIT_SMALL_INT(int_val)) {
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, int_val);
} else {
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_INTEGER, qstr_from_strn_copy(str, len));
@@ -252,14 +266,20 @@ static void push_result_rule(parser_t *parser, const rule_t *rule, int num_args)
}
mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
- parser_t *parser = m_new(parser_t, 1);
+
+ // allocate memory for the parser and its stacks
+
+ parser_t *parser = m_new_obj(parser_t);
+
parser->rule_stack_alloc = 64;
parser->rule_stack_top = 0;
parser->rule_stack = m_new(rule_stack_t, parser->rule_stack_alloc);
- parser->result_stack = m_new(mp_parse_node_t, 1000);
+ parser->result_stack_alloc = 64;
parser->result_stack_top = 0;
+ parser->result_stack = m_new(mp_parse_node_t, parser->result_stack_alloc);
+ // work out the top-level rule to use, and push it on the stack
int top_level_rule;
switch (input_kind) {
case MP_PARSE_SINGLE_INPUT: top_level_rule = RULE_single_input; break;
@@ -268,6 +288,8 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
}
push_rule(parser, rules[top_level_rule], 0);
+ // parse!
+
uint n, i;
bool backtrack = false;
const rule_t *rule;
@@ -558,12 +580,25 @@ mp_parse_node_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
//printf("--------------\n");
//result_stack_show(parser);
- assert(parser->result_stack_top == 1);
- //printf("maximum depth: %d\n", parser->rule_stack_alloc);
+ //printf("rule stack alloc: %d\n", parser->rule_stack_alloc);
+ //printf("result stack alloc: %d\n", parser->result_stack_alloc);
//printf("number of parse nodes allocated: %d\n", num_parse_nodes_allocated);
- return parser->result_stack[0];
+
+ // get the root parse node that we created
+ assert(parser->result_stack_top == 1);
+ mp_parse_node_t result = parser->result_stack[0];
+
+finished:
+ // free the memory that we don't need anymore
+ m_del(rule_stack_t, parser->rule_stack, parser->rule_stack_alloc);
+ m_del(mp_parse_node_t, parser->result_stack, parser->result_stack_alloc);
+ m_del_obj(parser_t, parser);
+
+ // return the result
+ return result;
syntax_error:
+ // TODO these should raise a proper exception
if (mp_lexer_is_kind(lex, MP_TOKEN_INDENT)) {
mp_lexer_show_error_pythonic(lex, "IndentationError: unexpected indent");
} else if (mp_lexer_is_kind(lex, MP_TOKEN_DEDENT_MISMATCH)) {
@@ -575,5 +610,6 @@ syntax_error:
#endif
mp_token_show(mp_lexer_cur(lex));
}
- return MP_PARSE_NODE_NULL;
+ result = MP_PARSE_NODE_NULL;
+ goto finished;
}
diff --git a/py/runtime.c b/py/runtime.c
index 63cb83da56..50ee37ec0d 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -90,6 +90,7 @@ void rt_init(void) {
mp_map_add_qstr(&map_builtins, MP_QSTR_SyntaxError, mp_obj_new_exception(MP_QSTR_SyntaxError));
mp_map_add_qstr(&map_builtins, MP_QSTR_ValueError, mp_obj_new_exception(MP_QSTR_ValueError));
mp_map_add_qstr(&map_builtins, MP_QSTR_OSError, mp_obj_new_exception(MP_QSTR_OSError));
+ mp_map_add_qstr(&map_builtins, MP_QSTR_AssertionError, mp_obj_new_exception(MP_QSTR_AssertionError));
// built-in objects
mp_map_add_qstr(&map_builtins, MP_QSTR_Ellipsis, mp_const_ellipsis);
@@ -267,10 +268,6 @@ void rt_assign_inline_asm_code(int unique_code_id, void *fun, uint len, int n_ar
#endif
}
-static bool fit_small_int(mp_small_int_t o) {
- return true;
-}
-
int rt_is_true(mp_obj_t arg) {
DEBUG_OP_printf("is true %p\n", arg);
if (MP_OBJ_IS_SMALL_INT(arg)) {
@@ -435,13 +432,10 @@ mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
case RT_UNARY_OP_INVERT: val = ~val; break;
default: assert(0); val = 0;
}
- if (fit_small_int(val)) {
+ if (MP_OBJ_FITS_SMALL_INT(val)) {
return MP_OBJ_NEW_SMALL_INT(val);
- } else {
- // TODO make a bignum
- assert(0);
- return mp_const_none;
}
+ return mp_obj_new_int(val);
} else { // will be an object (small ints are caught in previous if)
mp_obj_base_t *o = arg;
if (o->type->unary_op != NULL) {
@@ -550,9 +544,11 @@ mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
default: assert(0);
}
- if (fit_small_int(lhs_val)) {
+ // TODO: We just should make mp_obj_new_int() inline and use that
+ if (MP_OBJ_FITS_SMALL_INT(lhs_val)) {
return MP_OBJ_NEW_SMALL_INT(lhs_val);
}
+ return mp_obj_new_int(lhs_val);
} else if (MP_OBJ_IS_TYPE(rhs, &float_type)) {
return mp_obj_float_binary_op(op, lhs_val, rhs);
} else if (MP_OBJ_IS_TYPE(rhs, &complex_type)) {
@@ -809,12 +805,25 @@ void rt_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
dest[0] = base;
} else {
// generic method lookup
+ // this is a lookup in the object (ie not class or type)
const mp_method_t *meth = type->methods;
if (meth != NULL) {
for (; meth->name != NULL; meth++) {
if (strcmp(meth->name, qstr_str(attr)) == 0) {
- dest[1] = (mp_obj_t)meth->fun;
- dest[0] = base;
+ // check if the methods are functions, static or class methods
+ // see http://docs.python.org/3.3/howto/descriptor.html
+ if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_staticmethod)) {
+ // return just the function
+ dest[1] = ((mp_obj_staticmethod_t*)meth->fun)->fun;
+ } else if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_classmethod)) {
+ // return a bound method, with self being the type of this object
+ dest[1] = ((mp_obj_classmethod_t*)meth->fun)->fun;
+ dest[0] = mp_obj_get_type(base);
+ } else {
+ // return a bound method, with self being this object
+ dest[1] = (mp_obj_t)meth->fun;
+ dest[0] = base;
+ }
break;
}
}