diff -r efade142ef01 Lib/test/test_memoryio.py --- a/Lib/test/test_memoryio.py Sun Jul 29 16:33:05 2012 +0200 +++ b/Lib/test/test_memoryio.py Sun Jul 29 22:42:32 2012 +0300 @@ -701,6 +701,21 @@ 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('P2PP4c5P') + check = self.check_sizeof + self.assertEqual(object.__sizeof__(io.StringIO()), basesize) + usize = len('\0'.encode('unicode-internal')) + wusize = len('\U00010000'.encode('unicode-internal')) + check(io.StringIO(), basesize + 2 * usize ) + check(io.StringIO('a' * 1000), basesize + (1000 + 2) * usize) + check(io.StringIO('\x80' * 1000), basesize + (1000 + 2) * usize) + check(io.StringIO('\u0100' * 1000), basesize + (1000 + 2) * usize) + check(io.StringIO('\U00010000' * 1000), basesize + 1000 * wusize + 2 * usize) + class CStringIOPickleTest(PyStringIOPickleTest): UnsupportedOperation = io.UnsupportedOperation diff -r efade142ef01 Modules/_io/stringio.c --- a/Modules/_io/stringio.c Sun Jul 29 16:33:05 2012 +0200 +++ b/Modules/_io/stringio.c Sun Jul 29 22:42:32 2012 +0300 @@ -649,6 +649,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_UNICODE); + return PyLong_FromSsize_t(res); +} + /* Properties and pseudo-properties */ static PyObject * stringio_seekable(stringio *self, PyObject *args) @@ -841,6 +852,7 @@ {"__getstate__", (PyCFunction)stringio_getstate, METH_NOARGS}, {"__setstate__", (PyCFunction)stringio_setstate, METH_O}, + {"__sizeof__", (PyCFunction)stringio_sizeof, METH_NOARGS}, {NULL, NULL} /* sentinel */ };