Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
2dae92a8
Commit
2dae92a8
authored
Dec 09, 2013
by
Serhiy Storchaka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #15475: Add __sizeof__ implementations for itertools objects.
parent
a881a7f2
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
95 additions
and
1 deletion
+95
-1
Lib/test/test_itertools.py
Lib/test/test_itertools.py
+42
-1
Misc/NEWS
Misc/NEWS
+2
-0
Modules/itertoolsmodule.c
Modules/itertoolsmodule.c
+51
-0
No files found.
Lib/test/test_itertools.py
View file @
2dae92a8
...
...
@@ -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
...
...
Misc/NEWS
View file @
2dae92a8
...
...
@@ -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.
...
...
Modules/itertoolsmodule.c
View file @
2dae92a8
...
...
@@ -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 */
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment