py: Change mp_const_* objects to macros.

Addresses issue #388.
This commit is contained in:
Damien George 2014-03-29 13:15:08 +00:00
parent da51a399cf
commit 07ddab529c
15 changed files with 57 additions and 57 deletions

View File

@ -25,7 +25,7 @@ STATIC const mp_builtin_elem_t builtin_object_table[] = {
{ MP_QSTR___repl_print__, (mp_obj_t)&mp_builtin___repl_print___obj },
// built-in types
{ MP_QSTR_bool, (mp_obj_t)&bool_type },
{ MP_QSTR_bool, (mp_obj_t)&mp_type_bool },
{ MP_QSTR_bytes, (mp_obj_t)&bytes_type },
#if MICROPY_ENABLE_FLOAT
{ MP_QSTR_complex, (mp_obj_t)&mp_type_complex },
@ -43,13 +43,16 @@ STATIC const mp_builtin_elem_t builtin_object_table[] = {
{ MP_QSTR_set, (mp_obj_t)&set_type },
{ MP_QSTR_str, (mp_obj_t)&str_type },
{ MP_QSTR_super, (mp_obj_t)&super_type },
{ MP_QSTR_tuple, (mp_obj_t)&tuple_type },
{ MP_QSTR_tuple, (mp_obj_t)&mp_type_tuple },
{ MP_QSTR_type, (mp_obj_t)&mp_type_type },
{ MP_QSTR_zip, (mp_obj_t)&zip_type },
{ MP_QSTR_classmethod, (mp_obj_t)&mp_type_classmethod },
{ MP_QSTR_staticmethod, (mp_obj_t)&mp_type_staticmethod },
// built-in objects
{ MP_QSTR_Ellipsis, (mp_obj_t)&mp_const_ellipsis_obj },
// built-in user functions
{ MP_QSTR_abs, (mp_obj_t)&mp_builtin_abs_obj },
{ MP_QSTR_all, (mp_obj_t)&mp_builtin_all_obj },

View File

@ -79,11 +79,11 @@ machine_int_t mp_obj_hash(mp_obj_t o_in) {
return MP_OBJ_SMALL_INT_VALUE(o_in);
} else if (MP_OBJ_IS_STR(o_in)) {
return mp_obj_str_get_hash(o_in);
} else if (MP_OBJ_IS_TYPE(o_in, &none_type)) {
} else if (MP_OBJ_IS_TYPE(o_in, &mp_type_NoneType)) {
return (machine_int_t)o_in;
} else if (MP_OBJ_IS_TYPE(o_in, &fun_native_type) || MP_OBJ_IS_TYPE(o_in, &fun_bc_type)) {
return (machine_int_t)o_in;
} else if (MP_OBJ_IS_TYPE(o_in, &tuple_type)) {
} else if (MP_OBJ_IS_TYPE(o_in, &mp_type_tuple)) {
return mp_obj_tuple_hash(o_in);
// TODO hash class and instances
@ -207,7 +207,7 @@ void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
#endif
void mp_obj_get_array(mp_obj_t o, uint *len, mp_obj_t **items) {
if (MP_OBJ_IS_TYPE(o, &tuple_type)) {
if (MP_OBJ_IS_TYPE(o, &mp_type_tuple)) {
mp_obj_tuple_get(o, len, items);
} else if (MP_OBJ_IS_TYPE(o, &list_type)) {
mp_obj_list_get(o, len, items);
@ -217,9 +217,9 @@ void mp_obj_get_array(mp_obj_t o, uint *len, mp_obj_t **items) {
}
void mp_obj_get_array_fixed_n(mp_obj_t o, uint len, mp_obj_t **items) {
if (MP_OBJ_IS_TYPE(o, &tuple_type) || MP_OBJ_IS_TYPE(o, &list_type)) {
if (MP_OBJ_IS_TYPE(o, &mp_type_tuple) || MP_OBJ_IS_TYPE(o, &list_type)) {
uint seq_len;
if (MP_OBJ_IS_TYPE(o, &tuple_type)) {
if (MP_OBJ_IS_TYPE(o, &mp_type_tuple)) {
mp_obj_tuple_get(o, &seq_len, items);
} else {
mp_obj_list_get(o, &seq_len, items);
@ -237,7 +237,7 @@ uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index,
int i;
if (MP_OBJ_IS_SMALL_INT(index)) {
i = MP_OBJ_SMALL_INT_VALUE(index);
} else if (MP_OBJ_IS_TYPE(index, &bool_type)) {
} else if (MP_OBJ_IS_TYPE(index, &mp_type_bool)) {
i = (index == mp_const_true ? 1 : 0);
} else {
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "%s indices must be integers, not %s", qstr_str(type->name), mp_obj_get_type_str(index)));

View File

@ -226,12 +226,17 @@ extern const mp_obj_type_t mp_type_ValueError;
extern const mp_obj_type_t mp_type_ZeroDivisionError;
// Constant objects, globally accessible
extern const mp_obj_t mp_const_none;
extern const mp_obj_t mp_const_false;
extern const mp_obj_t mp_const_true;
extern const mp_obj_t mp_const_empty_tuple;
extern const mp_obj_t mp_const_ellipsis;
extern const mp_obj_t mp_const_GeneratorExit;
// The macros are for convenience only
#define mp_const_none ((mp_obj_t)&mp_const_none_obj)
#define mp_const_false ((mp_obj_t)&mp_const_false_obj)
#define mp_const_true ((mp_obj_t)&mp_const_true_obj)
#define mp_const_empty_tuple ((mp_obj_t)&mp_const_empty_tuple_obj)
extern const struct _mp_obj_none_t mp_const_none_obj;
extern const struct _mp_obj_bool_t mp_const_false_obj;
extern const struct _mp_obj_bool_t mp_const_true_obj;
extern const struct _mp_obj_tuple_t mp_const_empty_tuple_obj;
extern const struct _mp_obj_ellipsis_t mp_const_ellipsis_obj;
extern const struct _mp_obj_exception_t mp_const_GeneratorExit_obj;
// General API for objects
@ -298,10 +303,10 @@ mp_obj_t mp_obj_len_maybe(mp_obj_t o_in); /* may return NULL */
extern const mp_obj_type_t mp_type_object;
// none
extern const mp_obj_type_t none_type;
extern const mp_obj_type_t mp_type_NoneType;
// bool
extern const mp_obj_type_t bool_type;
extern const mp_obj_type_t mp_type_bool;
#define MP_BOOL(x) (x ? mp_const_true : mp_const_false)
// cell
@ -359,7 +364,7 @@ mp_obj_t mp_obj_complex_binary_op(int op, mp_float_t lhs_real, mp_float_t lhs_im
#endif
// tuple
extern const mp_obj_type_t tuple_type;
extern const mp_obj_type_t mp_type_tuple;
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);
machine_int_t mp_obj_tuple_hash(mp_obj_t self_in);

View File

@ -43,7 +43,7 @@ STATIC mp_obj_t bool_unary_op(int op, mp_obj_t o_in) {
}
}
const mp_obj_type_t bool_type = {
const mp_obj_type_t mp_type_bool = {
{ &mp_type_type },
.name = MP_QSTR_bool,
.print = bool_print,
@ -51,8 +51,5 @@ const mp_obj_type_t bool_type = {
.unary_op = bool_unary_op,
};
STATIC const mp_obj_bool_t false_obj = {{&bool_type}, false};
STATIC const mp_obj_bool_t true_obj = {{&bool_type}, true};
const mp_obj_t mp_const_false = (mp_obj_t)&false_obj;
const mp_obj_t mp_const_true = (mp_obj_t)&true_obj;
const mp_obj_bool_t mp_const_false_obj = {{&mp_type_bool}, false};
const mp_obj_bool_t mp_const_true_obj = {{&mp_type_bool}, true};

View File

@ -14,7 +14,7 @@
// This is unified class for C-level and Python-level exceptions
// Python-level exceptions have empty ->msg and all arguments are in
// args tuple. C-level exceptions likely have ->msg set, and args is empty.
typedef struct mp_obj_exception_t {
typedef struct _mp_obj_exception_t {
mp_obj_base_t base;
mp_obj_t traceback; // a list object, holding (file,line,block) as numbers (not Python objects); a hack for now
vstr_t *msg;
@ -24,8 +24,7 @@ typedef struct mp_obj_exception_t {
// Instance of GeneratorExit exception - needed by generator.close()
// This would belong to objgenerator.c, but to keep mp_obj_exception_t
// definition module-private so far, have it here.
STATIC mp_obj_exception_t GeneratorExit_obj = {{&mp_type_GeneratorExit}, MP_OBJ_NULL, NULL, {{&tuple_type}, 0}};
const mp_obj_t mp_const_GeneratorExit = (mp_obj_t)&GeneratorExit_obj;
const mp_obj_exception_t mp_const_GeneratorExit_obj = {{&mp_type_GeneratorExit}, MP_OBJ_NULL, NULL, {{&mp_type_tuple}, 0}};
STATIC void mp_obj_exception_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
mp_obj_exception_t *o = o_in;
@ -61,7 +60,7 @@ STATIC mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, uint n_args, uint n_
o->base.type = type;
o->traceback = MP_OBJ_NULL;
o->msg = NULL;
o->args.base.type = &tuple_type;
o->args.base.type = &mp_type_tuple;
o->args.len = n_args;
memcpy(o->args.items, args, n_args * sizeof(mp_obj_t));
return o;
@ -95,7 +94,7 @@ const mp_obj_type_t mp_type_BaseException = {
};
#define MP_DEFINE_EXCEPTION_BASE(base_name) \
STATIC const mp_obj_tuple_t mp_type_ ## base_name ## _base_tuple = {{&tuple_type}, 1, {(mp_obj_t)&mp_type_ ## base_name}};\
STATIC const mp_obj_tuple_t mp_type_ ## base_name ## _base_tuple = {{&mp_type_tuple}, 1, {(mp_obj_t)&mp_type_ ## base_name}};\
#define MP_DEFINE_EXCEPTION(exc_name, base_name) \
const mp_obj_type_t mp_type_ ## exc_name = { \

View File

@ -373,7 +373,7 @@ STATIC machine_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
// convert float to int (could also pass in float registers)
return (machine_int_t)mp_obj_float_get(obj);
#endif
} else if (MP_OBJ_IS_TYPE(obj, &tuple_type)) {
} else if (MP_OBJ_IS_TYPE(obj, &mp_type_tuple)) {
// pointer to start of tuple (could pass length, but then could use len(x) for that)
uint len;
mp_obj_t *items;

View File

@ -171,7 +171,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gen_instance_throw_obj, 2, 4, gen_ins
STATIC mp_obj_t gen_instance_close(mp_obj_t self_in) {
mp_obj_t ret;
switch (mp_obj_gen_resume(self_in, mp_const_none, mp_const_GeneratorExit, &ret)) {
switch (mp_obj_gen_resume(self_in, mp_const_none, (mp_obj_t)&mp_const_GeneratorExit_obj, &ret)) {
case MP_VM_RETURN_YIELD:
nlr_jump(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));

View File

@ -119,7 +119,7 @@ STATIC mp_obj_t namedtuple_make_new(mp_obj_t type_in, uint n_args, uint n_kw, co
return tuple;
}
STATIC const mp_obj_tuple_t namedtuple_base_tuple = {{&tuple_type}, 1, {(mp_obj_t)&tuple_type}};
STATIC const mp_obj_tuple_t namedtuple_base_tuple = {{&mp_type_tuple}, 1, {(mp_obj_t)&mp_type_tuple}};
mp_obj_t mp_obj_new_namedtuple_type(qstr name, const char *fields) {
mp_obj_namedtuple_type_t *o = m_new0(mp_obj_namedtuple_type_t, 1);

View File

@ -22,12 +22,11 @@ STATIC mp_obj_t none_unary_op(int op, mp_obj_t o_in) {
}
}
const mp_obj_type_t none_type = {
const mp_obj_type_t mp_type_NoneType = {
{ &mp_type_type },
.name = MP_QSTR_NoneType,
.print = none_print,
.unary_op = none_unary_op,
};
STATIC const mp_obj_none_t none_obj = {{&none_type}};
const mp_obj_t mp_const_none = (mp_obj_t)&none_obj;
const mp_obj_none_t mp_const_none_obj = {{&mp_type_NoneType}};

View File

@ -19,14 +19,13 @@ void ellipsis_print(void (*print)(void *env, const char *fmt, ...), void *env, m
print(env, "Ellipsis");
}
const mp_obj_type_t ellipsis_type = {
const mp_obj_type_t mp_type_ellipsis = {
{ &mp_type_type },
.name = MP_QSTR_Ellipsis,
.print = ellipsis_print,
};
STATIC const mp_obj_ellipsis_t ellipsis_obj = {{&ellipsis_type}};
const mp_obj_t mp_const_ellipsis = (mp_obj_t)&ellipsis_obj;
const mp_obj_ellipsis_t mp_const_ellipsis_obj = {{&mp_type_ellipsis}};
/******************************************************************************/
/* slice object */

View File

@ -309,7 +309,7 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
// process args
uint seq_len;
mp_obj_t *seq_items;
if (MP_OBJ_IS_TYPE(arg, &tuple_type)) {
if (MP_OBJ_IS_TYPE(arg, &mp_type_tuple)) {
mp_obj_tuple_get(arg, &seq_len, &seq_items);
} else if (MP_OBJ_IS_TYPE(arg, &list_type)) {
mp_obj_list_get(arg, &seq_len, &seq_items);

View File

@ -42,7 +42,7 @@ STATIC mp_obj_t tuple_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m
case 1:
{
// 1 argument, an iterable from which we make a new tuple
if (MP_OBJ_IS_TYPE(args[0], &tuple_type)) {
if (MP_OBJ_IS_TYPE(args[0], &mp_type_tuple)) {
return args[0];
}
@ -75,8 +75,8 @@ STATIC mp_obj_t tuple_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m
// Don't pass RT_BINARY_OP_NOT_EQUAL here
STATIC bool tuple_cmp_helper(int op, mp_obj_t self_in, mp_obj_t another_in) {
assert(MP_OBJ_IS_TYPE(self_in, &tuple_type));
if (!MP_OBJ_IS_TYPE(another_in, &tuple_type)) {
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_tuple));
if (!MP_OBJ_IS_TYPE(another_in, &mp_type_tuple)) {
return false;
}
mp_obj_tuple_t *self = self_in;
@ -115,7 +115,7 @@ mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
}
case RT_BINARY_OP_ADD:
{
if (!mp_obj_is_subclass_fast(mp_obj_get_type(rhs), (mp_obj_t)&tuple_type)) {
if (!mp_obj_is_subclass_fast(mp_obj_get_type(rhs), (mp_obj_t)&mp_type_tuple)) {
return NULL;
}
mp_obj_tuple_t *p = rhs;
@ -153,14 +153,14 @@ STATIC mp_obj_t tuple_getiter(mp_obj_t o_in) {
}
STATIC mp_obj_t tuple_count(mp_obj_t self_in, mp_obj_t value) {
assert(MP_OBJ_IS_TYPE(self_in, &tuple_type));
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_tuple));
mp_obj_tuple_t *self = self_in;
return mp_seq_count_obj(self->items, self->len, value);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(tuple_count_obj, tuple_count);
STATIC mp_obj_t tuple_index(uint n_args, const mp_obj_t *args) {
assert(MP_OBJ_IS_TYPE(args[0], &tuple_type));
assert(MP_OBJ_IS_TYPE(args[0], &mp_type_tuple));
mp_obj_tuple_t *self = args[0];
return mp_seq_index_obj(self->items, self->len, n_args, args);
}
@ -173,7 +173,7 @@ STATIC const mp_map_elem_t tuple_locals_dict_table[] = {
STATIC MP_DEFINE_CONST_DICT(tuple_locals_dict, tuple_locals_dict_table);
const mp_obj_type_t tuple_type = {
const mp_obj_type_t mp_type_tuple = {
{ &mp_type_type },
.name = MP_QSTR_tuple,
.print = tuple_print,
@ -185,15 +185,14 @@ const mp_obj_type_t tuple_type = {
};
// the zero-length tuple
STATIC const mp_obj_tuple_t empty_tuple_obj = {{&tuple_type}, 0};
const mp_obj_t mp_const_empty_tuple = (mp_obj_t)&empty_tuple_obj;
const mp_obj_tuple_t mp_const_empty_tuple_obj = {{&mp_type_tuple}, 0};
mp_obj_t mp_obj_new_tuple(uint n, const mp_obj_t *items) {
if (n == 0) {
return mp_const_empty_tuple;
}
mp_obj_tuple_t *o = m_new_obj_var(mp_obj_tuple_t, mp_obj_t, n);
o->base.type = &tuple_type;
o->base.type = &mp_type_tuple;
o->len = n;
if (items) {
for (int i = 0; i < n; i++) {
@ -204,7 +203,7 @@ mp_obj_t mp_obj_new_tuple(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));
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_tuple));
mp_obj_tuple_t *self = self_in;
if (len) {
*len = self->len;
@ -215,13 +214,13 @@ 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) {
assert(MP_OBJ_IS_TYPE(self_in, &tuple_type));
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_tuple));
mp_obj_tuple_t *self = self_in;
m_del_var(mp_obj_tuple_t, mp_obj_t, self->len, self);
}
machine_int_t mp_obj_tuple_hash(mp_obj_t self_in) {
assert(MP_OBJ_IS_TYPE(self_in, &tuple_type));
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_tuple));
mp_obj_tuple_t *self = self_in;
// start hash with pointer to empty tuple, to make it fairly unique
machine_int_t hash = (machine_int_t)mp_const_empty_tuple;

View File

@ -348,7 +348,7 @@ const mp_obj_type_t mp_type_type = {
};
mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) {
assert(MP_OBJ_IS_TYPE(bases_tuple, &tuple_type)); // Micro Python restriction, for now
assert(MP_OBJ_IS_TYPE(bases_tuple, &mp_type_tuple)); // Micro Python restriction, for now
assert(MP_OBJ_IS_TYPE(locals_dict, &dict_type)); // Micro Python restriction, for now
mp_obj_type_t *o = m_new0(mp_obj_type_t, 1);
o->base.type = &mp_type_type;
@ -502,7 +502,7 @@ STATIC mp_obj_t mp_obj_is_subclass(mp_obj_t object, mp_obj_t classinfo) {
if (MP_OBJ_IS_TYPE(classinfo, &mp_type_type)) {
len = 1;
items = &classinfo;
} else if (MP_OBJ_IS_TYPE(classinfo, &tuple_type)) {
} else if (MP_OBJ_IS_TYPE(classinfo, &mp_type_tuple)) {
mp_obj_tuple_get(classinfo, &len, &items);
} else {
nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "issubclass() arg 2 must be a class or a tuple of classes"));

View File

@ -51,7 +51,6 @@ void rt_init(void) {
// add some builtins that can't be done in ROM
mp_map_add_qstr(map_globals, MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR___main__));
mp_map_add_qstr(&map_builtins, MP_QSTR_Ellipsis, mp_const_ellipsis);
#if MICROPY_CPYTHON_COMPAT
// Precreate sys module, so "import sys" didn't throw exceptions.
@ -559,9 +558,9 @@ mp_obj_t rt_store_set(mp_obj_t set, mp_obj_t item) {
// unpacked items are stored in reverse order into the array pointed to by items
void rt_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
uint seq_len;
if (MP_OBJ_IS_TYPE(seq_in, &tuple_type) || MP_OBJ_IS_TYPE(seq_in, &list_type)) {
if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(seq_in, &list_type)) {
mp_obj_t *seq_items;
if (MP_OBJ_IS_TYPE(seq_in, &tuple_type)) {
if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple)) {
mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
} else {
mp_obj_list_get(seq_in, &seq_len, &seq_items);

View File

@ -173,7 +173,7 @@ dispatch_loop:
break;
case MP_BC_LOAD_CONST_ELLIPSIS:
PUSH(mp_const_ellipsis);
PUSH((mp_obj_t)&mp_const_ellipsis_obj);
break;
case MP_BC_LOAD_CONST_SMALL_INT: {