py/modsys: Initial implementation of sys.getsizeof().

Implemented as a new MP_UNARY_OP. This patch adds support lists, dicts and
instances.
This commit is contained in:
Paul Sokolovsky
2017-08-11 09:42:39 +03:00
parent 7d4a2f773c
commit bfc2092dc5
8 changed files with 49 additions and 1 deletions

View File

@@ -339,11 +339,27 @@ const qstr mp_unary_op_method_name[] = {
[MP_UNARY_OP_NEGATIVE] = MP_QSTR___neg__,
[MP_UNARY_OP_INVERT] = MP_QSTR___invert__,
#endif
#if MICROPY_PY_SYS_GETSIZEOF
[MP_UNARY_OP_SIZEOF] = MP_QSTR_getsizeof,
#endif
[MP_UNARY_OP_NOT] = MP_QSTR_, // don't need to implement this, used to make sure array has full size
};
STATIC mp_obj_t instance_unary_op(mp_uint_t op, mp_obj_t self_in) {
mp_obj_instance_t *self = MP_OBJ_TO_PTR(self_in);
#if MICROPY_PY_SYS_GETSIZEOF
if (MP_UNLIKELY(op == MP_UNARY_OP_SIZEOF)) {
// TODO: This doesn't count inherited objects (self->subobj)
const mp_obj_type_t *native_base;
size_t num_native_bases = instance_count_native_bases(mp_obj_get_type(self_in), &native_base);
size_t sz = sizeof(*self) + sizeof(*self->subobj) * num_native_bases
+ sizeof(*self->members.table) * self->members.alloc;
return MP_OBJ_NEW_SMALL_INT(sz);
}
#endif
qstr op_name = mp_unary_op_method_name[op];
/* Still try to lookup native slot
if (op_name == 0) {