diff -r 1d811e1097ed Lib/test/test_memoryio.py --- a/Lib/test/test_memoryio.py Sun Jul 29 19:04:57 2012 +0200 +++ b/Lib/test/test_memoryio.py Sun Jul 29 22:34:41 2012 +0300 @@ -701,6 +701,19 @@ memio.close() self.assertRaises(ValueError, memio.__setstate__, ("closed", "", 0, None)) + check_sizeof = support.check_sizeof + + @support.cpython_only + def test_sizeof(self): + basesize = support.calcobjsize('P2nNi 2P 4c5P') + check = self.check_sizeof + self.assertEqual(object.__sizeof__(io.StringIO()), basesize) + check(io.StringIO(), basesize + 2 * 4 ) + check(io.StringIO('a' * 1000), basesize + (1000 + 2) * 4) + check(io.StringIO('\x80' * 1000), basesize + (1000 + 2) * 4) + check(io.StringIO('\u0100' * 1000), basesize + (1000 + 2) * 4) + check(io.StringIO('\U00010000' * 1000), basesize + (1000 + 2) * 4) + class CStringIOPickleTest(PyStringIOPickleTest): UnsupportedOperation = io.UnsupportedOperation diff -r 1d811e1097ed Modules/_io/stringio.c --- a/Modules/_io/stringio.c Sun Jul 29 19:04:57 2012 +0200 +++ b/Modules/_io/stringio.c Sun Jul 29 22:34:41 2012 +0300 @@ -759,6 +759,17 @@ return 0; } +static PyObject * +stringio_sizeof(stringio *self, void *unused) +{ + Py_ssize_t res; + + res = sizeof(stringio); + if (self->buf) + res += self->buf_size * sizeof(Py_UCS4); + return PyLong_FromSsize_t(res); +} + /* Properties and pseudo-properties */ static PyObject * stringio_seekable(stringio *self, PyObject *args) @@ -962,6 +973,7 @@ {"__getstate__", (PyCFunction)stringio_getstate, METH_NOARGS}, {"__setstate__", (PyCFunction)stringio_setstate, METH_O}, + {"__sizeof__", (PyCFunction)stringio_sizeof, METH_NOARGS}, {NULL, NULL} /* sentinel */ };