summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2008-06-04 14:18:43 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2008-06-04 14:18:43 (GMT)
commit00709aaa3dc7da374e94a3ede8e05603e8a95066 (patch)
tree50acc73f0d94cd4294b8a6a0f2efdb03cb43f409 /Objects
parent01a7d82432edbf297c32289737a40d7103a03527 (diff)
downloadcpython-00709aaa3dc7da374e94a3ede8e05603e8a95066.zip
cpython-00709aaa3dc7da374e94a3ede8e05603e8a95066.tar.gz
cpython-00709aaa3dc7da374e94a3ede8e05603e8a95066.tar.bz2
Merged revisions 63856-63857,63859-63860 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r63856 | robert.schuppenies | 2008-06-01 18:16:17 +0200 (So, 01 Jun 2008) | 2 lines Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes. ........ r63859 | georg.brandl | 2008-06-01 18:42:16 +0200 (So, 01 Jun 2008) | 2 lines Some style nits. Also clarify in the docstrings what __sizeof__ does. ........ r63860 | georg.brandl | 2008-06-01 19:05:56 +0200 (So, 01 Jun 2008) | 2 lines Fix test_descrtut. ........
Diffstat (limited to 'Objects')
-rw-r--r--Objects/bytesobject.c13
-rw-r--r--Objects/dictobject.c19
-rw-r--r--Objects/listobject.c12
-rw-r--r--Objects/longobject.c13
-rw-r--r--Objects/typeobject.c16
5 files changed, 73 insertions, 0 deletions
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 3f22e7c..ab6207b 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -2784,6 +2784,17 @@ string_fromhex(PyObject *cls, PyObject *args)
return NULL;
}
+PyDoc_STRVAR(sizeof__doc__,
+"S.__sizeof__() -> size of S in memory, in bytes");
+
+static PyObject *
+string_sizeof(PyBytesObject *v)
+{
+ Py_ssize_t res;
+ res = sizeof(PyBytesObject) + Py_SIZE(v) * Py_TYPE(v)->tp_itemsize;
+ return PyLong_FromSsize_t(res);
+}
+
static PyObject *
string_getnewargs(PyBytesObject *v)
@@ -2848,6 +2859,8 @@ string_methods[] = {
translate__doc__},
{"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__},
{"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__},
+ {"__sizeof__", (PyCFunction)string_sizeof, METH_NOARGS,
+ sizeof__doc__},
{NULL, NULL} /* sentinel */
};
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index fc2c8f0..b4f9182 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -1840,11 +1840,28 @@ dict_tp_clear(PyObject *op)
static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
+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 PyLong_FromSsize_t(res);
+}
+
+PyDoc_STRVAR(has_key__doc__,
+"D.has_key(k) -> True if D has a key k, else False");
+
PyDoc_STRVAR(contains__doc__,
"D.__contains__(k) -> True if D has a key k, else False");
PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
+PyDoc_STRVAR(sizeof__doc__,
+"D.__sizeof__() -> size of D in memory, in bytes");
+
PyDoc_STRVAR(get__doc__,
"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
@@ -1890,6 +1907,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__},
{"get", (PyCFunction)dict_get, METH_VARARGS,
get__doc__},
{"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
diff --git a/Objects/listobject.c b/Objects/listobject.c
index c84cce5..d5f3a19 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -2247,6 +2247,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 PyLong_FromSsize_t(res);
+}
+
static PyObject *list_iter(PyObject *seq);
static PyObject *list_reversed(PyListObject* seq, PyObject* unused);
@@ -2254,6 +2263,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 memory, in bytes");
PyDoc_STRVAR(append_doc,
"L.append(object) -- append object to end");
PyDoc_STRVAR(extend_doc,
@@ -2279,6 +2290,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 10d03ae..d0d4d3a 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -3625,6 +3625,17 @@ long_round(PyObject *self, PyObject *args)
#undef UNDEF_NDIGITS
}
+static PyObject *
+long_sizeof(PyLongObject *v)
+{
+ Py_ssize_t res;
+
+ res = sizeof(PyLongObject) + abs(Py_SIZE(v)) * sizeof(digit);
+ if (Py_SIZE(v) != 0)
+ res -= sizeof(digit);
+ return PyLong_FromSsize_t(res);
+}
+
#if 0
static PyObject *
long_is_finite(PyObject *v)
@@ -3651,6 +3662,8 @@ static PyMethodDef long_methods[] = {
"Rounding with an ndigits arguments defers to float.__round__."},
{"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
{"__format__", (PyCFunction)long__format__, METH_VARARGS},
+ {"__sizeof__", (PyCFunction)long_sizeof, METH_NOARGS,
+ "Returns size in memory, in bytes"},
{NULL, NULL} /* sentinel */
};
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 73d501c..6e4ac7a 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -3275,6 +3275,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 = Py_SIZE(self->ob_type) * isize;
+ res += self->ob_type->tp_basicsize;
+
+ return PyLong_FromSsize_t(res);
+}
+
static PyMethodDef object_methods[] = {
{"__reduce_ex__", object_reduce_ex, METH_VARARGS,
PyDoc_STR("helper for pickle")},
@@ -3284,6 +3298,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 memory, in bytes")},
{0}
};