]> granicus.if.org Git - python/commitdiff
#2196 hasattr now allows SystemExit and KeyboardInterrupt to propagate
authorBenjamin Peterson <benjamin@python.org>
Mon, 12 May 2008 00:41:23 +0000 (00:41 +0000)
committerBenjamin Peterson <benjamin@python.org>
Mon, 12 May 2008 00:41:23 +0000 (00:41 +0000)
Lib/test/test_builtin.py
Misc/NEWS
Python/bltinmodule.c

index 1e559eb9ba83bd8088c4463fe752d8a9ac87d373..15d80a369831dc35a384844e57d94410a1e426d8 100644 (file)
@@ -590,6 +590,16 @@ class BuiltinTest(unittest.TestCase):
         if have_unicode:
             self.assertRaises(UnicodeError, hasattr, sys, unichr(sys.maxunicode))
 
+        # Check that hasattr allows SystemExit and KeyboardInterrupts by
+        class A:
+            def __getattr__(self, what):
+                raise KeyboardInterrupt
+        self.assertRaises(KeyboardInterrupt, hasattr, A(), "b")
+        class B:
+            def __getattr__(self, what):
+                raise SystemExit
+        self.assertRaises(SystemExit, hasattr, B(), "b")
+
     def test_hash(self):
         hash(None)
         self.assertEqual(hash(1), hash(1L))
index 252dbe3fa97d9682b7adf444df7713db054383ad..7384c9dcfa3841cbd083b1dfc52826d3e1387696 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -17,6 +17,9 @@ Core and Builtins
 
 - Issue #2790: sys.flags was not properly exposing its bytes_warning attribute.
 
+- Issue #2196: hasattr now lets exceptions which do not inherit Exception
+  (KeyboardInterrupt, and SystemExit) propagate instead of ignoring them
+
 Extension Modules
 -----------------
 
index 02a2faae3cdaa8bafd7a582f752a9edbc3b273ee..0234b6bd809f66e8451d956ae1810499c4592ee4 100644 (file)
@@ -877,9 +877,13 @@ builtin_hasattr(PyObject *self, PyObject *args)
        }
        v = PyObject_GetAttr(v, name);
        if (v == NULL) {
-               PyErr_Clear();
-               Py_INCREF(Py_False);
-               return Py_False;
+               if (!PyErr_ExceptionMatches(PyExc_Exception))
+                       return NULL;
+               else {
+                       PyErr_Clear();
+                       Py_INCREF(Py_False);
+                       return Py_False;
+               }
        }
        Py_DECREF(v);
        Py_INCREF(Py_True);