]> granicus.if.org Git - python/commitdiff
SF bug #1242657: list(obj) can swallow KeyboardInterrupt
authorRaymond Hettinger <python@rcn.com>
Sun, 21 Aug 2005 11:03:59 +0000 (11:03 +0000)
committerRaymond Hettinger <python@rcn.com>
Sun, 21 Aug 2005 11:03:59 +0000 (11:03 +0000)
Fix over-aggressive PyErr_Clear().  The same code fragment appears in
various guises in list.extend(), map(), filter(), zip(), and internally
in PySequence_Tuple().

Lib/test/list_tests.py
Objects/abstract.c
Objects/listobject.c
Python/bltinmodule.c

index 284edb39c3919b92ba9cf6f24c3b6f69728dbfbe..14b54c77352a319d0d7707298ad4b22fcd323ef8 100644 (file)
@@ -514,3 +514,12 @@ class CommonTest(seq_tests.CommonTest):
         a = self.type2test(range(10))
         a[::2] = tuple(range(5))
         self.assertEqual(a, self.type2test([0, 1, 1, 3, 2, 5, 3, 7, 4, 9]))
+
+    def test_constructor_exception_handling(self):
+        # Bug #1242657
+        class F(object):
+            def __iter__(self):
+                yield 23
+            def __len__(self):
+                raise KeyboardInterrupt
+        self.assertRaises(KeyboardInterrupt, list, F())
index cade2aa19d3c88706a2470eecd5b66212a75f512..94af3da039b19ab2f33a1ebae00d5cd4084039b4 100644 (file)
@@ -1401,6 +1401,11 @@ PySequence_Tuple(PyObject *v)
        /* Guess result size and allocate space. */
        n = PyObject_Size(v);
        if (n < 0) {
+               if (!PyErr_ExceptionMatches(PyExc_TypeError)  &&
+                   !PyErr_ExceptionMatches(PyExc_AttributeError)) {
+                       Py_DECREF(it);
+                       return NULL;
+               }
                PyErr_Clear();
                n = 10;  /* arbitrary */
        }
index 08ab0951c9bda74303aca0e081ef3daccbfabbca..3b7358a312ec8f2a665501ea224e593ad1e57477 100644 (file)
@@ -777,6 +777,11 @@ listextend(PyListObject *self, PyObject *b)
        /* Guess a result list size. */
        n = PyObject_Size(b);
        if (n < 0) {
+               if (!PyErr_ExceptionMatches(PyExc_TypeError)  &&
+                   !PyErr_ExceptionMatches(PyExc_AttributeError)) {
+                       Py_DECREF(it);
+                       return NULL;
+               }
                PyErr_Clear();
                n = 8;  /* arbitrary */
        }
index f63e27adc63b95037a185dd4c1cbe4f8523b591b..af5a55b8e071ad404b5435f51583132f8b757aec 100644 (file)
@@ -223,6 +223,10 @@ builtin_filter(PyObject *self, PyObject *args)
        /* Guess a result list size. */
        len = PyObject_Size(seq);
        if (len < 0) {
+               if (!PyErr_ExceptionMatches(PyExc_TypeError)  &&
+                   !PyErr_ExceptionMatches(PyExc_AttributeError)) {
+                       goto Fail_it;
+               }
                PyErr_Clear();
                len = 8;        /* arbitrary */
        }
@@ -864,6 +868,10 @@ builtin_map(PyObject *self, PyObject *args)
                /* Update len. */
                curlen = PyObject_Size(curseq);
                if (curlen < 0) {
+                       if (!PyErr_ExceptionMatches(PyExc_TypeError)  &&
+                           !PyErr_ExceptionMatches(PyExc_AttributeError)) {
+                               goto Fail_2;
+                       }
                        PyErr_Clear();
                        curlen = 8;  /* arbitrary */
                }
@@ -2097,6 +2105,10 @@ builtin_zip(PyObject *self, PyObject *args)
                PyObject *item = PyTuple_GET_ITEM(args, i);
                int thislen = PyObject_Size(item);
                if (thislen < 0) {
+                       if (!PyErr_ExceptionMatches(PyExc_TypeError)  &&
+                           !PyErr_ExceptionMatches(PyExc_AttributeError)) {
+                               return NULL;
+                       }
                        PyErr_Clear();
                        len = -1;
                        break;