#1574217: only swallow AttributeErrors in isinstance, not everything.
authorR. David Murray <rdmurray@bitdance.com>
Sat, 20 Nov 2010 16:33:30 +0000 (16:33 +0000)
committerR. David Murray <rdmurray@bitdance.com>
Sat, 20 Nov 2010 16:33:30 +0000 (16:33 +0000)
Patch and tests by Brian Harring, with improvements by Ralf Schmitt.

Lib/test/test_isinstance.py
Misc/ACKS
Misc/NEWS
Objects/abstract.c

index 9482e754386eb003f5c2dec95ea56526528a14ea..50920c56560bdb395868a5e9548e434504119886 100644 (file)
@@ -81,6 +81,20 @@ class TestIsInstanceExceptions(unittest.TestCase):
 
         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)
 
 \f
 # These tests are similar to above, but tickle certain code paths in
index 3cb3a29595d6dcd5dc1feac79a279fb353a8ae15..9c003e988eba050ce3f5d24853f3f5f679fa8287 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -337,6 +337,7 @@ Barry Hantman
 Lynda Hardman
 Derek Harland
 Jason Harper
+Brian Harring
 Larry Hastings
 Shane Hathaway
 Rycharde Hawkes
index cb0e19dfb4dbfc87202a8eb86275013d7e9049ea..76bf48126e971184450638135e4cf933b5ea2d36 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.2 Beta 1?
 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
   module.
 
index 4eb33d3bc509652f83977666144b1c03ad1a7d46..d039a9c864185cf2c8f4dbfb36ecba78d15ea6f9 100644 (file)
@@ -2500,7 +2500,12 @@ recursive_isinstance(PyObject *inst, PyObject *cls)
         if (retval == 0) {
             PyObject *c = PyObject_GetAttr(inst, __class__);
             if (c == NULL) {
-                PyErr_Clear();
+                if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
+                    PyErr_Clear();
+                }
+                else {
+                    retval = -1;
+                }
             }
             else {
                 if (c != (PyObject *)(inst->ob_type) &&
@@ -2518,8 +2523,12 @@ recursive_isinstance(PyObject *inst, PyObject *cls)
             return -1;
         icls = PyObject_GetAttr(inst, __class__);
         if (icls == NULL) {
-            PyErr_Clear();
-            retval = 0;
+            if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
+                PyErr_Clear();
+            }
+            else {
+                retval = -1;
+            }
         }
         else {
             retval = abstract_issubclass(icls, cls);