summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorRobert Schuppenies <okkotonushi@googlemail.com>2008-06-01 16:16:17 (GMT)
committerRobert Schuppenies <okkotonushi@googlemail.com>2008-06-01 16:16:17 (GMT)
commit51df0647672bc758da6d58eecfe45da9dc5913df (patch)
tree3cb568af01a3310eda4e1647901cd5ac3df118fb /Objects
parent6495c8da8f945def4749192b0536c1678f208664 (diff)
downloadcpython-51df0647672bc758da6d58eecfe45da9dc5913df.zip
cpython-51df0647672bc758da6d58eecfe45da9dc5913df.tar.gz
cpython-51df0647672bc758da6d58eecfe45da9dc5913df.tar.bz2
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/bytesobject.c13
-rw-r--r--Objects/dictobject.c15
-rw-r--r--Objects/listobject.c12
-rw-r--r--Objects/longobject.c13
-rw-r--r--Objects/typeobject.c16
5 files changed, 69 insertions, 0 deletions
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 79c1e4f..0de24f8 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -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 */
};
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index cdf0dfa..d3a7cb9 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -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,
diff --git a/Objects/listobject.c b/Objects/listobject.c
index 9d742d8..0216a85 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -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},
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 82a57ec..5876495 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -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 */
};
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 151ea69..5405fec 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -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}
};