]> granicus.if.org Git - python/commitdiff
Closes #15469: Correct __sizeof__ support for deque
authorJesus Cea <jcea@jcea.es>
Fri, 3 Aug 2012 12:49:42 +0000 (14:49 +0200)
committerJesus Cea <jcea@jcea.es>
Fri, 3 Aug 2012 12:49:42 +0000 (14:49 +0200)
Lib/test/test_deque.py
Misc/NEWS
Modules/_collectionsmodule.c

index 0dcadeb3c584930cfd984107a5d27a3bac711e43..f0afe1dcb75a9d78be32b35d11f7520f98d4c243 100644 (file)
@@ -7,6 +7,7 @@ import copy
 import pickle
 from io import StringIO
 import random
+import struct
 
 BIG = 100000
 
@@ -518,6 +519,21 @@ class TestBasic(unittest.TestCase):
             gc.collect()
             self.assertTrue(ref() is None, "Cycle was not collected")
 
+    check_sizeof = support.check_sizeof
+
+    @support.cpython_only
+    def test_sizeof(self):
+        BLOCKLEN = 62
+        basesize = 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 a968c24adc5c7f52e9fd328e73ed564b92c2a784..d5e3113db0c419a6347f0bd84791e390bc42fe6d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -110,6 +110,9 @@ Library
 - Issue #15512: Add a __sizeof__ implementation for parser.
   Patch by Serhiy Storchaka.
 
+- Issue #15469: Add a __sizeof__ implementation for deque objects.
+  Patch by Serhiy Storchaka.
+
 - Issue #15489: Add a __sizeof__ implementation for BytesIO objects.
   Patch by Serhiy Storchaka.
 
index 434315965ab5a64535d473fc8aea014963a3b012..314bafd1e7d2860884e0931679d8e55623f5ee7e 100644 (file)
@@ -932,6 +932,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)
 {
@@ -995,7 +1012,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 */
 };