Skip to content

Commit a71c83a

Browse files
committed
Change mp_obj_type_t.name from const char * to qstr.
Ultimately all static strings should be qstr. This entry in the type structure is only used for printing error messages (to tell the type of the bad argument), and printing objects that don't supply a .print method.
1 parent 8ac72b9 commit a71c83a

47 files changed

Lines changed: 100 additions & 77 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

py/makeqstrdata.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
elif platform.python_version_tuple()[0] == '3':
99
from html.entities import codepoint2name
1010

11+
# add some custom names to map characters that aren't in HTML
12+
codepoint2name[ord('.')] = 'dot'
13+
1114
# this must match the equivalent function in qstr.c
1215
def compute_hash(qstr):
1316
hash = 0

py/obj.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ mp_obj_type_t *mp_obj_get_type(mp_obj_t o_in) {
2626
}
2727

2828
const char *mp_obj_get_type_str(mp_obj_t o_in) {
29-
return mp_obj_get_type(o_in)->name;
29+
return qstr_str(mp_obj_get_type(o_in)->name);
3030
}
3131

3232
void printf_wrapper(void *env, const char *fmt, ...) {
@@ -41,7 +41,7 @@ void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *e
4141
if (type->print != NULL) {
4242
type->print(print, env, o_in, kind);
4343
} else {
44-
print(env, "<%s>", type->name);
44+
print(env, "<%s>", qstr_str(type->name));
4545
}
4646
}
4747

@@ -226,11 +226,11 @@ uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index)
226226
i += len;
227227
}
228228
if (i < 0 || i >= len) {
229-
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_IndexError, "%s index out of range", type->name));
229+
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_IndexError, "%s index out of range", qstr_str(type->name)));
230230
}
231231
return i;
232232
} else {
233-
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "%s indices must be integers, not %s", type->name, mp_obj_get_type_str(index)));
233+
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "%s indices must be integers, not %s", qstr_str(type->name), mp_obj_get_type_str(index)));
234234
}
235235
}
236236

py/obj.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,20 @@ typedef struct _mp_stream_p_t {
142142

143143
struct _mp_obj_type_t {
144144
mp_obj_base_t base;
145-
const char *name;
145+
qstr name;
146146
mp_print_fun_t print;
147147
mp_make_new_fun_t make_new; // to make an instance of the type
148148

149149
mp_call_fun_t call;
150150
mp_unary_op_fun_t unary_op; // can return NULL if op not supported
151151
mp_binary_op_fun_t binary_op; // can return NULL if op not supported
152152

153+
mp_load_attr_fun_t load_attr;
154+
mp_store_attr_fun_t store_attr;
155+
// Implements container[index] = val; note that load_item is implemented
156+
// by binary_op(RT_BINARY_OP_SUBSCR)
157+
mp_store_item_fun_t store_item;
158+
153159
mp_fun_1_t getiter;
154160
mp_fun_1_t iternext;
155161

@@ -161,12 +167,6 @@ struct _mp_obj_type_t {
161167

162168
const mp_method_t *methods;
163169

164-
mp_load_attr_fun_t load_attr;
165-
mp_store_attr_fun_t store_attr;
166-
// Implements container[index] = val; note that load_item is implemented
167-
// by binary_op(RT_BINARY_OP_SUBSCR)
168-
mp_store_item_fun_t store_item;
169-
170170
// these are for dynamically created types (classes)
171171
mp_obj_t bases_tuple;
172172
mp_obj_t locals_dict;
@@ -200,7 +200,7 @@ extern const mp_obj_t mp_const_stop_iteration; // special object indicating end
200200

201201
// General API for objects
202202

203-
mp_obj_t mp_obj_new_type(const char *name, mp_obj_t bases_tuple, mp_obj_t locals_dict);
203+
mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict);
204204
mp_obj_t mp_obj_new_none(void);
205205
mp_obj_t mp_obj_new_bool(bool value);
206206
mp_obj_t mp_obj_new_cell(mp_obj_t obj);

py/objarray.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ STATIC const mp_method_t array_type_methods[] = {
161161

162162
const mp_obj_type_t array_type = {
163163
{ &mp_const_type },
164-
"array",
164+
.name = MP_QSTR_array,
165165
.print = array_print,
166166
.make_new = array_make_new,
167167
.getiter = array_iterator_new,
@@ -223,7 +223,7 @@ mp_obj_t array_it_iternext(mp_obj_t self_in) {
223223

224224
STATIC const mp_obj_type_t array_it_type = {
225225
{ &mp_const_type },
226-
"array_iterator",
226+
.name = MP_QSTR_iterator,
227227
.iternext = array_it_iternext,
228228
};
229229

py/objbool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ STATIC mp_obj_t bool_unary_op(int op, mp_obj_t o_in) {
4747

4848
const mp_obj_type_t bool_type = {
4949
{ &mp_const_type },
50-
"bool",
50+
.name = MP_QSTR_bool,
5151
.print = bool_print,
5252
.make_new = bool_make_new,
5353
.unary_op = bool_unary_op,

py/objboundmeth.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ mp_obj_t bound_meth_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_
4141

4242
const mp_obj_type_t bound_meth_type = {
4343
{ &mp_const_type },
44-
"bound_method",
44+
.name = MP_QSTR_bound_method,
4545
.call = bound_meth_call,
4646
};
4747

py/objcell.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj) {
2626

2727
const mp_obj_type_t cell_type = {
2828
{ &mp_const_type },
29-
"cell",
29+
.name = MP_QSTR_, // should never need to print cell type
3030
};
3131

3232
mp_obj_t mp_obj_new_cell(mp_obj_t obj) {

py/objclosure.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ mp_obj_t closure_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *
4242

4343
const mp_obj_type_t closure_type = {
4444
{ &mp_const_type },
45-
"closure",
45+
.name = MP_QSTR_closure,
4646
.call = closure_call,
4747
};
4848

py/objcomplex.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ STATIC mp_obj_t complex_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
8787

8888
const mp_obj_type_t complex_type = {
8989
{ &mp_const_type },
90-
"complex",
90+
.name = MP_QSTR_complex,
9191
.print = complex_print,
9292
.make_new = complex_make_new,
9393
.unary_op = complex_unary_op,

py/objdict.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ mp_obj_t dict_it_iternext(mp_obj_t self_in) {
113113

114114
STATIC const mp_obj_type_t dict_it_type = {
115115
{ &mp_const_type },
116-
"dict_iterator",
116+
.name = MP_QSTR_iterator,
117117
.iternext = dict_it_iternext,
118118
};
119119

@@ -342,7 +342,7 @@ STATIC mp_obj_t dict_view_it_iternext(mp_obj_t self_in) {
342342

343343
STATIC const mp_obj_type_t dict_view_it_type = {
344344
{ &mp_const_type },
345-
"dict_view_iterator",
345+
.name = MP_QSTR_iterator,
346346
.iternext = dict_view_it_iternext,
347347
.methods = NULL, /* set operations still to come */
348348
};
@@ -386,7 +386,7 @@ STATIC mp_obj_t dict_view_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
386386

387387
STATIC const mp_obj_type_t dict_view_type = {
388388
{ &mp_const_type },
389-
"dict_view",
389+
.name = MP_QSTR_dict_view,
390390
.print = dict_view_print,
391391
.binary_op = dict_view_binary_op,
392392
.getiter = dict_view_getiter,
@@ -441,7 +441,7 @@ STATIC const mp_method_t dict_type_methods[] = {
441441

442442
const mp_obj_type_t dict_type = {
443443
{ &mp_const_type },
444-
"dict",
444+
.name = MP_QSTR_dict,
445445
.print = dict_print,
446446
.make_new = dict_make_new,
447447
.unary_op = dict_unary_op,

0 commit comments

Comments
 (0)