Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1768999

Browse files
committed
only catch AttributeError in hasattr() #9666
1 parent 9cf5ef4 commit 1768999

5 files changed

Lines changed: 19 additions & 17 deletions

File tree

Doc/library/functions.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -463,10 +463,10 @@ are always available. They are listed here in alphabetical order.
463463

464464
.. function:: hasattr(object, name)
465465

466-
The arguments are an object and a string. The result is ``True`` if the string
467-
is the name of one of the object's attributes, ``False`` if not. (This is
468-
implemented by calling ``getattr(object, name)`` and seeing whether it raises an
469-
exception or not.)
466+
The arguments are an object and a string. The result is ``True`` if the
467+
string is the name of one of the object's attributes, ``False`` if not. (This
468+
is implemented by calling ``getattr(object, name)`` and seeing whether it
469+
raises an :exc:`AttributeError` or not.)
470470

471471

472472
.. function:: hash(object)

Lib/test/test_builtin.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -495,15 +495,16 @@ def test_hasattr(self):
495495
self.assertRaises(TypeError, hasattr)
496496
self.assertEqual(False, hasattr(sys, chr(sys.maxunicode)))
497497

498-
# Check that hasattr allows SystemExit and KeyboardInterrupts by
498+
# Check that hasattr propagates all exceptions outside of
499+
# AttributeError.
499500
class A:
500501
def __getattr__(self, what):
501-
raise KeyboardInterrupt
502-
self.assertRaises(KeyboardInterrupt, hasattr, A(), "b")
502+
raise SystemExit
503+
self.assertRaises(SystemExit, hasattr, A(), "b")
503504
class B:
504505
def __getattr__(self, what):
505-
raise SystemExit
506-
self.assertRaises(SystemExit, hasattr, B(), "b")
506+
raise ValueError
507+
self.assertRaises(ValueError, hasattr, B(), "b")
507508

508509
def test_hash(self):
509510
hash(None)

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,7 @@ Steven Scott
730730
Barry Scott
731731
Nick Seidenman
732732
Žiga Seilnach
733+
Yury Selivanov
733734
Fred Sells
734735
Jiwon Seo
735736
Roger D. Serwy

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 3.2 Alpha 2?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #9666: Only catch AttributeError in hasattr(). All other exceptions that
16+
occur during attribute lookup are now propagated to the caller.
17+
1518
- Issue #8622: Add PYTHONFSENCODING environment variable to override the
1619
filesystem encoding.
1720

Python/bltinmodule.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -893,24 +893,21 @@ builtin_hasattr(PyObject *self, PyObject *args)
893893
}
894894
v = PyObject_GetAttr(v, name);
895895
if (v == NULL) {
896-
if (!PyErr_ExceptionMatches(PyExc_Exception))
897-
return NULL;
898-
else {
896+
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
899897
PyErr_Clear();
900-
Py_INCREF(Py_False);
901-
return Py_False;
898+
Py_RETURN_FALSE;
902899
}
900+
return NULL;
903901
}
904902
Py_DECREF(v);
905-
Py_INCREF(Py_True);
906-
return Py_True;
903+
Py_RETURN_TRUE;
907904
}
908905

909906
PyDoc_STRVAR(hasattr_doc,
910907
"hasattr(object, name) -> bool\n\
911908
\n\
912909
Return whether the object has an attribute with the given name.\n\
913-
(This is done by calling getattr(object, name) and catching exceptions.)");
910+
(This is done by calling getattr(object, name) and catching AttributeError.)");
914911

915912

916913
static PyObject *

0 commit comments

Comments
 (0)