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:
parent
7d4a2f773c
commit
bfc2092dc5
12
py/modsys.c
12
py/modsys.c
@ -4,6 +4,7 @@
|
|||||||
* The MIT License (MIT)
|
* The MIT License (MIT)
|
||||||
*
|
*
|
||||||
* Copyright (c) 2013, 2014 Damien P. George
|
* Copyright (c) 2013, 2014 Damien P. George
|
||||||
|
* Copyright (c) 2014-2017 Paul Sokolovsky
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -31,8 +32,11 @@
|
|||||||
#include "py/objtuple.h"
|
#include "py/objtuple.h"
|
||||||
#include "py/objstr.h"
|
#include "py/objstr.h"
|
||||||
#include "py/objint.h"
|
#include "py/objint.h"
|
||||||
|
#include "py/objtype.h"
|
||||||
#include "py/stream.h"
|
#include "py/stream.h"
|
||||||
#include "py/smallint.h"
|
#include "py/smallint.h"
|
||||||
|
#include "py/runtime0.h"
|
||||||
|
#include "py/runtime.h"
|
||||||
|
|
||||||
#if MICROPY_PY_SYS
|
#if MICROPY_PY_SYS
|
||||||
|
|
||||||
@ -143,6 +147,11 @@ STATIC mp_obj_t mp_sys_exc_info(void) {
|
|||||||
MP_DEFINE_CONST_FUN_OBJ_0(mp_sys_exc_info_obj, mp_sys_exc_info);
|
MP_DEFINE_CONST_FUN_OBJ_0(mp_sys_exc_info_obj, mp_sys_exc_info);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
STATIC mp_obj_t mp_sys_getsizeof(mp_obj_t obj) {
|
||||||
|
return mp_unary_op(MP_UNARY_OP_SIZEOF, obj);
|
||||||
|
}
|
||||||
|
MP_DEFINE_CONST_FUN_OBJ_1(mp_sys_getsizeof_obj, mp_sys_getsizeof);
|
||||||
|
|
||||||
STATIC const mp_rom_map_elem_t mp_module_sys_globals_table[] = {
|
STATIC const mp_rom_map_elem_t mp_module_sys_globals_table[] = {
|
||||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_sys) },
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_sys) },
|
||||||
|
|
||||||
@ -192,6 +201,9 @@ STATIC const mp_rom_map_elem_t mp_module_sys_globals_table[] = {
|
|||||||
#if MICROPY_PY_SYS_EXC_INFO
|
#if MICROPY_PY_SYS_EXC_INFO
|
||||||
{ MP_ROM_QSTR(MP_QSTR_exc_info), MP_ROM_PTR(&mp_sys_exc_info_obj) },
|
{ MP_ROM_QSTR(MP_QSTR_exc_info), MP_ROM_PTR(&mp_sys_exc_info_obj) },
|
||||||
#endif
|
#endif
|
||||||
|
#if MICROPY_PY_SYS_GETSIZEOF
|
||||||
|
{ MP_ROM_QSTR(MP_QSTR_getsizeof), MP_ROM_PTR(&mp_sys_getsizeof_obj) },
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Extensions to CPython
|
* Extensions to CPython
|
||||||
|
@ -944,6 +944,11 @@ typedef double mp_float_t;
|
|||||||
#define MICROPY_PY_SYS_EXIT (1)
|
#define MICROPY_PY_SYS_EXIT (1)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Whether to provide "sys.getsizeof" function
|
||||||
|
#ifndef MICROPY_PY_SYS_GETSIZEOF
|
||||||
|
#define MICROPY_PY_SYS_GETSIZEOF (0)
|
||||||
|
#endif
|
||||||
|
|
||||||
// Whether to provide sys.{stdin,stdout,stderr} objects
|
// Whether to provide sys.{stdin,stdout,stderr} objects
|
||||||
#ifndef MICROPY_PY_SYS_STDFILES
|
#ifndef MICROPY_PY_SYS_STDFILES
|
||||||
#define MICROPY_PY_SYS_STDFILES (0)
|
#define MICROPY_PY_SYS_STDFILES (0)
|
||||||
|
@ -105,6 +105,12 @@ STATIC mp_obj_t dict_unary_op(mp_uint_t op, mp_obj_t self_in) {
|
|||||||
switch (op) {
|
switch (op) {
|
||||||
case MP_UNARY_OP_BOOL: return mp_obj_new_bool(self->map.used != 0);
|
case MP_UNARY_OP_BOOL: return mp_obj_new_bool(self->map.used != 0);
|
||||||
case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->map.used);
|
case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->map.used);
|
||||||
|
#if MICROPY_PY_SYS_GETSIZEOF
|
||||||
|
case MP_UNARY_OP_SIZEOF: {
|
||||||
|
size_t sz = sizeof(*self) + sizeof(*self->map.table) * self->map.alloc;
|
||||||
|
return MP_OBJ_NEW_SMALL_INT(sz);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
default: return MP_OBJ_NULL; // op not supported
|
default: return MP_OBJ_NULL; // op not supported
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -104,6 +104,12 @@ STATIC mp_obj_t list_unary_op(mp_uint_t op, mp_obj_t self_in) {
|
|||||||
switch (op) {
|
switch (op) {
|
||||||
case MP_UNARY_OP_BOOL: return mp_obj_new_bool(self->len != 0);
|
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);
|
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
|
default: return MP_OBJ_NULL; // op not supported
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
16
py/objtype.c
16
py/objtype.c
@ -339,11 +339,27 @@ const qstr mp_unary_op_method_name[] = {
|
|||||||
[MP_UNARY_OP_NEGATIVE] = MP_QSTR___neg__,
|
[MP_UNARY_OP_NEGATIVE] = MP_QSTR___neg__,
|
||||||
[MP_UNARY_OP_INVERT] = MP_QSTR___invert__,
|
[MP_UNARY_OP_INVERT] = MP_QSTR___invert__,
|
||||||
#endif
|
#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
|
[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) {
|
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);
|
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];
|
qstr op_name = mp_unary_op_method_name[op];
|
||||||
/* Still try to lookup native slot
|
/* Still try to lookup native slot
|
||||||
if (op_name == 0) {
|
if (op_name == 0) {
|
||||||
|
@ -50,6 +50,7 @@ typedef enum {
|
|||||||
MP_UNARY_OP_NEGATIVE,
|
MP_UNARY_OP_NEGATIVE,
|
||||||
MP_UNARY_OP_INVERT,
|
MP_UNARY_OP_INVERT,
|
||||||
MP_UNARY_OP_NOT,
|
MP_UNARY_OP_NOT,
|
||||||
|
MP_UNARY_OP_SIZEOF, // for sys.getsizeof()
|
||||||
} mp_unary_op_t;
|
} mp_unary_op_t;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
|
@ -25,7 +25,8 @@ ame__
|
|||||||
__name__ path argv version
|
__name__ path argv version
|
||||||
version_info implementation platform byteorder
|
version_info implementation platform byteorder
|
||||||
maxsize exit stdin stdout
|
maxsize exit stdin stdout
|
||||||
stderr modules exc_info print_exception
|
stderr modules exc_info getsizeof
|
||||||
|
print_exception
|
||||||
ementation
|
ementation
|
||||||
# attrtuple
|
# attrtuple
|
||||||
(start=1, stop=2, step=3)
|
(start=1, stop=2, step=3)
|
||||||
|
@ -37,6 +37,7 @@
|
|||||||
#define MICROPY_PY_DELATTR_SETATTR (1)
|
#define MICROPY_PY_DELATTR_SETATTR (1)
|
||||||
#define MICROPY_PY_BUILTINS_HELP (1)
|
#define MICROPY_PY_BUILTINS_HELP (1)
|
||||||
#define MICROPY_PY_BUILTINS_HELP_MODULES (1)
|
#define MICROPY_PY_BUILTINS_HELP_MODULES (1)
|
||||||
|
#define MICROPY_PY_SYS_GETSIZEOF (1)
|
||||||
#define MICROPY_PY_URANDOM_EXTRA_FUNCS (1)
|
#define MICROPY_PY_URANDOM_EXTRA_FUNCS (1)
|
||||||
#define MICROPY_PY_IO_BUFFEREDWRITER (1)
|
#define MICROPY_PY_IO_BUFFEREDWRITER (1)
|
||||||
#undef MICROPY_VFS_FAT
|
#undef MICROPY_VFS_FAT
|
||||||
|
Loading…
Reference in New Issue
Block a user