Skip to content

Commit 68e7c51

Browse files
committed
py: Factor out impl of special methods for builtin types into opmethods.c
1 parent 036ad76 commit 68e7c51

File tree

5 files changed

+28
-15
lines changed

5 files changed

+28
-15
lines changed

py/builtin.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sum_obj);
3535

3636
MP_DECLARE_CONST_FUN_OBJ(mp_namedtuple_obj);
3737

38+
MP_DECLARE_CONST_FUN_OBJ(mp_op_contains_obj);
39+
MP_DECLARE_CONST_FUN_OBJ(mp_op_getitem_obj);
40+
3841
extern const mp_obj_module_t mp_module___main__;
3942
extern const mp_obj_module_t mp_module_array;
4043
extern const mp_obj_module_t mp_module_collections;

py/objdict.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "objtuple.h"
1111
#include "runtime0.h"
1212
#include "runtime.h"
13+
#include "builtin.h"
1314

1415
STATIC mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur);
1516
STATIC mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in);
@@ -137,12 +138,6 @@ STATIC bool dict_store_item(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
137138
return true;
138139
}
139140

140-
STATIC mp_obj_t dict_getitem(mp_obj_t lhs_in, mp_obj_t rhs_in) {
141-
return dict_binary_op(MP_BINARY_OP_SUBSCR, lhs_in, rhs_in);
142-
}
143-
144-
STATIC MP_DEFINE_CONST_FUN_OBJ_2(dict_getitem_obj, dict_getitem);
145-
146141
/******************************************************************************/
147142
/* dict iterator */
148143

@@ -501,7 +496,7 @@ STATIC const mp_map_elem_t dict_locals_dict_table[] = {
501496
{ MP_OBJ_NEW_QSTR(MP_QSTR_setdefault), (mp_obj_t)&dict_setdefault_obj },
502497
{ MP_OBJ_NEW_QSTR(MP_QSTR_update), (mp_obj_t)&dict_update_obj },
503498
{ MP_OBJ_NEW_QSTR(MP_QSTR_values), (mp_obj_t)&dict_values_obj },
504-
{ MP_OBJ_NEW_QSTR(MP_QSTR___getitem__), (mp_obj_t)&dict_getitem_obj },
499+
{ MP_OBJ_NEW_QSTR(MP_QSTR___getitem__), (mp_obj_t)&mp_op_getitem_obj },
505500
};
506501

507502
STATIC MP_DEFINE_CONST_DICT(dict_locals_dict, dict_locals_dict_table);

py/objset.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "obj.h"
1010
#include "runtime.h"
1111
#include "runtime0.h"
12+
#include "builtin.h"
1213

1314
typedef struct _mp_obj_set_t {
1415
mp_obj_base_t base;
@@ -423,13 +424,6 @@ STATIC mp_obj_t set_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
423424
}
424425
}
425426

426-
// TODO: Move to common file
427-
STATIC mp_obj_t set_contains(mp_obj_t lhs_in, mp_obj_t rhs_in) {
428-
return set_binary_op(MP_BINARY_OP_IN, lhs_in, rhs_in);
429-
}
430-
431-
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_contains_obj, set_contains);
432-
433427
/******************************************************************************/
434428
/* set constructors & public C API */
435429

@@ -452,7 +446,7 @@ STATIC const mp_map_elem_t set_locals_dict_table[] = {
452446
{ MP_OBJ_NEW_QSTR(MP_QSTR_symmetric_difference_update), (mp_obj_t)&set_symmetric_difference_update_obj },
453447
{ MP_OBJ_NEW_QSTR(MP_QSTR_union), (mp_obj_t)&set_union_obj },
454448
{ MP_OBJ_NEW_QSTR(MP_QSTR_update), (mp_obj_t)&set_update_obj },
455-
{ MP_OBJ_NEW_QSTR(MP_QSTR___contains__), (mp_obj_t)&set_contains_obj },
449+
{ MP_OBJ_NEW_QSTR(MP_QSTR___contains__), (mp_obj_t)&mp_op_contains_obj },
456450
};
457451

458452
STATIC MP_DEFINE_CONST_DICT(set_locals_dict, set_locals_dict_table);

py/opmethods.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "nlr.h"
2+
#include "misc.h"
3+
#include "mpconfig.h"
4+
#include "qstr.h"
5+
#include "obj.h"
6+
#include "runtime.h"
7+
#include "runtime0.h"
8+
#include "builtin.h"
9+
10+
STATIC mp_obj_t op_getitem(mp_obj_t lhs_in, mp_obj_t rhs_in) {
11+
mp_obj_type_t *type = mp_obj_get_type(lhs_in);
12+
return type->binary_op(MP_BINARY_OP_SUBSCR, lhs_in, rhs_in);
13+
}
14+
MP_DEFINE_CONST_FUN_OBJ_2(mp_op_getitem_obj, op_getitem);
15+
16+
STATIC mp_obj_t op_contains(mp_obj_t lhs_in, mp_obj_t rhs_in) {
17+
mp_obj_type_t *type = mp_obj_get_type(lhs_in);
18+
return type->binary_op(MP_BINARY_OP_IN, lhs_in, rhs_in);
19+
}
20+
MP_DEFINE_CONST_FUN_OBJ_2(mp_op_contains_obj, op_contains);

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ PY_O_BASENAME = \
6969
objtuple.o \
7070
objtype.o \
7171
objzip.o \
72+
opmethods.o \
7273
sequence.o \
7374
stream.o \
7475
binary.o \

0 commit comments

Comments
 (0)