diff -r fa95b04e67fd Lib/test/test_memoryio.py --- a/Lib/test/test_memoryio.py Sun Jul 29 16:30:50 2012 +0200 +++ b/Lib/test/test_memoryio.py Sun Jul 29 22:28:48 2012 +0300 @@ -638,6 +638,17 @@ 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(b'P2PP2P') + 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 diff -r fa95b04e67fd Modules/_io/bytesio.c --- a/Modules/_io/bytesio.c Sun Jul 29 16:30:50 2012 +0200 +++ b/Modules/_io/bytesio.c Sun Jul 29 22:28:48 2012 +0300 @@ -794,6 +794,17 @@ 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) { @@ -835,6 +846,7 @@ {"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 */ };