diff -r f691b32cc1fd Lib/test/test_deque.py --- a/Lib/test/test_deque.py Fri Jul 27 14:55:17 2012 +0100 +++ b/Lib/test/test_deque.py Fri Jul 27 18:35:41 2012 +0300 @@ -7,6 +7,8 @@ import pickle from io import StringIO import random +import struct +import sys BIG = 100000 @@ -531,6 +533,21 @@ 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('2P4nlP') + 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): diff -r f691b32cc1fd Modules/_collectionsmodule.c --- a/Modules/_collectionsmodule.c Fri Jul 27 14:55:17 2012 +0100 +++ b/Modules/_collectionsmodule.c Fri Jul 27 18:35:41 2012 +0300 @@ -933,6 +933,22 @@ } static PyObject * +deque_sizeof(dequeobject *deque, void *unused) +{ + Py_ssize_t res; + block *b; + + res = sizeof(dequeobject); + for (b = deque->leftblock; b != NULL; b = b->rightlink) { + res += 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) { if (deque->maxlen == -1) @@ -995,7 +1011,9 @@ {"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 */ };