Issue #15402: Add a __sizeof__ method to struct.Struct.

Initial patch by Serhiy Storchaka.
This commit is contained in:
Meador Inge 2012-07-23 10:22:36 -05:00
commit 9f65899d19
3 changed files with 31 additions and 0 deletions

View file

@ -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 */
};