diff options
author | Meador Inge <meadori@gmail.com> | 2012-07-23 15:22:36 (GMT) |
---|---|---|
committer | Meador Inge <meadori@gmail.com> | 2012-07-23 15:22:36 (GMT) |
commit | 9f65899d193b3056a79f2a7ea8dc32820d1127cf (patch) | |
tree | 7aa12519f8d4ec1a5f2106256861020a28e89266 /Modules/_struct.c | |
parent | fe114f0293bde5e213211a6658644144f7df94b9 (diff) | |
parent | b14d8c9bcfb7f58e83a5c18c8b6eac0fa261c4b6 (diff) | |
download | cpython-9f65899d193b3056a79f2a7ea8dc32820d1127cf.zip cpython-9f65899d193b3056a79f2a7ea8dc32820d1127cf.tar.gz cpython-9f65899d193b3056a79f2a7ea8dc32820d1127cf.tar.bz2 |
Issue #15402: Add a __sizeof__ method to struct.Struct.
Initial patch by Serhiy Storchaka.
Diffstat (limited to 'Modules/_struct.c')
-rw-r--r-- | Modules/_struct.c | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/Modules/_struct.c b/Modules/_struct.c index dcdc83e..04692f0 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -1752,6 +1752,22 @@ s_get_size(PyStructObject *self, void *unused) return PyLong_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[] = { @@ -1760,6 +1776,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 */ }; |