Skip to content

Commit b883112

Browse files
author
benjamin.peterson
committed
#2196 hasattr now allows SystemExit and KeyboardInterrupt to propagate
git-svn-id: http://svn.python.org/projects/python/trunk@63119 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 0b2ba35 commit b883112

3 files changed

Lines changed: 20 additions & 3 deletions

File tree

Lib/test/test_builtin.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,16 @@ def test_hasattr(self):
590590
if have_unicode:
591591
self.assertRaises(UnicodeError, hasattr, sys, unichr(sys.maxunicode))
592592

593+
# Check that hasattr allows SystemExit and KeyboardInterrupts by
594+
class A:
595+
def __getattr__(self, what):
596+
raise KeyboardInterrupt
597+
self.assertRaises(KeyboardInterrupt, hasattr, A(), "b")
598+
class B:
599+
def __getattr__(self, what):
600+
raise SystemExit
601+
self.assertRaises(SystemExit, hasattr, B(), "b")
602+
593603
def test_hash(self):
594604
hash(None)
595605
self.assertEqual(hash(1), hash(1L))

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ Core and Builtins
1717

1818
- Issue #2790: sys.flags was not properly exposing its bytes_warning attribute.
1919

20+
- Issue #2196: hasattr now lets exceptions which do not inherit Exception
21+
(KeyboardInterrupt, and SystemExit) propagate instead of ignoring them
22+
2023
Extension Modules
2124
-----------------
2225

Python/bltinmodule.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -877,9 +877,13 @@ builtin_hasattr(PyObject *self, PyObject *args)
877877
}
878878
v = PyObject_GetAttr(v, name);
879879
if (v == NULL) {
880-
PyErr_Clear();
881-
Py_INCREF(Py_False);
882-
return Py_False;
880+
if (!PyErr_ExceptionMatches(PyExc_Exception))
881+
return NULL;
882+
else {
883+
PyErr_Clear();
884+
Py_INCREF(Py_False);
885+
return Py_False;
886+
}
883887
}
884888
Py_DECREF(v);
885889
Py_INCREF(Py_True);

0 commit comments

Comments
 (0)