Mercurial > cpython-lru_cache-weakref
diff Modules/_struct.c @ 78255:dbe7f39ff341 2.7
Issue #15402: Add a __sizeof__ method to struct.Struct.
Initial patch by Serhiy Storchaka.
| author | Meador Inge <meadori@gmail.com> |
|---|---|
| date | Mon, 23 Jul 2012 09:27:00 -0500 |
| parents | ee5350f1d0c2 |
| children | 37554bda2014 |
line wrap: on
line diff
--- a/Modules/_struct.c Sun Jul 22 19:36:03 2012 -0700 +++ b/Modules/_struct.c Mon Jul 23 09:27:00 2012 -0500 @@ -1693,6 +1693,22 @@ 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 @@ {"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 */ };
