Skip to content

Commit 07ddab5

Browse files
committed
py: Change mp_const_* objects to macros.
Addresses issue adafruit#388.
1 parent da51a39 commit 07ddab5

File tree

15 files changed

+57
-57
lines changed

15 files changed

+57
-57
lines changed

py/builtintables.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ STATIC const mp_builtin_elem_t builtin_object_table[] = {
2525
{ MP_QSTR___repl_print__, (mp_obj_t)&mp_builtin___repl_print___obj },
2626

2727
// built-in types
28-
{ MP_QSTR_bool, (mp_obj_t)&bool_type },
28+
{ MP_QSTR_bool, (mp_obj_t)&mp_type_bool },
2929
{ MP_QSTR_bytes, (mp_obj_t)&bytes_type },
3030
#if MICROPY_ENABLE_FLOAT
3131
{ MP_QSTR_complex, (mp_obj_t)&mp_type_complex },
@@ -43,13 +43,16 @@ STATIC const mp_builtin_elem_t builtin_object_table[] = {
4343
{ MP_QSTR_set, (mp_obj_t)&set_type },
4444
{ MP_QSTR_str, (mp_obj_t)&str_type },
4545
{ MP_QSTR_super, (mp_obj_t)&super_type },
46-
{ MP_QSTR_tuple, (mp_obj_t)&tuple_type },
46+
{ MP_QSTR_tuple, (mp_obj_t)&mp_type_tuple },
4747
{ MP_QSTR_type, (mp_obj_t)&mp_type_type },
4848
{ MP_QSTR_zip, (mp_obj_t)&zip_type },
4949

5050
{ MP_QSTR_classmethod, (mp_obj_t)&mp_type_classmethod },
5151
{ MP_QSTR_staticmethod, (mp_obj_t)&mp_type_staticmethod },
5252

53+
// built-in objects
54+
{ MP_QSTR_Ellipsis, (mp_obj_t)&mp_const_ellipsis_obj },
55+
5356
// built-in user functions
5457
{ MP_QSTR_abs, (mp_obj_t)&mp_builtin_abs_obj },
5558
{ MP_QSTR_all, (mp_obj_t)&mp_builtin_all_obj },

py/obj.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ machine_int_t mp_obj_hash(mp_obj_t o_in) {
7979
return MP_OBJ_SMALL_INT_VALUE(o_in);
8080
} else if (MP_OBJ_IS_STR(o_in)) {
8181
return mp_obj_str_get_hash(o_in);
82-
} else if (MP_OBJ_IS_TYPE(o_in, &none_type)) {
82+
} else if (MP_OBJ_IS_TYPE(o_in, &mp_type_NoneType)) {
8383
return (machine_int_t)o_in;
8484
} else if (MP_OBJ_IS_TYPE(o_in, &fun_native_type) || MP_OBJ_IS_TYPE(o_in, &fun_bc_type)) {
8585
return (machine_int_t)o_in;
86-
} else if (MP_OBJ_IS_TYPE(o_in, &tuple_type)) {
86+
} else if (MP_OBJ_IS_TYPE(o_in, &mp_type_tuple)) {
8787
return mp_obj_tuple_hash(o_in);
8888

8989
// 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) {
207207
#endif
208208

209209
void mp_obj_get_array(mp_obj_t o, uint *len, mp_obj_t **items) {
210-
if (MP_OBJ_IS_TYPE(o, &tuple_type)) {
210+
if (MP_OBJ_IS_TYPE(o, &mp_type_tuple)) {
211211
mp_obj_tuple_get(o, len, items);
212212
} else if (MP_OBJ_IS_TYPE(o, &list_type)) {
213213
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) {
217217
}
218218

219219
void mp_obj_get_array_fixed_n(mp_obj_t o, uint len, mp_obj_t **items) {
220-
if (MP_OBJ_IS_TYPE(o, &tuple_type) || MP_OBJ_IS_TYPE(o, &list_type)) {
220+
if (MP_OBJ_IS_TYPE(o, &mp_type_tuple) || MP_OBJ_IS_TYPE(o, &list_type)) {
221221
uint seq_len;
222-
if (MP_OBJ_IS_TYPE(o, &tuple_type)) {
222+
if (MP_OBJ_IS_TYPE(o, &mp_type_tuple)) {
223223
mp_obj_tuple_get(o, &seq_len, items);
224224
} else {
225225
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,
237237
int i;
238238
if (MP_OBJ_IS_SMALL_INT(index)) {
239239
i = MP_OBJ_SMALL_INT_VALUE(index);
240-
} else if (MP_OBJ_IS_TYPE(index, &bool_type)) {
240+
} else if (MP_OBJ_IS_TYPE(index, &mp_type_bool)) {
241241
i = (index == mp_const_true ? 1 : 0);
242242
} else {
243243
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)));

py/obj.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,17 @@ extern const mp_obj_type_t mp_type_ValueError;
226226
extern const mp_obj_type_t mp_type_ZeroDivisionError;
227227

228228
// Constant objects, globally accessible
229-
extern const mp_obj_t mp_const_none;
230-
extern const mp_obj_t mp_const_false;
231-
extern const mp_obj_t mp_const_true;
232-
extern const mp_obj_t mp_const_empty_tuple;
233-
extern const mp_obj_t mp_const_ellipsis;
234-
extern const mp_obj_t mp_const_GeneratorExit;
229+
// The macros are for convenience only
230+
#define mp_const_none ((mp_obj_t)&mp_const_none_obj)
231+
#define mp_const_false ((mp_obj_t)&mp_const_false_obj)
232+
#define mp_const_true ((mp_obj_t)&mp_const_true_obj)
233+
#define mp_const_empty_tuple ((mp_obj_t)&mp_const_empty_tuple_obj)
234+
extern const struct _mp_obj_none_t mp_const_none_obj;
235+
extern const struct _mp_obj_bool_t mp_const_false_obj;
236+
extern const struct _mp_obj_bool_t mp_const_true_obj;
237+
extern const struct _mp_obj_tuple_t mp_const_empty_tuple_obj;
238+
extern const struct _mp_obj_ellipsis_t mp_const_ellipsis_obj;
239+
extern const struct _mp_obj_exception_t mp_const_GeneratorExit_obj;
235240

236241
// General API for objects
237242

@@ -298,10 +303,10 @@ mp_obj_t mp_obj_len_maybe(mp_obj_t o_in); /* may return NULL */
298303
extern const mp_obj_type_t mp_type_object;
299304

300305
// none
301-
extern const mp_obj_type_t none_type;
306+
extern const mp_obj_type_t mp_type_NoneType;
302307

303308
// bool
304-
extern const mp_obj_type_t bool_type;
309+
extern const mp_obj_type_t mp_type_bool;
305310
#define MP_BOOL(x) (x ? mp_const_true : mp_const_false)
306311

307312
// 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
359364
#endif
360365

361366
// tuple
362-
extern const mp_obj_type_t tuple_type;
367+
extern const mp_obj_type_t mp_type_tuple;
363368
void mp_obj_tuple_get(mp_obj_t self_in, uint *len, mp_obj_t **items);
364369
void mp_obj_tuple_del(mp_obj_t self_in);
365370
machine_int_t mp_obj_tuple_hash(mp_obj_t self_in);

py/objbool.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,13 @@ STATIC mp_obj_t bool_unary_op(int op, mp_obj_t o_in) {
4343
}
4444
}
4545

46-
const mp_obj_type_t bool_type = {
46+
const mp_obj_type_t mp_type_bool = {
4747
{ &mp_type_type },
4848
.name = MP_QSTR_bool,
4949
.print = bool_print,
5050
.make_new = bool_make_new,
5151
.unary_op = bool_unary_op,
5252
};
5353

54-
STATIC const mp_obj_bool_t false_obj = {{&bool_type}, false};
55-
STATIC const mp_obj_bool_t true_obj = {{&bool_type}, true};
56-
57-
const mp_obj_t mp_const_false = (mp_obj_t)&false_obj;
58-
const mp_obj_t mp_const_true = (mp_obj_t)&true_obj;
54+
const mp_obj_bool_t mp_const_false_obj = {{&mp_type_bool}, false};
55+
const mp_obj_bool_t mp_const_true_obj = {{&mp_type_bool}, true};

py/objexcept.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// This is unified class for C-level and Python-level exceptions
1515
// Python-level exceptions have empty ->msg and all arguments are in
1616
// args tuple. C-level exceptions likely have ->msg set, and args is empty.
17-
typedef struct mp_obj_exception_t {
17+
typedef struct _mp_obj_exception_t {
1818
mp_obj_base_t base;
1919
mp_obj_t traceback; // a list object, holding (file,line,block) as numbers (not Python objects); a hack for now
2020
vstr_t *msg;
@@ -24,8 +24,7 @@ typedef struct mp_obj_exception_t {
2424
// Instance of GeneratorExit exception - needed by generator.close()
2525
// This would belong to objgenerator.c, but to keep mp_obj_exception_t
2626
// definition module-private so far, have it here.
27-
STATIC mp_obj_exception_t GeneratorExit_obj = {{&mp_type_GeneratorExit}, MP_OBJ_NULL, NULL, {{&tuple_type}, 0}};
28-
const mp_obj_t mp_const_GeneratorExit = (mp_obj_t)&GeneratorExit_obj;
27+
const mp_obj_exception_t mp_const_GeneratorExit_obj = {{&mp_type_GeneratorExit}, MP_OBJ_NULL, NULL, {{&mp_type_tuple}, 0}};
2928

3029
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) {
3130
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_
6160
o->base.type = type;
6261
o->traceback = MP_OBJ_NULL;
6362
o->msg = NULL;
64-
o->args.base.type = &tuple_type;
63+
o->args.base.type = &mp_type_tuple;
6564
o->args.len = n_args;
6665
memcpy(o->args.items, args, n_args * sizeof(mp_obj_t));
6766
return o;
@@ -95,7 +94,7 @@ const mp_obj_type_t mp_type_BaseException = {
9594
};
9695

9796
#define MP_DEFINE_EXCEPTION_BASE(base_name) \
98-
STATIC const mp_obj_tuple_t mp_type_ ## base_name ## _base_tuple = {{&tuple_type}, 1, {(mp_obj_t)&mp_type_ ## base_name}};\
97+
STATIC const mp_obj_tuple_t mp_type_ ## base_name ## _base_tuple = {{&mp_type_tuple}, 1, {(mp_obj_t)&mp_type_ ## base_name}};\
9998

10099
#define MP_DEFINE_EXCEPTION(exc_name, base_name) \
101100
const mp_obj_type_t mp_type_ ## exc_name = { \

py/objfun.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ STATIC machine_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
373373
// convert float to int (could also pass in float registers)
374374
return (machine_int_t)mp_obj_float_get(obj);
375375
#endif
376-
} else if (MP_OBJ_IS_TYPE(obj, &tuple_type)) {
376+
} else if (MP_OBJ_IS_TYPE(obj, &mp_type_tuple)) {
377377
// pointer to start of tuple (could pass length, but then could use len(x) for that)
378378
uint len;
379379
mp_obj_t *items;

py/objgenerator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gen_instance_throw_obj, 2, 4, gen_ins
171171

172172
STATIC mp_obj_t gen_instance_close(mp_obj_t self_in) {
173173
mp_obj_t ret;
174-
switch (mp_obj_gen_resume(self_in, mp_const_none, mp_const_GeneratorExit, &ret)) {
174+
switch (mp_obj_gen_resume(self_in, mp_const_none, (mp_obj_t)&mp_const_GeneratorExit_obj, &ret)) {
175175
case MP_VM_RETURN_YIELD:
176176
nlr_jump(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));
177177

py/objnamedtuple.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ STATIC mp_obj_t namedtuple_make_new(mp_obj_t type_in, uint n_args, uint n_kw, co
119119
return tuple;
120120
}
121121

122-
STATIC const mp_obj_tuple_t namedtuple_base_tuple = {{&tuple_type}, 1, {(mp_obj_t)&tuple_type}};
122+
STATIC const mp_obj_tuple_t namedtuple_base_tuple = {{&mp_type_tuple}, 1, {(mp_obj_t)&mp_type_tuple}};
123123

124124
mp_obj_t mp_obj_new_namedtuple_type(qstr name, const char *fields) {
125125
mp_obj_namedtuple_type_t *o = m_new0(mp_obj_namedtuple_type_t, 1);

py/objnone.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@ STATIC mp_obj_t none_unary_op(int op, mp_obj_t o_in) {
2222
}
2323
}
2424

25-
const mp_obj_type_t none_type = {
25+
const mp_obj_type_t mp_type_NoneType = {
2626
{ &mp_type_type },
2727
.name = MP_QSTR_NoneType,
2828
.print = none_print,
2929
.unary_op = none_unary_op,
3030
};
3131

32-
STATIC const mp_obj_none_t none_obj = {{&none_type}};
33-
const mp_obj_t mp_const_none = (mp_obj_t)&none_obj;
32+
const mp_obj_none_t mp_const_none_obj = {{&mp_type_NoneType}};

py/objslice.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ void ellipsis_print(void (*print)(void *env, const char *fmt, ...), void *env, m
1919
print(env, "Ellipsis");
2020
}
2121

22-
const mp_obj_type_t ellipsis_type = {
22+
const mp_obj_type_t mp_type_ellipsis = {
2323
{ &mp_type_type },
2424
.name = MP_QSTR_Ellipsis,
2525
.print = ellipsis_print,
2626
};
2727

28-
STATIC const mp_obj_ellipsis_t ellipsis_obj = {{&ellipsis_type}};
29-
const mp_obj_t mp_const_ellipsis = (mp_obj_t)&ellipsis_obj;
28+
const mp_obj_ellipsis_t mp_const_ellipsis_obj = {{&mp_type_ellipsis}};
3029

3130
/******************************************************************************/
3231
/* slice object */

0 commit comments

Comments
 (0)