py: Factor out impl of special methods for builtin types into opmethods.c

This commit is contained in:
Paul Sokolovsky 2014-04-13 11:53:15 +03:00
parent 036ad76659
commit 68e7c5146c
5 changed files with 28 additions and 15 deletions

View File

@ -35,6 +35,9 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sum_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_namedtuple_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_op_contains_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_op_getitem_obj);
extern const mp_obj_module_t mp_module___main__;
extern const mp_obj_module_t mp_module_array;
extern const mp_obj_module_t mp_module_collections;

View File

@ -10,6 +10,7 @@
#include "objtuple.h"
#include "runtime0.h"
#include "runtime.h"
#include "builtin.h"
STATIC mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur);
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) {
return true;
}
STATIC mp_obj_t dict_getitem(mp_obj_t lhs_in, mp_obj_t rhs_in) {
return dict_binary_op(MP_BINARY_OP_SUBSCR, lhs_in, rhs_in);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(dict_getitem_obj, dict_getitem);
/******************************************************************************/
/* dict iterator */
@ -501,7 +496,7 @@ STATIC const mp_map_elem_t dict_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_setdefault), (mp_obj_t)&dict_setdefault_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_update), (mp_obj_t)&dict_update_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_values), (mp_obj_t)&dict_values_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR___getitem__), (mp_obj_t)&dict_getitem_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR___getitem__), (mp_obj_t)&mp_op_getitem_obj },
};
STATIC MP_DEFINE_CONST_DICT(dict_locals_dict, dict_locals_dict_table);

View File

@ -9,6 +9,7 @@
#include "obj.h"
#include "runtime.h"
#include "runtime0.h"
#include "builtin.h"
typedef struct _mp_obj_set_t {
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) {
}
}
// TODO: Move to common file
STATIC mp_obj_t set_contains(mp_obj_t lhs_in, mp_obj_t rhs_in) {
return set_binary_op(MP_BINARY_OP_IN, lhs_in, rhs_in);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_contains_obj, set_contains);
/******************************************************************************/
/* set constructors & public C API */
@ -452,7 +446,7 @@ STATIC const mp_map_elem_t set_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_symmetric_difference_update), (mp_obj_t)&set_symmetric_difference_update_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_union), (mp_obj_t)&set_union_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_update), (mp_obj_t)&set_update_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR___contains__), (mp_obj_t)&set_contains_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR___contains__), (mp_obj_t)&mp_op_contains_obj },
};
STATIC MP_DEFINE_CONST_DICT(set_locals_dict, set_locals_dict_table);

20
py/opmethods.c Normal file
View File

@ -0,0 +1,20 @@
#include "nlr.h"
#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
#include "runtime0.h"
#include "builtin.h"
STATIC mp_obj_t op_getitem(mp_obj_t lhs_in, mp_obj_t rhs_in) {
mp_obj_type_t *type = mp_obj_get_type(lhs_in);
return type->binary_op(MP_BINARY_OP_SUBSCR, lhs_in, rhs_in);
}
MP_DEFINE_CONST_FUN_OBJ_2(mp_op_getitem_obj, op_getitem);
STATIC mp_obj_t op_contains(mp_obj_t lhs_in, mp_obj_t rhs_in) {
mp_obj_type_t *type = mp_obj_get_type(lhs_in);
return type->binary_op(MP_BINARY_OP_IN, lhs_in, rhs_in);
}
MP_DEFINE_CONST_FUN_OBJ_2(mp_op_contains_obj, op_contains);

View File

@ -69,6 +69,7 @@ PY_O_BASENAME = \
objtuple.o \
objtype.o \
objzip.o \
opmethods.o \
sequence.o \
stream.o \
binary.o \