Issue #15489: Add a __sizeof__ implementation for BytesIO objects.

Patch by Serhiy Storchaka.
This commit is contained in:
Antoine Pitrou 2012-07-30 00:01:06 +02:00
parent 10f0c50a0b
commit 8f328d0c1d
3 changed files with 26 additions and 0 deletions

View File

@ -654,6 +654,17 @@ class CBytesIOTest(PyBytesIOTest):
memio.close()
self.assertRaises(ValueError, memio.__setstate__, (b"closed", 0, None))
check_sizeof = support.check_sizeof
@support.cpython_only
def test_sizeof(self):
basesize = support.calcobjsize('P2PP2PP')
check = self.check_sizeof
self.assertEqual(object.__sizeof__(io.BytesIO()), basesize)
check(io.BytesIO(), basesize )
check(io.BytesIO(b'a'), basesize + 1 + 1 )
check(io.BytesIO(b'a' * 1000), basesize + 1000 + 1 )
class CStringIOTest(PyStringIOTest):
ioclass = io.StringIO

View File

@ -98,6 +98,9 @@ Core and Builtins
Library
-------
- Issue #15489: Add a __sizeof__ implementation for BytesIO objects.
Patch by Serhiy Storchaka.
- Issue #15487: Add a __sizeof__ implementation for buffered I/O objects.
Patch by Serhiy Storchaka.

View File

@ -834,6 +834,17 @@ bytesio_init(bytesio *self, PyObject *args, PyObject *kwds)
return 0;
}
static PyObject *
bytesio_sizeof(bytesio *self, void *unused)
{
Py_ssize_t res;
res = sizeof(bytesio);
if (self->buf)
res += self->buf_size;
return PyLong_FromSsize_t(res);
}
static int
bytesio_traverse(bytesio *self, visitproc visit, void *arg)
{
@ -876,6 +887,7 @@ static struct PyMethodDef bytesio_methods[] = {
{"truncate", (PyCFunction)bytesio_truncate, METH_VARARGS, truncate_doc},
{"__getstate__", (PyCFunction)bytesio_getstate, METH_NOARGS, NULL},
{"__setstate__", (PyCFunction)bytesio_setstate, METH_O, NULL},
{"__sizeof__", (PyCFunction)bytesio_sizeof, METH_NOARGS, NULL},
{NULL, NULL} /* sentinel */
};