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

@@ -104,6 +104,12 @@ STATIC mp_obj_t list_unary_op(mp_uint_t op, mp_obj_t self_in) {
switch (op) {
case MP_UNARY_OP_BOOL: return mp_obj_new_bool(self->len != 0);
case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->len);
#if MICROPY_PY_SYS_GETSIZEOF
case MP_UNARY_OP_SIZEOF: {
size_t sz = sizeof(*self) + sizeof(mp_obj_t) * self->alloc;
return MP_OBJ_NEW_SMALL_INT(sz);
}
#endif
default: return MP_OBJ_NULL; // op not supported
}
}