summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorRobert Schuppenies <okkotonushi@googlemail.com>2008-07-10 15:24:04 (GMT)
committerRobert Schuppenies <okkotonushi@googlemail.com>2008-07-10 15:24:04 (GMT)
commit9be2ec109bcec499c1a6971fb4c40d9a8e7886fe (patch)
tree04942da45d0452f8555758be18bfa3effda1dedb /Objects
parentcc3f2b1d1677b4464f223c2fbff77f1b8bd6df0a (diff)
downloadcpython-9be2ec109bcec499c1a6971fb4c40d9a8e7886fe.zip
cpython-9be2ec109bcec499c1a6971fb4c40d9a8e7886fe.tar.gz
cpython-9be2ec109bcec499c1a6971fb4c40d9a8e7886fe.tar.bz2
Added additional __sizeof__ implementations and addressed comments made in
Issue3122.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/bytearrayobject.c14
-rw-r--r--Objects/frameobject.c25
-rw-r--r--Objects/longobject.c4
-rw-r--r--Objects/setobject.c16
-rw-r--r--Objects/unicodeobject.c16
5 files changed, 58 insertions, 17 deletions
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index b2ddf6b..91a1ed9 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -3106,6 +3106,19 @@ bytes_reduce(PyByteArrayObject *self)
return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
}
+PyDoc_STRVAR(sizeof_doc,
+"B.__sizeof__() -> int\n\
+ \n\
+Returns the size of B in memory, in bytes");
+static PyObject *
+bytes_sizeof(PyByteArrayObject *self)
+{
+ Py_ssize_t res;
+
+ res = sizeof(PyByteArrayObject) + self->ob_alloc * sizeof(char);
+ return PyInt_FromSsize_t(res);
+}
+
static PySequenceMethods bytes_as_sequence = {
(lenfunc)bytes_length, /* sq_length */
(binaryfunc)PyByteArray_Concat, /* sq_concat */
@@ -3138,6 +3151,7 @@ static PyMethodDef
bytes_methods[] = {
{"__alloc__", (PyCFunction)bytes_alloc, METH_NOARGS, alloc_doc},
{"__reduce__", (PyCFunction)bytes_reduce, METH_NOARGS, reduce_doc},
+ {"__sizeof__", (PyCFunction)bytes_sizeof, METH_NOARGS, sizeof_doc},
{"append", (PyCFunction)bytes_append, METH_O, append__doc__},
{"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS,
_Py_capitalize__doc__},
diff --git a/Objects/frameobject.c b/Objects/frameobject.c
index 025431e..d89d72d 100644
--- a/Objects/frameobject.c
+++ b/Objects/frameobject.c
@@ -508,6 +508,29 @@ frame_clear(PyFrameObject *f)
}
}
+static PyObject *
+frame_sizeof(PyFrameObject *f)
+{
+ Py_ssize_t res, extras, ncells, nfrees;
+
+ ncells = PyTuple_GET_SIZE(f->f_code->co_cellvars);
+ nfrees = PyTuple_GET_SIZE(f->f_code->co_freevars);
+ extras = f->f_code->co_stacksize + f->f_code->co_nlocals +
+ ncells + nfrees;
+ // subtract one as it is already included in PyFrameObject
+ res = sizeof(PyFrameObject) + (extras-1) * sizeof(PyObject *);
+
+ return PyInt_FromSsize_t(res);
+}
+
+PyDoc_STRVAR(sizeof__doc__,
+"F.__sizeof__() -> size of F in memory, in bytes");
+
+static PyMethodDef frame_methods[] = {
+ {"__sizeof__", (PyCFunction)frame_sizeof, METH_NOARGS,
+ sizeof__doc__},
+ {NULL, NULL} /* sentinel */
+};
PyTypeObject PyFrame_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
@@ -537,7 +560,7 @@ PyTypeObject PyFrame_Type = {
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
- 0, /* tp_methods */
+ frame_methods, /* tp_methods */
frame_memberlist, /* tp_members */
frame_getsetlist, /* tp_getset */
0, /* tp_base */
diff --git a/Objects/longobject.c b/Objects/longobject.c
index c65d0c0..2c228bb 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -3441,9 +3441,9 @@ long_sizeof(PyLongObject *v)
{
Py_ssize_t res;
- res = sizeof(PyLongObject) + abs(v->ob_size) * sizeof(digit);
+ res = v->ob_type->tp_basicsize;
if (v->ob_size != 0)
- res -= sizeof(digit);
+ res += abs(v->ob_size) * sizeof(digit);
return PyInt_FromSsize_t(res);
}
diff --git a/Objects/setobject.c b/Objects/setobject.c
index f3eea21..39465f3 100644
--- a/Objects/setobject.c
+++ b/Objects/setobject.c
@@ -1956,6 +1956,18 @@ done:
PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
+static PyObject *
+set_sizeof(PySetObject *so)
+{
+ Py_ssize_t res;
+
+ res = sizeof(PySetObject);
+ if (so->table != so->smalltable)
+ res = res + (so->mask + 1) * sizeof(setentry);
+ return PyInt_FromSsize_t(res);
+}
+
+PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
static int
set_init(PySetObject *self, PyObject *args, PyObject *kwds)
{
@@ -2023,6 +2035,8 @@ static PyMethodDef set_methods[] = {
reduce_doc},
{"remove", (PyCFunction)set_remove, METH_O,
remove_doc},
+ {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
+ sizeof_doc},
{"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
symmetric_difference_doc},
{"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
@@ -2144,6 +2158,8 @@ static PyMethodDef frozenset_methods[] = {
issuperset_doc},
{"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
reduce_doc},
+ {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
+ sizeof_doc},
{"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
symmetric_difference_doc},
{"union", (PyCFunction)set_union, METH_VARARGS,
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 840efb9..a62e929 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -7898,20 +7898,8 @@ PyDoc_STRVAR(p_format__doc__,
static PyObject *
unicode__sizeof__(PyUnicodeObject *v)
{
- PyObject *res = NULL, *defsize = NULL;
-
- res = PyInt_FromSsize_t(sizeof(PyUnicodeObject) +
- sizeof(Py_UNICODE) * (v->length + 1));
- if (v->defenc) {
- defsize = PyObject_CallMethod(v->defenc, "__sizeof__", NULL);
- if (defsize == NULL) {
- Py_DECREF(res);
- return NULL;
- }
- res = PyNumber_Add(res, defsize);
- Py_DECREF(defsize);
- }
- return res;
+ return PyInt_FromSsize_t(sizeof(PyUnicodeObject) +
+ sizeof(Py_UNICODE) * (v->length + 1));
}
PyDoc_STRVAR(sizeof__doc__,