Skip to content

Commit a710b33

Browse files
committed
SF bug #1242657: list(obj) can swallow KeyboardInterrupt
Fix over-aggressive PyErr_Clear(). The same code fragment appears in various guises in list.extend(), map(), filter(), zip(), and internally in PySequence_Tuple().
1 parent b285974 commit a710b33

4 files changed

Lines changed: 31 additions & 0 deletions

File tree

Lib/test/list_tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,3 +514,12 @@ def test_extendedslicing(self):
514514
a = self.type2test(range(10))
515515
a[::2] = tuple(range(5))
516516
self.assertEqual(a, self.type2test([0, 1, 1, 3, 2, 5, 3, 7, 4, 9]))
517+
518+
def test_constructor_exception_handling(self):
519+
# Bug #1242657
520+
class F(object):
521+
def __iter__(self):
522+
yield 23
523+
def __len__(self):
524+
raise KeyboardInterrupt
525+
self.assertRaises(KeyboardInterrupt, list, F())

Objects/abstract.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,11 @@ PySequence_Tuple(PyObject *v)
14011401
/* Guess result size and allocate space. */
14021402
n = PyObject_Size(v);
14031403
if (n < 0) {
1404+
if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
1405+
!PyErr_ExceptionMatches(PyExc_AttributeError)) {
1406+
Py_DECREF(it);
1407+
return NULL;
1408+
}
14041409
PyErr_Clear();
14051410
n = 10; /* arbitrary */
14061411
}

Objects/listobject.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,11 @@ listextend(PyListObject *self, PyObject *b)
777777
/* Guess a result list size. */
778778
n = PyObject_Size(b);
779779
if (n < 0) {
780+
if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
781+
!PyErr_ExceptionMatches(PyExc_AttributeError)) {
782+
Py_DECREF(it);
783+
return NULL;
784+
}
780785
PyErr_Clear();
781786
n = 8; /* arbitrary */
782787
}

Python/bltinmodule.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ builtin_filter(PyObject *self, PyObject *args)
223223
/* Guess a result list size. */
224224
len = PyObject_Size(seq);
225225
if (len < 0) {
226+
if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
227+
!PyErr_ExceptionMatches(PyExc_AttributeError)) {
228+
goto Fail_it;
229+
}
226230
PyErr_Clear();
227231
len = 8; /* arbitrary */
228232
}
@@ -864,6 +868,10 @@ builtin_map(PyObject *self, PyObject *args)
864868
/* Update len. */
865869
curlen = PyObject_Size(curseq);
866870
if (curlen < 0) {
871+
if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
872+
!PyErr_ExceptionMatches(PyExc_AttributeError)) {
873+
goto Fail_2;
874+
}
867875
PyErr_Clear();
868876
curlen = 8; /* arbitrary */
869877
}
@@ -2097,6 +2105,10 @@ builtin_zip(PyObject *self, PyObject *args)
20972105
PyObject *item = PyTuple_GET_ITEM(args, i);
20982106
int thislen = PyObject_Size(item);
20992107
if (thislen < 0) {
2108+
if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
2109+
!PyErr_ExceptionMatches(PyExc_AttributeError)) {
2110+
return NULL;
2111+
}
21002112
PyErr_Clear();
21012113
len = -1;
21022114
break;

0 commit comments

Comments
 (0)