mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
This commit is contained in:
parent
6495c8da8f
commit
51df064767
9 changed files with 271 additions and 2 deletions
|
@ -3917,6 +3917,17 @@ string_splitlines(PyBytesObject *self, PyObject *args)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(sizeof__doc__,
|
||||
"S.__sizeof__() -> size of S in bytes");
|
||||
|
||||
static PyObject *
|
||||
string_sizeof(PyBytesObject *v)
|
||||
{
|
||||
Py_ssize_t res;
|
||||
res = sizeof(PyBytesObject) + v->ob_size * v->ob_type->tp_itemsize;
|
||||
return PyInt_FromSsize_t(res);
|
||||
}
|
||||
|
||||
#undef SPLIT_APPEND
|
||||
#undef SPLIT_ADD
|
||||
#undef MAX_PREALLOC
|
||||
|
@ -4024,6 +4035,8 @@ string_methods[] = {
|
|||
expandtabs__doc__},
|
||||
{"splitlines", (PyCFunction)string_splitlines, METH_VARARGS,
|
||||
splitlines__doc__},
|
||||
{"__sizeof__", (PyCFunction)string_sizeof, METH_NOARGS,
|
||||
sizeof__doc__},
|
||||
{"__getnewargs__", (PyCFunction)string_getnewargs, METH_NOARGS},
|
||||
{NULL, NULL} /* sentinel */
|
||||
};
|
||||
|
|
|
@ -2032,6 +2032,16 @@ dict_iteritems(PyDictObject *dict)
|
|||
return dictiter_new(dict, &PyDictIterItem_Type);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
dict_sizeof(PyDictObject *mp)
|
||||
{
|
||||
Py_ssize_t res;
|
||||
|
||||
res = sizeof(PyDictObject) + sizeof(mp->ma_table);
|
||||
if (mp->ma_table != mp->ma_smalltable)
|
||||
res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry);
|
||||
return PyInt_FromSsize_t(res);
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(has_key__doc__,
|
||||
"D.has_key(k) -> True if D has a key k, else False");
|
||||
|
@ -2041,6 +2051,9 @@ PyDoc_STRVAR(contains__doc__,
|
|||
|
||||
PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
|
||||
|
||||
PyDoc_STRVAR(sizeof__doc__,
|
||||
"D.__sizeof__() -> size of D in bytes");
|
||||
|
||||
PyDoc_STRVAR(get__doc__,
|
||||
"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
|
||||
|
||||
|
@ -2092,6 +2105,8 @@ static PyMethodDef mapp_methods[] = {
|
|||
contains__doc__},
|
||||
{"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
|
||||
getitem__doc__},
|
||||
{"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
|
||||
sizeof__doc__},
|
||||
{"has_key", (PyCFunction)dict_has_key, METH_O,
|
||||
has_key__doc__},
|
||||
{"get", (PyCFunction)dict_get, METH_VARARGS,
|
||||
|
|
|
@ -2420,6 +2420,15 @@ list_init(PyListObject *self, PyObject *args, PyObject *kw)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
list_sizeof(PyListObject *self)
|
||||
{
|
||||
Py_ssize_t res;
|
||||
|
||||
res = sizeof(PyListObject) + self->allocated * sizeof(void*);
|
||||
return PyInt_FromSsize_t(res);
|
||||
}
|
||||
|
||||
static PyObject *list_iter(PyObject *seq);
|
||||
static PyObject *list_reversed(PyListObject* seq, PyObject* unused);
|
||||
|
||||
|
@ -2427,6 +2436,8 @@ PyDoc_STRVAR(getitem_doc,
|
|||
"x.__getitem__(y) <==> x[y]");
|
||||
PyDoc_STRVAR(reversed_doc,
|
||||
"L.__reversed__() -- return a reverse iterator over the list");
|
||||
PyDoc_STRVAR(sizeof_doc,
|
||||
"L.__sizeof__() -- size of L in bytes");
|
||||
PyDoc_STRVAR(append_doc,
|
||||
"L.append(object) -- append object to end");
|
||||
PyDoc_STRVAR(extend_doc,
|
||||
|
@ -2452,6 +2463,7 @@ static PyObject *list_subscript(PyListObject*, PyObject*);
|
|||
static PyMethodDef list_methods[] = {
|
||||
{"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
|
||||
{"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
|
||||
{"__sizeof__", (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc},
|
||||
{"append", (PyCFunction)listappend, METH_O, append_doc},
|
||||
{"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
|
||||
{"extend", (PyCFunction)listextend, METH_O, extend_doc},
|
||||
|
|
|
@ -3436,6 +3436,17 @@ long__format__(PyObject *self, PyObject *args)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
long_sizeof(PyLongObject *v)
|
||||
{
|
||||
Py_ssize_t res;
|
||||
|
||||
res = sizeof(PyLongObject) + abs(v->ob_size) * sizeof(digit);
|
||||
if (v->ob_size != 0)
|
||||
res -= sizeof(digit);
|
||||
return PyInt_FromSsize_t(res);
|
||||
}
|
||||
|
||||
#if 0
|
||||
static PyObject *
|
||||
long_is_finite(PyObject *v)
|
||||
|
@ -3455,6 +3466,8 @@ static PyMethodDef long_methods[] = {
|
|||
"Truncating an Integral returns itself."},
|
||||
{"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
|
||||
{"__format__", (PyCFunction)long__format__, METH_VARARGS},
|
||||
{"__sizeof__", (PyCFunction)long_sizeof, METH_NOARGS,
|
||||
"Returns size in bytes"},
|
||||
{NULL, NULL} /* sentinel */
|
||||
};
|
||||
|
||||
|
|
|
@ -3397,6 +3397,20 @@ object_format(PyObject *self, PyObject *args)
|
|||
return result;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
object_sizeof(PyObject *self, PyObject *args)
|
||||
{
|
||||
Py_ssize_t res, isize;
|
||||
|
||||
res = 0;
|
||||
isize = self->ob_type->tp_itemsize;
|
||||
if (isize > 0)
|
||||
res = self->ob_type->ob_size * isize;
|
||||
res += self->ob_type->tp_basicsize;
|
||||
|
||||
return PyInt_FromSsize_t(res);
|
||||
}
|
||||
|
||||
static PyMethodDef object_methods[] = {
|
||||
{"__reduce_ex__", object_reduce_ex, METH_VARARGS,
|
||||
PyDoc_STR("helper for pickle")},
|
||||
|
@ -3406,6 +3420,8 @@ static PyMethodDef object_methods[] = {
|
|||
object_subclasshook_doc},
|
||||
{"__format__", object_format, METH_VARARGS,
|
||||
PyDoc_STR("default object formatter")},
|
||||
{"__sizeof__", object_sizeof, METH_NOARGS,
|
||||
PyDoc_STR("__sizeof__() -> size of object in bytes")},
|
||||
{0}
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue