]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Closes #15469: Correct __sizeof__ support for deque
authorJesus Cea <jcea@jcea.es>
Fri, 3 Aug 2012 12:48:23 +0000 (14:48 +0200)
committerJesus Cea <jcea@jcea.es>
Fri, 3 Aug 2012 12:48:23 +0000 (14:48 +0200)
Lib/test/test_deque.py
Misc/NEWS
Modules/_collectionsmodule.c

index a0d30f182810b7ec930427ee43482af519564b1e..595a0c4a35cace415f66468eb046664d63c2ec63 100644 (file)
@@ -6,6 +6,7 @@ import weakref
 import copy
 import cPickle as pickle
 import random
+import struct
 
 BIG = 100000
 
@@ -517,6 +518,21 @@ class TestBasic(unittest.TestCase):
             gc.collect()
             self.assertTrue(ref() is None, "Cycle was not collected")
 
+    check_sizeof = test_support.check_sizeof
+
+    @test_support.cpython_only
+    def test_sizeof(self):
+        BLOCKLEN = 62
+        basesize = test_support.calcobjsize('2P4PlP')
+        blocksize = struct.calcsize('2P%dP' % BLOCKLEN)
+        self.assertEqual(object.__sizeof__(deque()), basesize)
+        check = self.check_sizeof
+        check(deque(), basesize + blocksize)
+        check(deque('a'), basesize + blocksize)
+        check(deque('a' * (BLOCKLEN // 2)), basesize + blocksize)
+        check(deque('a' * (BLOCKLEN // 2 + 1)), basesize + 2 * blocksize)
+        check(deque('a' * (42 * BLOCKLEN)), basesize + 43 * blocksize)
+
 class TestVariousIteratorArgs(unittest.TestCase):
 
     def test_constructor(self):
index b6e4791eebe69a5979338a7a16333713a65d1bc9..8cf3226b9eda9bf54aaa4ffb014d58d611dadd37 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -104,6 +104,9 @@ Library
 - Issue #15489: Add a __sizeof__ implementation for BytesIO objects.
   Patch by Serhiy Storchaka.
 
+- Issue #15469: Add a __sizeof__ implementation for deque objects.
+  Patch by Serhiy Storchaka.
+
 - Issue #15487: Add a __sizeof__ implementation for buffered I/O objects.
   Patch by Serhiy Storchaka.
 
index ccc3043cae01d1becec696b927cfb17bd6255e28..41ce61f1ebd7ba621075637d2e98dfb15334f834 100644 (file)
@@ -990,6 +990,23 @@ deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs)
     return 0;
 }
 
+static PyObject *
+deque_sizeof(dequeobject *deque, void *unused)
+{
+    Py_ssize_t res;
+    Py_ssize_t blocks;
+
+    res = sizeof(dequeobject);
+    blocks = (deque->leftindex + deque->len + BLOCKLEN - 1) / BLOCKLEN;
+    assert(deque->leftindex + deque->len - 1 ==
+           (blocks - 1) * BLOCKLEN + deque->rightindex);
+    res += blocks * sizeof(block);
+    return PyLong_FromSsize_t(res);
+}
+
+PyDoc_STRVAR(sizeof_doc,
+"D.__sizeof__() -- size of D in memory, in bytes");
+
 static PyObject *
 deque_get_maxlen(dequeobject *deque)
 {
@@ -1053,7 +1070,9 @@ static PyMethodDef deque_methods[] = {
     {"reverse",                 (PyCFunction)deque_reverse,
         METH_NOARGS,             reverse_doc},
     {"rotate",                  (PyCFunction)deque_rotate,
-        METH_VARARGS,           rotate_doc},
+        METH_VARARGS,            rotate_doc},
+    {"__sizeof__",              (PyCFunction)deque_sizeof,
+        METH_NOARGS,             sizeof_doc},
     {NULL,              NULL}   /* sentinel */
 };