mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
#1574217: only swallow AttributeErrors in isinstance, not everything.
Patch and tests by Brian Harring, with improvements by Ralf Schmitt.
This commit is contained in:
parent
f149e45a4e
commit
6bb9989ae3
4 changed files with 30 additions and 3 deletions
|
@ -81,6 +81,20 @@ class TestIsInstanceExceptions(unittest.TestCase):
|
||||||
|
|
||||||
self.assertRaises(TypeError, isinstance, I(), C())
|
self.assertRaises(TypeError, isinstance, I(), C())
|
||||||
|
|
||||||
|
# check that we don't mask non AttributeErrors
|
||||||
|
# see: http://bugs.python.org/issue1574217
|
||||||
|
def test_isinstance_dont_mask_non_attribute_error(self):
|
||||||
|
class C(object):
|
||||||
|
def getclass(self):
|
||||||
|
raise RuntimeError()
|
||||||
|
__class__=property(getclass)
|
||||||
|
|
||||||
|
c=C()
|
||||||
|
self.assertRaises(RuntimeError, isinstance, c, bool)
|
||||||
|
|
||||||
|
# test another code path
|
||||||
|
class D: pass
|
||||||
|
self.assertRaises(RuntimeError, isinstance, c, D)
|
||||||
|
|
||||||
|
|
||||||
# These tests are similar to above, but tickle certain code paths in
|
# These tests are similar to above, but tickle certain code paths in
|
||||||
|
|
|
@ -337,6 +337,7 @@ Barry Hantman
|
||||||
Lynda Hardman
|
Lynda Hardman
|
||||||
Derek Harland
|
Derek Harland
|
||||||
Jason Harper
|
Jason Harper
|
||||||
|
Brian Harring
|
||||||
Larry Hastings
|
Larry Hastings
|
||||||
Shane Hathaway
|
Shane Hathaway
|
||||||
Rycharde Hawkes
|
Rycharde Hawkes
|
||||||
|
|
|
@ -10,6 +10,9 @@ What's New in Python 3.2 Beta 1?
|
||||||
Core and Builtins
|
Core and Builtins
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
- Issue #1574217: isinstance now catches only AttributeError, rather than
|
||||||
|
masking all errors.
|
||||||
|
|
||||||
- Issue #10391: Don't dereference invalid memory in error messages in the ast
|
- Issue #10391: Don't dereference invalid memory in error messages in the ast
|
||||||
module.
|
module.
|
||||||
|
|
||||||
|
|
|
@ -2500,7 +2500,12 @@ recursive_isinstance(PyObject *inst, PyObject *cls)
|
||||||
if (retval == 0) {
|
if (retval == 0) {
|
||||||
PyObject *c = PyObject_GetAttr(inst, __class__);
|
PyObject *c = PyObject_GetAttr(inst, __class__);
|
||||||
if (c == NULL) {
|
if (c == NULL) {
|
||||||
PyErr_Clear();
|
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
|
||||||
|
PyErr_Clear();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
retval = -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (c != (PyObject *)(inst->ob_type) &&
|
if (c != (PyObject *)(inst->ob_type) &&
|
||||||
|
@ -2518,8 +2523,12 @@ recursive_isinstance(PyObject *inst, PyObject *cls)
|
||||||
return -1;
|
return -1;
|
||||||
icls = PyObject_GetAttr(inst, __class__);
|
icls = PyObject_GetAttr(inst, __class__);
|
||||||
if (icls == NULL) {
|
if (icls == NULL) {
|
||||||
PyErr_Clear();
|
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
|
||||||
retval = 0;
|
PyErr_Clear();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
retval = -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
retval = abstract_issubclass(icls, cls);
|
retval = abstract_issubclass(icls, cls);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue