]> granicus.if.org Git - python/commitdiff
Issue #15475: Add __sizeof__ implementations for itertools objects.
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 9 Dec 2013 15:45:57 +0000 (17:45 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Mon, 9 Dec 2013 15:45:57 +0000 (17:45 +0200)
Lib/test/test_itertools.py
Misc/NEWS
Modules/itertoolsmodule.c

index 2672b00c56f421684f9e0931cc7b96c0cd99183d..21f1bdeb82c4d14b4fee6f8a011948d195e32bb8 100644 (file)
@@ -10,6 +10,8 @@ import random
 import copy
 import pickle
 from functools import reduce
+import sys
+import struct
 maxsize = support.MAX_Py_ssize_t
 minsize = -maxsize-1
 
@@ -1807,6 +1809,44 @@ class SubclassWithKwargsTest(unittest.TestCase):
                 # we expect type errors because of wrong argument count
                 self.assertNotIn("does not take keyword arguments", err.args[0])
 
+@support.cpython_only
+class SizeofTest(unittest.TestCase):
+    def setUp(self):
+        self.ssize_t = struct.calcsize('n')
+
+    check_sizeof = support.check_sizeof
+
+    def test_product_sizeof(self):
+        basesize = support.calcobjsize('3Pi')
+        check = self.check_sizeof
+        check(product('ab', '12'), basesize + 2 * self.ssize_t)
+        check(product(*(('abc',) * 10)), basesize + 10 * self.ssize_t)
+
+    def test_combinations_sizeof(self):
+        basesize = support.calcobjsize('3Pni')
+        check = self.check_sizeof
+        check(combinations('abcd', 3), basesize + 3 * self.ssize_t)
+        check(combinations(range(10), 4), basesize + 4 * self.ssize_t)
+
+    def test_combinations_with_replacement_sizeof(self):
+        cwr = combinations_with_replacement
+        basesize = support.calcobjsize('3Pni')
+        check = self.check_sizeof
+        check(cwr('abcd', 3), basesize + 3 * self.ssize_t)
+        check(cwr(range(10), 4), basesize + 4 * self.ssize_t)
+
+    def test_permutations_sizeof(self):
+        basesize = support.calcobjsize('4Pni')
+        check = self.check_sizeof
+        check(permutations('abcd'),
+              basesize + 4 * self.ssize_t + 4 * self.ssize_t)
+        check(permutations('abcd', 3),
+              basesize + 4 * self.ssize_t + 3 * self.ssize_t)
+        check(permutations('abcde', 3),
+              basesize + 5 * self.ssize_t + 3 * self.ssize_t)
+        check(permutations(range(10), 4),
+              basesize + 10 * self.ssize_t + 4 * self.ssize_t)
+
 
 libreftest = """ Doctest for examples in the library reference: libitertools.tex
 
@@ -2042,7 +2082,8 @@ __test__ = {'libreftest' : libreftest}
 def test_main(verbose=None):
     test_classes = (TestBasicOps, TestVariousIteratorArgs, TestGC,
                     RegressionTests, LengthTransparency,
-                    SubclassWithKwargsTest, TestExamples)
+                    SubclassWithKwargsTest, TestExamples,
+                    SizeofTest)
     support.run_unittest(*test_classes)
 
     # verify reference counting
index 2cefcb65b248b9ba5cda26ef209613f3052d5626..6bd277cf8cce1c2c22fd3e5e3f33248b85bcddfe 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -21,6 +21,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #15475: Add __sizeof__ implementations for itertools objects.
+
 - Issue #19880: Fix a reference leak in unittest.TestCase. Explicitly break
   reference cycles between frames and the _Outcome instance.
 
index 8be62ce94c04f987d022b4979a1972f5834e8efd..db7cdfeefeb5404b4d86f3d7f87c0e52c10a6f3e 100644 (file)
@@ -2057,6 +2057,18 @@ product_dealloc(productobject *lz)
     Py_TYPE(lz)->tp_free(lz);
 }
 
+static PyObject *
+product_sizeof(productobject *lz, void *unused)
+{
+    Py_ssize_t res;
+
+    res = sizeof(productobject);
+    res += PyTuple_GET_SIZE(lz->pools) * sizeof(Py_ssize_t);
+    return PyLong_FromSsize_t(res);
+}
+
+PyDoc_STRVAR(sizeof_doc, "Returns size in memory, in bytes.");
+
 static int
 product_traverse(productobject *lz, visitproc visit, void *arg)
 {
@@ -2226,6 +2238,8 @@ static PyMethodDef product_methods[] = {
      reduce_doc},
     {"__setstate__",    (PyCFunction)product_setstate,    METH_O,
      setstate_doc},
+    {"__sizeof__",      (PyCFunction)product_sizeof,      METH_NOARGS,
+     sizeof_doc},
     {NULL,              NULL}   /* sentinel */
 };
 
@@ -2366,6 +2380,16 @@ combinations_dealloc(combinationsobject *co)
     Py_TYPE(co)->tp_free(co);
 }
 
+static PyObject *
+combinations_sizeof(combinationsobject *co, void *unused)
+{
+    Py_ssize_t res;
+
+    res = sizeof(combinationsobject);
+    res += co->r * sizeof(Py_ssize_t);
+    return PyLong_FromSsize_t(res);
+}
+
 static int
 combinations_traverse(combinationsobject *co, visitproc visit, void *arg)
 {
@@ -2537,6 +2561,8 @@ static PyMethodDef combinations_methods[] = {
      reduce_doc},
     {"__setstate__",    (PyCFunction)combinations_setstate,    METH_O,
      setstate_doc},
+    {"__sizeof__",      (PyCFunction)combinations_sizeof,      METH_NOARGS,
+     sizeof_doc},
     {NULL,              NULL}   /* sentinel */
 };
 
@@ -2695,6 +2721,16 @@ cwr_dealloc(cwrobject *co)
     Py_TYPE(co)->tp_free(co);
 }
 
+static PyObject *
+cwr_sizeof(cwrobject *co, void *unused)
+{
+    Py_ssize_t res;
+
+    res = sizeof(cwrobject);
+    res += co->r * sizeof(Py_ssize_t);
+    return PyLong_FromSsize_t(res);
+}
+
 static int
 cwr_traverse(cwrobject *co, visitproc visit, void *arg)
 {
@@ -2854,6 +2890,8 @@ static PyMethodDef cwr_methods[] = {
      reduce_doc},
     {"__setstate__",    (PyCFunction)cwr_setstate,    METH_O,
      setstate_doc},
+    {"__sizeof__",      (PyCFunction)cwr_sizeof,      METH_NOARGS,
+     sizeof_doc},
     {NULL,              NULL}   /* sentinel */
 };
 
@@ -3030,6 +3068,17 @@ permutations_dealloc(permutationsobject *po)
     Py_TYPE(po)->tp_free(po);
 }
 
+static PyObject *
+permutations_sizeof(permutationsobject *po, void *unused)
+{
+    Py_ssize_t res;
+
+    res = sizeof(permutationsobject);
+    res += PyTuple_GET_SIZE(po->pool) * sizeof(Py_ssize_t);
+    res += po->r * sizeof(Py_ssize_t);
+    return PyLong_FromSsize_t(res);
+}
+
 static int
 permutations_traverse(permutationsobject *po, visitproc visit, void *arg)
 {
@@ -3235,6 +3284,8 @@ static PyMethodDef permuations_methods[] = {
      reduce_doc},
     {"__setstate__",    (PyCFunction)permutations_setstate,    METH_O,
      setstate_doc},
+    {"__sizeof__",      (PyCFunction)permutations_sizeof,      METH_NOARGS,
+     sizeof_doc},
     {NULL,              NULL}   /* sentinel */
 };