mirror of
https://github.com/python/cpython.git
synced 2025-09-14 20:56:06 +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
|
@ -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