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:44:19 2012 +0300 @@ -686,6 +686,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(b'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 fa95b04e67fd Modules/_io/stringio.c --- a/Modules/_io/stringio.c Sun Jul 29 16:30:50 2012 +0200 +++ b/Modules/_io/stringio.c Sun Jul 29 22:44:19 2012 +0300 @@ -631,6 +631,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) @@ -823,6 +834,7 @@ {"__getstate__", (PyCFunction)stringio_getstate, METH_NOARGS}, {"__setstate__", (PyCFunction)stringio_setstate, METH_O}, + {"__sizeof__", (PyCFunction)stringio_sizeof, METH_NOARGS}, {NULL, NULL} /* sentinel */ };