diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2012-07-29 22:01:44 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2012-07-29 22:01:44 (GMT) |
commit | 57911f6c1a84dd0eb050908f81d6e60d38054271 (patch) | |
tree | 539f04bf5b1110f16170097d74467cfd188aa3bd | |
parent | a264384fe6de357680ca0cf02cd6024bbba0ba45 (diff) | |
parent | 8f328d0c1d86096b3d4e5f1c1ac497663e197a0d (diff) | |
download | cpython-57911f6c1a84dd0eb050908f81d6e60d38054271.zip cpython-57911f6c1a84dd0eb050908f81d6e60d38054271.tar.gz cpython-57911f6c1a84dd0eb050908f81d6e60d38054271.tar.bz2 |
Issue #15489: Add a __sizeof__ implementation for BytesIO objects.
Patch by Serhiy Storchaka.
-rw-r--r-- | Lib/test/test_memoryio.py | 11 | ||||
-rw-r--r-- | Misc/NEWS | 3 | ||||
-rw-r--r-- | Modules/_io/bytesio.c | 12 |
3 files changed, 26 insertions, 0 deletions
diff --git a/Lib/test/test_memoryio.py b/Lib/test/test_memoryio.py index 49ca44f..9248098 100644 --- a/Lib/test/test_memoryio.py +++ b/Lib/test/test_memoryio.py @@ -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 @@ -334,6 +334,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. diff --git a/Modules/_io/bytesio.c b/Modules/_io/bytesio.c index 65ec931..3d027e2 100644 --- a/Modules/_io/bytesio.c +++ b/Modules/_io/bytesio.c @@ -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 */ }; |