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
fa7dadd3
Commit
fa7dadd3
authored
Nov 01, 2009
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix exception handling in itertools.izip_longest().
parent
38f5d8fc
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
80 additions
and
35 deletions
+80
-35
Lib/test/test_itertools.py
Lib/test/test_itertools.py
+40
-0
Misc/NEWS
Misc/NEWS
+3
-0
Modules/itertoolsmodule.c
Modules/itertoolsmodule.c
+37
-35
No files found.
Lib/test/test_itertools.py
View file @
fa7dadd3
...
@@ -571,6 +571,46 @@ class TestBasicOps(unittest.TestCase):
...
@@ -571,6 +571,46 @@ class TestBasicOps(unittest.TestCase):
ids
=
map
(
id
,
list
(
izip_longest
(
'abc'
,
'def'
)))
ids
=
map
(
id
,
list
(
izip_longest
(
'abc'
,
'def'
)))
self
.
assertEqual
(
len
(
dict
.
fromkeys
(
ids
)),
len
(
ids
))
self
.
assertEqual
(
len
(
dict
.
fromkeys
(
ids
)),
len
(
ids
))
def
test_bug_7244
(
self
):
class
Repeater
(
object
):
# this class is similar to itertools.repeat
def
__init__
(
self
,
o
,
t
,
e
):
self
.
o
=
o
self
.
t
=
int
(
t
)
self
.
e
=
e
def
__iter__
(
self
):
# its iterator is itself
return
self
def
next
(
self
):
if
self
.
t
>
0
:
self
.
t
-=
1
return
self
.
o
else
:
raise
self
.
e
# Formerly this code in would fail in debug mode
# with Undetected Error and Stop Iteration
r1
=
Repeater
(
1
,
3
,
StopIteration
)
r2
=
Repeater
(
2
,
4
,
StopIteration
)
def
run
(
r1
,
r2
):
result
=
[]
for
i
,
j
in
izip_longest
(
r1
,
r2
,
fillvalue
=
0
):
with
test_support
.
captured_output
(
'stdout'
):
print
(
i
,
j
)
result
.
append
((
i
,
j
))
return
result
self
.
assertEqual
(
run
(
r1
,
r2
),
[(
1
,
2
),
(
1
,
2
),
(
1
,
2
),
(
0
,
2
)])
# Formerly, the RuntimeError would be lost
# and StopIteration would stop as expected
r1
=
Repeater
(
1
,
3
,
RuntimeError
)
r2
=
Repeater
(
2
,
4
,
StopIteration
)
it
=
izip_longest
(
r1
,
r2
,
fillvalue
=
0
)
self
.
assertEqual
(
next
(
it
),
(
1
,
2
))
self
.
assertEqual
(
next
(
it
),
(
1
,
2
))
self
.
assertEqual
(
next
(
it
),
(
1
,
2
))
self
.
assertRaises
(
RuntimeError
,
next
,
it
)
def
test_product
(
self
):
def
test_product
(
self
):
for
args
,
result
in
[
for
args
,
result
in
[
([],
[()]),
# zero iterables
([],
[()]),
# zero iterables
...
...
Misc/NEWS
View file @
fa7dadd3
...
@@ -15,6 +15,9 @@ Core and Builtins
...
@@ -15,6 +15,9 @@ Core and Builtins
- Remove length limitation when constructing a complex number from a
- Remove length limitation when constructing a complex number from a
unicode string.
unicode string.
- Issue #7244: itertools.izip_longest() no longer ignores exceptions
raised during the formation of an output tuple.
- Issue #1087418: Boost performance of bitwise operations for longs.
- Issue #1087418: Boost performance of bitwise operations for longs.
- Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
- Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
...
...
Modules/itertoolsmodule.c
View file @
fa7dadd3
...
@@ -3865,71 +3865,73 @@ izip_longest_traverse(iziplongestobject *lz, visitproc visit, void *arg)
...
@@ -3865,71 +3865,73 @@ izip_longest_traverse(iziplongestobject *lz, visitproc visit, void *arg)
static
PyObject
*
static
PyObject
*
izip_longest_next
(
iziplongestobject
*
lz
)
izip_longest_next
(
iziplongestobject
*
lz
)
{
{
Py_ssize_t
i
;
Py_ssize_t
i
;
Py_ssize_t
tuplesize
=
lz
->
tuplesize
;
Py_ssize_t
tuplesize
=
lz
->
tuplesize
;
PyObject
*
result
=
lz
->
result
;
PyObject
*
result
=
lz
->
result
;
PyObject
*
it
;
PyObject
*
it
;
PyObject
*
item
;
PyObject
*
item
;
PyObject
*
olditem
;
PyObject
*
olditem
;
if
(
tuplesize
==
0
)
if
(
tuplesize
==
0
)
return
NULL
;
return
NULL
;
if
(
lz
->
numactive
==
0
)
if
(
lz
->
numactive
==
0
)
return
NULL
;
return
NULL
;
if
(
Py_REFCNT
(
result
)
==
1
)
{
if
(
Py_REFCNT
(
result
)
==
1
)
{
Py_INCREF
(
result
);
Py_INCREF
(
result
);
for
(
i
=
0
;
i
<
tuplesize
;
i
++
)
{
for
(
i
=
0
;
i
<
tuplesize
;
i
++
)
{
it
=
PyTuple_GET_ITEM
(
lz
->
ittuple
,
i
);
it
=
PyTuple_GET_ITEM
(
lz
->
ittuple
,
i
);
if
(
it
==
NULL
)
{
if
(
it
==
NULL
)
{
Py_INCREF
(
lz
->
fillvalue
);
Py_INCREF
(
lz
->
fillvalue
);
item
=
lz
->
fillvalue
;
item
=
lz
->
fillvalue
;
}
else
{
}
else
{
item
=
(
*
Py_TYPE
(
it
)
->
tp_iternext
)
(
it
);
item
=
PyIter_Next
(
it
);
if
(
item
==
NULL
)
{
if
(
item
==
NULL
)
{
lz
->
numactive
-=
1
;
lz
->
numactive
-=
1
;
if
(
lz
->
numactive
==
0
)
{
if
(
lz
->
numactive
==
0
||
PyErr_Occurred
())
{
lz
->
numactive
=
0
;
Py_DECREF
(
result
);
Py_DECREF
(
result
);
return
NULL
;
return
NULL
;
}
else
{
}
else
{
Py_INCREF
(
lz
->
fillvalue
);
Py_INCREF
(
lz
->
fillvalue
);
item
=
lz
->
fillvalue
;
item
=
lz
->
fillvalue
;
PyTuple_SET_ITEM
(
lz
->
ittuple
,
i
,
NULL
);
PyTuple_SET_ITEM
(
lz
->
ittuple
,
i
,
NULL
);
Py_DECREF
(
it
);
Py_DECREF
(
it
);
}
}
}
}
}
}
olditem
=
PyTuple_GET_ITEM
(
result
,
i
);
olditem
=
PyTuple_GET_ITEM
(
result
,
i
);
PyTuple_SET_ITEM
(
result
,
i
,
item
);
PyTuple_SET_ITEM
(
result
,
i
,
item
);
Py_DECREF
(
olditem
);
Py_DECREF
(
olditem
);
}
}
}
else
{
}
else
{
result
=
PyTuple_New
(
tuplesize
);
result
=
PyTuple_New
(
tuplesize
);
if
(
result
==
NULL
)
if
(
result
==
NULL
)
return
NULL
;
return
NULL
;
for
(
i
=
0
;
i
<
tuplesize
;
i
++
)
{
for
(
i
=
0
;
i
<
tuplesize
;
i
++
)
{
it
=
PyTuple_GET_ITEM
(
lz
->
ittuple
,
i
);
it
=
PyTuple_GET_ITEM
(
lz
->
ittuple
,
i
);
if
(
it
==
NULL
)
{
if
(
it
==
NULL
)
{
Py_INCREF
(
lz
->
fillvalue
);
Py_INCREF
(
lz
->
fillvalue
);
item
=
lz
->
fillvalue
;
item
=
lz
->
fillvalue
;
}
else
{
}
else
{
item
=
(
*
Py_TYPE
(
it
)
->
tp_iternext
)
(
it
);
item
=
PyIter_Next
(
it
);
if
(
item
==
NULL
)
{
if
(
item
==
NULL
)
{
lz
->
numactive
-=
1
;
lz
->
numactive
-=
1
;
if
(
lz
->
numactive
==
0
)
{
if
(
lz
->
numactive
==
0
||
PyErr_Occurred
())
{
lz
->
numactive
=
0
;
Py_DECREF
(
result
);
Py_DECREF
(
result
);
return
NULL
;
return
NULL
;
}
else
{
}
else
{
Py_INCREF
(
lz
->
fillvalue
);
Py_INCREF
(
lz
->
fillvalue
);
item
=
lz
->
fillvalue
;
item
=
lz
->
fillvalue
;
PyTuple_SET_ITEM
(
lz
->
ittuple
,
i
,
NULL
);
PyTuple_SET_ITEM
(
lz
->
ittuple
,
i
,
NULL
);
Py_DECREF
(
it
);
Py_DECREF
(
it
);
}
}
}
}
}
}
PyTuple_SET_ITEM
(
result
,
i
,
item
);
PyTuple_SET_ITEM
(
result
,
i
,
item
);
}
}
}
}
return
result
;
return
result
;
}
}
PyDoc_STRVAR
(
izip_longest_doc
,
PyDoc_STRVAR
(
izip_longest_doc
,
...
...
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