[Python-checkins] r63119 - in python/trunk: Lib/test/test_builtin.py Misc/NEWS Python/bltinmodule.c

benjamin.peterson python-checkins at python.org
Mon May 12 02:41:23 CEST 2008


Author: benjamin.peterson
Date: Mon May 12 02:41:23 2008
New Revision: 63119

Log:
#2196 hasattr now allows SystemExit and KeyboardInterrupt to propagate


Modified:
   python/trunk/Lib/test/test_builtin.py
   python/trunk/Misc/NEWS
   python/trunk/Python/bltinmodule.c

Modified: python/trunk/Lib/test/test_builtin.py
==============================================================================
--- python/trunk/Lib/test/test_builtin.py	(original)
+++ python/trunk/Lib/test/test_builtin.py	Mon May 12 02:41:23 2008
@@ -590,6 +590,16 @@
         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))

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Mon May 12 02:41:23 2008
@@ -17,6 +17,9 @@
 
 - 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
 -----------------
 

Modified: python/trunk/Python/bltinmodule.c
==============================================================================
--- python/trunk/Python/bltinmodule.c	(original)
+++ python/trunk/Python/bltinmodule.c	Mon May 12 02:41:23 2008
@@ -877,9 +877,13 @@
 	}
 	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);


More information about the Python-checkins mailing list