summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorMeador Inge <meadori@gmail.com>2012-07-23 14:27:00 (GMT)
committerMeador Inge <meadori@gmail.com>2012-07-23 14:27:00 (GMT)
commit87c5b94c3980aa6f234b962acf047a3a5524986a (patch)
tree79f5df6c79db8bdd473059fd73fa0f8fde2781e3 /Modules
parent0c472c3271b20a1215cecd7ef8326a52de5e9541 (diff)
downloadcpython-87c5b94c3980aa6f234b962acf047a3a5524986a.zip
cpython-87c5b94c3980aa6f234b962acf047a3a5524986a.tar.gz
cpython-87c5b94c3980aa6f234b962acf047a3a5524986a.tar.bz2
Issue #15402: Add a __sizeof__ method to struct.Struct.
Initial patch by Serhiy Storchaka.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_struct.c17
1 files changed, 17 insertions, 0 deletions
diff --git a/Modules/_struct.c b/Modules/_struct.c
index c158eba..3ede1d0 100644
--- a/Modules/_struct.c
+++ b/Modules/_struct.c
@@ -1693,6 +1693,22 @@ s_get_size(PyStructObject *self, void *unused)
return PyInt_FromSsize_t(self->s_size);
}
+PyDoc_STRVAR(s_sizeof__doc__,
+"S.__sizeof__() -> size of S in memory, in bytes");
+
+static PyObject *
+s_sizeof(PyStructObject *self)
+{
+ Py_ssize_t size;
+ formatcode *code;
+
+ size = sizeof(PyStructObject) + sizeof(formatcode);
+ for (code = self->s_codes; code->fmtdef != NULL; code++) {
+ size += sizeof(formatcode);
+ }
+ return PyLong_FromSsize_t(size);
+}
+
/* List of functions */
static struct PyMethodDef s_methods[] = {
@@ -1701,6 +1717,7 @@ static struct PyMethodDef s_methods[] = {
{"unpack", s_unpack, METH_O, s_unpack__doc__},
{"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,
s_unpack_from__doc__},
+ {"__sizeof__", (PyCFunction)s_sizeof, METH_NOARGS, s_sizeof__doc__},
{NULL, NULL} /* sentinel */
};