mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
#2542: now that issubclass() may call arbitrary code,
make sure that PyErr_ExceptionMatches returns 0 when an exception occurs there.
This commit is contained in:
parent
b8827c00b8
commit
246daedd11
3 changed files with 49 additions and 4 deletions
|
@ -6,13 +6,21 @@ import unittest
|
||||||
import pickle, cPickle
|
import pickle, cPickle
|
||||||
|
|
||||||
from test.test_support import (TESTFN, unlink, run_unittest,
|
from test.test_support import (TESTFN, unlink, run_unittest,
|
||||||
catch_warning)
|
catch_warning, captured_output)
|
||||||
from test.test_pep352 import ignore_message_warning
|
from test.test_pep352 import ignore_message_warning
|
||||||
|
|
||||||
# XXX This is not really enough, each *operation* should be tested!
|
# XXX This is not really enough, each *operation* should be tested!
|
||||||
|
|
||||||
class ExceptionTests(unittest.TestCase):
|
class ExceptionTests(unittest.TestCase):
|
||||||
|
|
||||||
|
def test00(self):
|
||||||
|
try:
|
||||||
|
sys.exit(ValueError('aaa'))
|
||||||
|
except SystemExit:
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
pass
|
||||||
|
|
||||||
def testReload(self):
|
def testReload(self):
|
||||||
# Reloading the built-in exceptions module failed prior to Py2.2, while it
|
# Reloading the built-in exceptions module failed prior to Py2.2, while it
|
||||||
# should act the same as reloading built-in sys.
|
# should act the same as reloading built-in sys.
|
||||||
|
@ -344,6 +352,31 @@ class ExceptionTests(unittest.TestCase):
|
||||||
self.failUnless(unicode(Exception(u'a')))
|
self.failUnless(unicode(Exception(u'a')))
|
||||||
self.failUnless(unicode(Exception(u'\xe1')))
|
self.failUnless(unicode(Exception(u'\xe1')))
|
||||||
|
|
||||||
|
def test_badisinstance(self):
|
||||||
|
# Bug #2542: if issubclass(e, MyException) raises an exception,
|
||||||
|
# it should be ignored
|
||||||
|
class Meta(type):
|
||||||
|
def __subclasscheck__(cls, subclass):
|
||||||
|
raise ValueError()
|
||||||
|
|
||||||
|
class MyException(Exception):
|
||||||
|
__metaclass__ = Meta
|
||||||
|
pass
|
||||||
|
|
||||||
|
with captured_output("stderr") as stderr:
|
||||||
|
try:
|
||||||
|
raise KeyError()
|
||||||
|
except MyException, e:
|
||||||
|
self.fail("exception should not be a MyException")
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
except:
|
||||||
|
self.fail("Should have raised TypeError")
|
||||||
|
else:
|
||||||
|
self.fail("Should have raised TypeError")
|
||||||
|
self.assertEqual(stderr.getvalue(),
|
||||||
|
"Exception ValueError: ValueError() in "
|
||||||
|
"<type 'exceptions.KeyError'> ignored\n")
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
run_unittest(ExceptionTests)
|
run_unittest(ExceptionTests)
|
||||||
|
|
|
@ -12,6 +12,9 @@ What's New in Python 2.6 beta 3?
|
||||||
Core and Builtins
|
Core and Builtins
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
- Issue #2542: Now that issubclass() may call arbitrary code, ensure that
|
||||||
|
PyErr_ExceptionMatches returns 0 when an exception occurs there.
|
||||||
|
|
||||||
- Issue #1819: function calls with several named parameters are now on
|
- Issue #1819: function calls with several named parameters are now on
|
||||||
average 35% faster (as measured by pybench).
|
average 35% faster (as measured by pybench).
|
||||||
|
|
||||||
|
|
|
@ -106,9 +106,18 @@ PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
|
||||||
err = PyExceptionInstance_Class(err);
|
err = PyExceptionInstance_Class(err);
|
||||||
|
|
||||||
if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
|
if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
|
||||||
/* problems here!? not sure PyObject_IsSubclass expects to
|
int res = 0;
|
||||||
be called with an exception pending... */
|
PyObject *exception, *value, *tb;
|
||||||
return PyObject_IsSubclass(err, exc);
|
PyErr_Fetch(&exception, &value, &tb);
|
||||||
|
res = PyObject_IsSubclass(err, exc);
|
||||||
|
/* This function must not fail, so print the error here */
|
||||||
|
if (res == -1) {
|
||||||
|
PyErr_WriteUnraisable(err);
|
||||||
|
/* issubclass did not succeed */
|
||||||
|
res = 0;
|
||||||
|
}
|
||||||
|
PyErr_Restore(exception, value, tb);
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
return err == exc;
|
return err == exc;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue