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

Skip to content

Commit 69c88f7

Browse files
committed
Merged revisions 65320 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r65320 | amaury.forgeotdarc | 2008-07-30 19:42:16 -0500 (Wed, 30 Jul 2008) | 3 lines #2542: now that issubclass() may call arbitrary code, make sure that PyErr_ExceptionMatches returns 0 when an exception occurs there. ........
1 parent ebaf75d commit 69c88f7

2 files changed

Lines changed: 45 additions & 5 deletions

File tree

Lib/test/test_exceptions.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,20 @@
66
import pickle
77
import weakref
88

9-
from test.support import TESTFN, unlink, run_unittest
9+
from test.support import TESTFN, unlink, run_unittest, captured_output
1010

1111
# XXX This is not really enough, each *operation* should be tested!
1212

1313
class ExceptionTests(unittest.TestCase):
1414

15+
def test00(self):
16+
try:
17+
sys.exit(ValueError('aaa'))
18+
except SystemExit:
19+
pass
20+
finally:
21+
pass
22+
1523
def raise_catch(self, exc, excname):
1624
try:
1725
raise exc("spam")
@@ -404,7 +412,6 @@ def testUnicodeStrUsage(self):
404412
def testExceptionCleanupNames(self):
405413
# Make sure the local variable bound to the exception instance by
406414
# an "except" statement is only visible inside the except block.
407-
408415
try:
409416
raise Exception()
410417
except Exception as e:
@@ -565,6 +572,30 @@ def __del__(self):
565572
pass
566573
self.assertEquals(e, (None, None, None))
567574

575+
def test_badisinstance(self):
576+
# Bug #2542: if issubclass(e, MyException) raises an exception,
577+
# it should be ignored
578+
class Meta(type):
579+
def __subclasscheck__(cls, subclass):
580+
raise ValueError()
581+
class MyException(Exception, metaclass=Meta):
582+
pass
583+
584+
with captured_output("stderr") as stderr:
585+
try:
586+
raise KeyError()
587+
except MyException as e:
588+
self.fail("exception should not be a MyException")
589+
except KeyError:
590+
pass
591+
except:
592+
self.fail("Should have raised TypeError")
593+
else:
594+
self.fail("Should have raised TypeError")
595+
self.assertEqual(stderr.getvalue(),
596+
"Exception ValueError: ValueError() "
597+
"in <class 'KeyError'> ignored\n")
598+
568599
def test_main():
569600
run_unittest(ExceptionTests)
570601

Python/errors.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,18 @@ PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
157157
err = PyExceptionInstance_Class(err);
158158

159159
if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
160-
/* problems here!? not sure PyObject_IsSubclass expects to
161-
be called with an exception pending... */
162-
return PyObject_IsSubclass(err, exc);
160+
int res = 0;
161+
PyObject *exception, *value, *tb;
162+
PyErr_Fetch(&exception, &value, &tb);
163+
res = PyObject_IsSubclass(err, exc);
164+
/* This function must not fail, so print the error here */
165+
if (res == -1) {
166+
PyErr_WriteUnraisable(err);
167+
/* issubclass did not succeed */
168+
res = 0;
169+
}
170+
PyErr_Restore(exception, value, tb);
171+
return res;
163172
}
164173

165174
return err == exc;

0 commit comments

Comments
 (0)