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
2f045dbe
Commit
2f045dbe
authored
Sep 28, 2004
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
* Increase test coverage.
* Have groupby() be careful about decreffing structure members.
parent
e6e9f1eb
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
5 deletions
+38
-5
Lib/test/test_itertools.py
Lib/test/test_itertools.py
+29
-0
Modules/itertoolsmodule.c
Modules/itertoolsmodule.c
+9
-5
No files found.
Lib/test/test_itertools.py
View file @
2f045dbe
...
...
@@ -53,6 +53,10 @@ class TestBasicOps(unittest.TestCase):
self
.
assertRaises
(
TypeError
,
count
,
'a'
)
c
=
count
(
sys
.
maxint
-
2
)
# verify that rollover doesn't crash
c
.
next
();
c
.
next
();
c
.
next
();
c
.
next
();
c
.
next
()
c
=
count
(
3
)
self
.
assertEqual
(
repr
(
c
),
'count(3)'
)
c
.
next
()
self
.
assertEqual
(
repr
(
c
),
'count(4)'
)
def
test_cycle
(
self
):
self
.
assertEqual
(
take
(
10
,
cycle
(
'abc'
)),
list
(
'abcabcabca'
))
...
...
@@ -67,6 +71,7 @@ class TestBasicOps(unittest.TestCase):
self
.
assertEqual
([],
list
(
groupby
([],
key
=
id
)))
self
.
assertRaises
(
TypeError
,
list
,
groupby
(
'abc'
,
[]))
self
.
assertRaises
(
TypeError
,
groupby
,
None
)
self
.
assertRaises
(
TypeError
,
groupby
,
'abc'
,
lambda
x
:
x
,
10
)
# Check normal input
s
=
[(
0
,
10
,
20
),
(
0
,
11
,
21
),
(
0
,
12
,
21
),
(
1
,
13
,
21
),
(
1
,
14
,
22
),
...
...
@@ -199,6 +204,12 @@ class TestBasicOps(unittest.TestCase):
self
.
assertRaises
(
TypeError
,
repeat
)
self
.
assertRaises
(
TypeError
,
repeat
,
None
,
3
,
4
)
self
.
assertRaises
(
TypeError
,
repeat
,
None
,
'a'
)
r
=
repeat
(
1
+
0j
)
self
.
assertEqual
(
repr
(
r
),
'repeat((1+0j))'
)
r
=
repeat
(
1
+
0j
,
5
)
self
.
assertEqual
(
repr
(
r
),
'repeat((1+0j), 5)'
)
list
(
r
)
self
.
assertEqual
(
repr
(
r
),
'repeat((1+0j), 0)'
)
def
test_imap
(
self
):
self
.
assertEqual
(
list
(
imap
(
operator
.
pow
,
range
(
3
),
range
(
1
,
7
))),
...
...
@@ -275,6 +286,9 @@ class TestBasicOps(unittest.TestCase):
self
.
assertRaises
(
TypeError
,
takewhile
,
operator
.
pow
,
[(
4
,
5
)],
'extra'
)
self
.
assertRaises
(
TypeError
,
takewhile
(
10
,
[(
4
,
5
)]).
next
)
self
.
assertRaises
(
ValueError
,
takewhile
(
errfunc
,
[(
4
,
5
)]).
next
)
t
=
takewhile
(
bool
,
[
1
,
1
,
1
,
0
,
0
,
0
])
self
.
assertEqual
(
list
(
t
),
[
1
,
1
,
1
])
self
.
assertRaises
(
StopIteration
,
t
.
next
)
def
test_dropwhile
(
self
):
data
=
[
1
,
3
,
5
,
20
,
2
,
4
,
6
,
8
]
...
...
@@ -347,11 +361,26 @@ class TestBasicOps(unittest.TestCase):
self
.
assertEqual
(
list
(
a
),
range
(
100
,
2000
))
self
.
assertEqual
(
list
(
c
),
range
(
2
,
2000
))
# test values of n
self
.
assertRaises
(
TypeError
,
tee
,
'abc'
,
'invalid'
)
for
n
in
xrange
(
5
):
result
=
tee
(
'abc'
,
n
)
self
.
assertEqual
(
type
(
result
),
tuple
)
self
.
assertEqual
(
len
(
result
),
n
)
self
.
assertEqual
(
map
(
list
,
result
),
[
list
(
'abc'
)]
*
n
)
# tee pass-through to copyable iterator
a
,
b
=
tee
(
'abc'
)
c
,
d
=
tee
(
a
)
self
.
assert_
(
a
is
c
)
# test tee_new
t1
,
t2
=
tee
(
'abc'
)
tnew
=
type
(
t1
)
self
.
assertRaises
(
TypeError
,
tnew
)
self
.
assertRaises
(
TypeError
,
tnew
,
10
)
t3
=
tnew
(
t1
)
self
.
assert_
(
list
(
t1
)
==
list
(
t2
)
==
list
(
t3
)
==
list
(
'abc'
))
def
test_StopIteration
(
self
):
self
.
assertRaises
(
StopIteration
,
izip
().
next
)
...
...
Modules/itertoolsmodule.c
View file @
2f045dbe
...
...
@@ -75,7 +75,7 @@ groupby_traverse(groupbyobject *gbo, visitproc visit, void *arg)
static
PyObject
*
groupby_next
(
groupbyobject
*
gbo
)
{
PyObject
*
newvalue
,
*
newkey
,
*
r
,
*
grouper
;
PyObject
*
newvalue
,
*
newkey
,
*
r
,
*
grouper
,
*
tmp
;
/* skip to next iteration group */
for
(;;)
{
...
...
@@ -110,15 +110,19 @@ groupby_next(groupbyobject *gbo)
}
}
Py_XDECREF
(
gbo
->
currkey
)
;
tmp
=
gbo
->
currkey
;
gbo
->
currkey
=
newkey
;
Py_XDECREF
(
gbo
->
currvalue
);
Py_XDECREF
(
tmp
);
tmp
=
gbo
->
currvalue
;
gbo
->
currvalue
=
newvalue
;
Py_XDECREF
(
tmp
);
}
Py_XDECREF
(
gbo
->
tgtkey
);
gbo
->
tgtkey
=
gbo
->
currkey
;
Py_INCREF
(
gbo
->
currkey
);
tmp
=
gbo
->
tgtkey
;
gbo
->
tgtkey
=
gbo
->
currkey
;
Py_XDECREF
(
tmp
);
grouper
=
_grouper_create
(
gbo
,
gbo
->
tgtkey
);
if
(
grouper
==
NULL
)
...
...
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