From b9030f4f0d566c8ae2eeb28ed858f02f3b853ae7 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Mon, 12 May 2008 00:41:23 +0000 Subject: [PATCH] #2196 hasattr now allows SystemExit and KeyboardInterrupt to propagate --- Lib/test/test_builtin.py | 10 ++++++++++ Misc/NEWS | 3 +++ Python/bltinmodule.c | 10 +++++++--- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index 1e559eb9ba8..15d80a36983 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -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)) diff --git a/Misc/NEWS b/Misc/NEWS index 252dbe3fa97..7384c9dcfa3 100644 --- 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 ----------------- diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 02a2faae3cd..0234b6bd809 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -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);