mirror of
https://github.com/python/cpython.git
synced 2025-08-25 19:24:42 +00:00
Issue #15475: Add __sizeof__ implementations for itertools objects.
This commit is contained in:
parent
a881a7f205
commit
2dae92a807
3 changed files with 95 additions and 1 deletions
|
@ -10,6 +10,8 @@ import random
|
||||||
import copy
|
import copy
|
||||||
import pickle
|
import pickle
|
||||||
from functools import reduce
|
from functools import reduce
|
||||||
|
import sys
|
||||||
|
import struct
|
||||||
maxsize = support.MAX_Py_ssize_t
|
maxsize = support.MAX_Py_ssize_t
|
||||||
minsize = -maxsize-1
|
minsize = -maxsize-1
|
||||||
|
|
||||||
|
@ -1807,6 +1809,44 @@ class SubclassWithKwargsTest(unittest.TestCase):
|
||||||
# we expect type errors because of wrong argument count
|
# we expect type errors because of wrong argument count
|
||||||
self.assertNotIn("does not take keyword arguments", err.args[0])
|
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
|
libreftest = """ Doctest for examples in the library reference: libitertools.tex
|
||||||
|
|
||||||
|
@ -2042,7 +2082,8 @@ __test__ = {'libreftest' : libreftest}
|
||||||
def test_main(verbose=None):
|
def test_main(verbose=None):
|
||||||
test_classes = (TestBasicOps, TestVariousIteratorArgs, TestGC,
|
test_classes = (TestBasicOps, TestVariousIteratorArgs, TestGC,
|
||||||
RegressionTests, LengthTransparency,
|
RegressionTests, LengthTransparency,
|
||||||
SubclassWithKwargsTest, TestExamples)
|
SubclassWithKwargsTest, TestExamples,
|
||||||
|
SizeofTest)
|
||||||
support.run_unittest(*test_classes)
|
support.run_unittest(*test_classes)
|
||||||
|
|
||||||
# verify reference counting
|
# verify reference counting
|
||||||
|
|
|
@ -21,6 +21,8 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #15475: Add __sizeof__ implementations for itertools objects.
|
||||||
|
|
||||||
- Issue #19880: Fix a reference leak in unittest.TestCase. Explicitly break
|
- Issue #19880: Fix a reference leak in unittest.TestCase. Explicitly break
|
||||||
reference cycles between frames and the _Outcome instance.
|
reference cycles between frames and the _Outcome instance.
|
||||||
|
|
||||||
|
|
|
@ -2057,6 +2057,18 @@ product_dealloc(productobject *lz)
|
||||||
Py_TYPE(lz)->tp_free(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
|
static int
|
||||||
product_traverse(productobject *lz, visitproc visit, void *arg)
|
product_traverse(productobject *lz, visitproc visit, void *arg)
|
||||||
{
|
{
|
||||||
|
@ -2226,6 +2238,8 @@ static PyMethodDef product_methods[] = {
|
||||||
reduce_doc},
|
reduce_doc},
|
||||||
{"__setstate__", (PyCFunction)product_setstate, METH_O,
|
{"__setstate__", (PyCFunction)product_setstate, METH_O,
|
||||||
setstate_doc},
|
setstate_doc},
|
||||||
|
{"__sizeof__", (PyCFunction)product_sizeof, METH_NOARGS,
|
||||||
|
sizeof_doc},
|
||||||
{NULL, NULL} /* sentinel */
|
{NULL, NULL} /* sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2366,6 +2380,16 @@ combinations_dealloc(combinationsobject *co)
|
||||||
Py_TYPE(co)->tp_free(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
|
static int
|
||||||
combinations_traverse(combinationsobject *co, visitproc visit, void *arg)
|
combinations_traverse(combinationsobject *co, visitproc visit, void *arg)
|
||||||
{
|
{
|
||||||
|
@ -2537,6 +2561,8 @@ static PyMethodDef combinations_methods[] = {
|
||||||
reduce_doc},
|
reduce_doc},
|
||||||
{"__setstate__", (PyCFunction)combinations_setstate, METH_O,
|
{"__setstate__", (PyCFunction)combinations_setstate, METH_O,
|
||||||
setstate_doc},
|
setstate_doc},
|
||||||
|
{"__sizeof__", (PyCFunction)combinations_sizeof, METH_NOARGS,
|
||||||
|
sizeof_doc},
|
||||||
{NULL, NULL} /* sentinel */
|
{NULL, NULL} /* sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2695,6 +2721,16 @@ cwr_dealloc(cwrobject *co)
|
||||||
Py_TYPE(co)->tp_free(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
|
static int
|
||||||
cwr_traverse(cwrobject *co, visitproc visit, void *arg)
|
cwr_traverse(cwrobject *co, visitproc visit, void *arg)
|
||||||
{
|
{
|
||||||
|
@ -2854,6 +2890,8 @@ static PyMethodDef cwr_methods[] = {
|
||||||
reduce_doc},
|
reduce_doc},
|
||||||
{"__setstate__", (PyCFunction)cwr_setstate, METH_O,
|
{"__setstate__", (PyCFunction)cwr_setstate, METH_O,
|
||||||
setstate_doc},
|
setstate_doc},
|
||||||
|
{"__sizeof__", (PyCFunction)cwr_sizeof, METH_NOARGS,
|
||||||
|
sizeof_doc},
|
||||||
{NULL, NULL} /* sentinel */
|
{NULL, NULL} /* sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -3030,6 +3068,17 @@ permutations_dealloc(permutationsobject *po)
|
||||||
Py_TYPE(po)->tp_free(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
|
static int
|
||||||
permutations_traverse(permutationsobject *po, visitproc visit, void *arg)
|
permutations_traverse(permutationsobject *po, visitproc visit, void *arg)
|
||||||
{
|
{
|
||||||
|
@ -3235,6 +3284,8 @@ static PyMethodDef permuations_methods[] = {
|
||||||
reduce_doc},
|
reduce_doc},
|
||||||
{"__setstate__", (PyCFunction)permutations_setstate, METH_O,
|
{"__setstate__", (PyCFunction)permutations_setstate, METH_O,
|
||||||
setstate_doc},
|
setstate_doc},
|
||||||
|
{"__sizeof__", (PyCFunction)permutations_sizeof, METH_NOARGS,
|
||||||
|
sizeof_doc},
|
||||||
{NULL, NULL} /* sentinel */
|
{NULL, NULL} /* sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue