Issue #15402: Add a __sizeof__ method to struct.Struct.
Initial patch by Serhiy Storchaka.
This commit is contained in:
parent
7e918cfe28
commit
b14d8c9bcf
@ -556,6 +556,16 @@ class StructTest(unittest.TestCase):
|
|||||||
s = struct.Struct('i')
|
s = struct.Struct('i')
|
||||||
s.__init__('ii')
|
s.__init__('ii')
|
||||||
|
|
||||||
|
def test_sizeof(self):
|
||||||
|
self.assertGreater(sys.getsizeof(struct.Struct('BHILfdspP')),
|
||||||
|
sys.getsizeof(struct.Struct('B')))
|
||||||
|
self.assertGreaterEqual(sys.getsizeof(struct.Struct('123B')),
|
||||||
|
sys.getsizeof(struct.Struct('B')))
|
||||||
|
self.assertGreaterEqual(sys.getsizeof(struct.Struct('B' * 123)),
|
||||||
|
sys.getsizeof(struct.Struct('123B')))
|
||||||
|
self.assertGreaterEqual(sys.getsizeof(struct.Struct('123xB')),
|
||||||
|
sys.getsizeof(struct.Struct('B')))
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
run_unittest(StructTest)
|
run_unittest(StructTest)
|
||||||
|
|
||||||
|
@ -98,6 +98,10 @@ Core and Builtins
|
|||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #15402: An issue in the struct module that caused sys.getsizeof to
|
||||||
|
return incorrect results for struct.Struct instances has been fixed.
|
||||||
|
Initial patch by Serhiy Storchaka.
|
||||||
|
|
||||||
- Issue #15232: when mangle_from is True, email.Generator now correctly mangles
|
- Issue #15232: when mangle_from is True, email.Generator now correctly mangles
|
||||||
lines that start with 'From' that occur in a MIME preamble or epilogue.
|
lines that start with 'From' that occur in a MIME preamble or epilogue.
|
||||||
|
|
||||||
|
@ -1665,6 +1665,22 @@ s_get_size(PyStructObject *self, void *unused)
|
|||||||
return PyLong_FromSsize_t(self->s_size);
|
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 */
|
/* List of functions */
|
||||||
|
|
||||||
static struct PyMethodDef s_methods[] = {
|
static struct PyMethodDef s_methods[] = {
|
||||||
@ -1673,6 +1689,7 @@ static struct PyMethodDef s_methods[] = {
|
|||||||
{"unpack", s_unpack, METH_O, s_unpack__doc__},
|
{"unpack", s_unpack, METH_O, s_unpack__doc__},
|
||||||
{"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,
|
{"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,
|
||||||
s_unpack_from__doc__},
|
s_unpack_from__doc__},
|
||||||
|
{"__sizeof__", (PyCFunction)s_sizeof, METH_NOARGS, s_sizeof__doc__},
|
||||||
{NULL, NULL} /* sentinel */
|
{NULL, NULL} /* sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user