Issue #15475: Add __sizeof__ implementations for itertools objects.
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index 2672b00..21f1bde 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -10,6 +10,8 @@
 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 @@
                 # 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 @@
 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
diff --git a/Misc/NEWS b/Misc/NEWS
index 2cefcb6..6bd277c 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -21,6 +21,8 @@
 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.
 
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index 8be62ce..db7cdfe 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -2057,6 +2057,18 @@
     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 @@
      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 @@
     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 @@
      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 @@
     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 @@
      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 @@
     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 @@
      reduce_doc},
     {"__setstate__",    (PyCFunction)permutations_setstate,    METH_O,
      setstate_doc},
+    {"__sizeof__",      (PyCFunction)permutations_sizeof,      METH_NOARGS,
+     sizeof_doc},
     {NULL,              NULL}   /* sentinel */
 };