benjamin.peterson
2008-07-31 01:47:09 UTC
Author: benjamin.peterson
Date: Thu Jul 31 03:47:08 2008
New Revision: 65322
Log:
Merged revisions 65320 via svnmerge from
svn+ssh://pythondev at svn.python.org/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.
........
Modified:
python/branches/py3k/ (props changed)
python/branches/py3k/Lib/test/test_exceptions.py
python/branches/py3k/Python/errors.c
Modified: python/branches/py3k/Lib/test/test_exceptions.py
==============================================================================
--- python/branches/py3k/Lib/test/test_exceptions.py (original)
+++ python/branches/py3k/Lib/test/test_exceptions.py Thu Jul 31 03:47:08 2008
@@ -6,12 +6,20 @@
import pickle
import weakref
-from test.support import TESTFN, unlink, run_unittest
+from test.support import TESTFN, unlink, run_unittest, captured_output
# XXX This is not really enough, each *operation* should be tested!
class ExceptionTests(unittest.TestCase):
+ def test00(self):
+ try:
+ sys.exit(ValueError('aaa'))
+ except SystemExit:
+ pass
+ finally:
+ pass
+
def raise_catch(self, exc, excname):
try:
raise exc("spam")
@@ -404,7 +412,6 @@
def testExceptionCleanupNames(self):
# Make sure the local variable bound to the exception instance by
# an "except" statement is only visible inside the except block.
-
try:
raise Exception()
except Exception as e:
@@ -565,6 +572,30 @@
pass
self.assertEquals(e, (None, None, None))
+ 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 as 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 <class 'KeyError'> ignored\n")
+
def test_main():
run_unittest(ExceptionTests)
Modified: python/branches/py3k/Python/errors.c
==============================================================================
--- python/branches/py3k/Python/errors.c (original)
+++ python/branches/py3k/Python/errors.c Thu Jul 31 03:47:08 2008
@@ -157,9 +157,18 @@
err = PyExceptionInstance_Class(err);
if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
- /* problems here!? not sure PyObject_IsSubclass expects to
- be called with an exception pending... */
- return PyObject_IsSubclass(err, exc);
+ int res = 0;
+ PyObject *exception, *value, *tb;
+ 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;
Date: Thu Jul 31 03:47:08 2008
New Revision: 65322
Log:
Merged revisions 65320 via svnmerge from
svn+ssh://pythondev at svn.python.org/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.
........
Modified:
python/branches/py3k/ (props changed)
python/branches/py3k/Lib/test/test_exceptions.py
python/branches/py3k/Python/errors.c
Modified: python/branches/py3k/Lib/test/test_exceptions.py
==============================================================================
--- python/branches/py3k/Lib/test/test_exceptions.py (original)
+++ python/branches/py3k/Lib/test/test_exceptions.py Thu Jul 31 03:47:08 2008
@@ -6,12 +6,20 @@
import pickle
import weakref
-from test.support import TESTFN, unlink, run_unittest
+from test.support import TESTFN, unlink, run_unittest, captured_output
# XXX This is not really enough, each *operation* should be tested!
class ExceptionTests(unittest.TestCase):
+ def test00(self):
+ try:
+ sys.exit(ValueError('aaa'))
+ except SystemExit:
+ pass
+ finally:
+ pass
+
def raise_catch(self, exc, excname):
try:
raise exc("spam")
@@ -404,7 +412,6 @@
def testExceptionCleanupNames(self):
# Make sure the local variable bound to the exception instance by
# an "except" statement is only visible inside the except block.
-
try:
raise Exception()
except Exception as e:
@@ -565,6 +572,30 @@
pass
self.assertEquals(e, (None, None, None))
+ 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 as 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 <class 'KeyError'> ignored\n")
+
def test_main():
run_unittest(ExceptionTests)
Modified: python/branches/py3k/Python/errors.c
==============================================================================
--- python/branches/py3k/Python/errors.c (original)
+++ python/branches/py3k/Python/errors.c Thu Jul 31 03:47:08 2008
@@ -157,9 +157,18 @@
err = PyExceptionInstance_Class(err);
if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
- /* problems here!? not sure PyObject_IsSubclass expects to
- be called with an exception pending... */
- return PyObject_IsSubclass(err, exc);
+ int res = 0;
+ PyObject *exception, *value, *tb;
+ 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;