diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2012-07-29 17:04:57 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2012-07-29 17:04:57 (GMT) |
commit | a264384fe6de357680ca0cf02cd6024bbba0ba45 (patch) | |
tree | ea2c7c77a9c36cda0843e1628a4e0262d13c4c4f | |
parent | 2b16844326f9a88f7c5daf16abfb04cee70f02bc (diff) | |
parent | 10f0c50a0bc9237d640cb7beb216d63f9ec2b3eb (diff) | |
download | cpython-a264384fe6de357680ca0cf02cd6024bbba0ba45.zip cpython-a264384fe6de357680ca0cf02cd6024bbba0ba45.tar.gz cpython-a264384fe6de357680ca0cf02cd6024bbba0ba45.tar.bz2 |
Issue #15487: Add a __sizeof__ implementation for buffered I/O objects.
Patch by Serhiy Storchaka.
-rw-r--r-- | Lib/test/test_io.py | 20 | ||||
-rw-r--r-- | Misc/NEWS | 3 | ||||
-rw-r--r-- | Modules/_io/bufferedio.c | 14 |
3 files changed, 34 insertions, 3 deletions
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index ed8564c..5735350 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -802,6 +802,20 @@ class CommonBufferedTests: buf.raw = x +class SizeofTest: + + @support.cpython_only + def test_sizeof(self): + bufsize1 = 4096 + bufsize2 = 8192 + rawio = self.MockRawIO() + bufio = self.tp(rawio, buffer_size=bufsize1) + size = sys.getsizeof(bufio) - bufsize1 + rawio = self.MockRawIO() + bufio = self.tp(rawio, buffer_size=bufsize2) + self.assertEqual(sys.getsizeof(bufio), size + bufsize2) + + class BufferedReaderTest(unittest.TestCase, CommonBufferedTests): read_mode = "rb" @@ -999,7 +1013,7 @@ class BufferedReaderTest(unittest.TestCase, CommonBufferedTests): "failed for {}: {} != 0".format(n, rawio._extraneous_reads)) -class CBufferedReaderTest(BufferedReaderTest): +class CBufferedReaderTest(BufferedReaderTest, SizeofTest): tp = io.BufferedReader def test_constructor(self): @@ -1260,7 +1274,7 @@ class BufferedWriterTest(unittest.TestCase, CommonBufferedTests): self.tp(self.MockRawIO(), 8, 12) -class CBufferedWriterTest(BufferedWriterTest): +class CBufferedWriterTest(BufferedWriterTest, SizeofTest): tp = io.BufferedWriter def test_constructor(self): @@ -1650,7 +1664,7 @@ class BufferedRandomTest(BufferedReaderTest, BufferedWriterTest): # You can't construct a BufferedRandom over a non-seekable stream. test_unseekable = None -class CBufferedRandomTest(BufferedRandomTest): +class CBufferedRandomTest(BufferedRandomTest, SizeofTest): tp = io.BufferedRandom def test_constructor(self): @@ -334,6 +334,9 @@ Core and Builtins Library ------- +- Issue #15487: Add a __sizeof__ implementation for buffered I/O objects. + Patch by Serhiy Storchaka. + - Issue #15187: Bugfix: remove temporary directories test_shutil was leaving behind. diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c index f717817..67b7cc4 100644 --- a/Modules/_io/bufferedio.c +++ b/Modules/_io/bufferedio.c @@ -398,6 +398,17 @@ buffered_dealloc(buffered *self) Py_TYPE(self)->tp_free((PyObject *)self); } +static PyObject * +buffered_sizeof(buffered *self, void *unused) +{ + Py_ssize_t res; + + res = sizeof(buffered); + if (self->buffer) + res += self->buffer_size; + return PyLong_FromSsize_t(res); +} + static int buffered_traverse(buffered *self, visitproc visit, void *arg) { @@ -1699,6 +1710,7 @@ static PyMethodDef bufferedreader_methods[] = { {"seek", (PyCFunction)buffered_seek, METH_VARARGS}, {"tell", (PyCFunction)buffered_tell, METH_NOARGS}, {"truncate", (PyCFunction)buffered_truncate, METH_VARARGS}, + {"__sizeof__", (PyCFunction)buffered_sizeof, METH_NOARGS}, {NULL, NULL} }; @@ -2079,6 +2091,7 @@ static PyMethodDef bufferedwriter_methods[] = { {"flush", (PyCFunction)buffered_flush, METH_NOARGS}, {"seek", (PyCFunction)buffered_seek, METH_VARARGS}, {"tell", (PyCFunction)buffered_tell, METH_NOARGS}, + {"__sizeof__", (PyCFunction)buffered_sizeof, METH_NOARGS}, {NULL, NULL} }; @@ -2470,6 +2483,7 @@ static PyMethodDef bufferedrandom_methods[] = { {"readline", (PyCFunction)buffered_readline, METH_VARARGS}, {"peek", (PyCFunction)buffered_peek, METH_VARARGS}, {"write", (PyCFunction)bufferedwriter_write, METH_VARARGS}, + {"__sizeof__", (PyCFunction)buffered_sizeof, METH_NOARGS}, {NULL, NULL} }; |