Deprecate BaseException.message as per PEP 352.
diff --git a/Lib/test/test_defaultdict.py b/Lib/test/test_defaultdict.py
index 08be005..6108840 100644
--- a/Lib/test/test_defaultdict.py
+++ b/Lib/test/test_defaultdict.py
@@ -137,7 +137,7 @@
try:
d1[(1,)]
except KeyError, err:
- self.assertEqual(err.message, (1,))
+ self.assertEqual(err.args[0], (1,))
else:
self.fail("expected KeyError")
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index d9a00b9..1f7105e 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -5,7 +5,9 @@
import unittest
import pickle, cPickle
-from test.test_support import TESTFN, unlink, run_unittest
+from test.test_support import (TESTFN, unlink, run_unittest,
+ guard_warnings_filter)
+from test.test_pep352 import ignore_message_warning
# XXX This is not really enough, each *operation* should be tested!
@@ -272,32 +274,34 @@
except NameError:
pass
- for exc, args, expected in exceptionList:
- try:
- raise exc(*args)
- except BaseException, e:
- if type(e) is not exc:
- raise
- # Verify module name
- self.assertEquals(type(e).__module__, 'exceptions')
- # Verify no ref leaks in Exc_str()
- s = str(e)
- for checkArgName in expected:
- self.assertEquals(repr(getattr(e, checkArgName)),
- repr(expected[checkArgName]),
- 'exception "%s", attribute "%s"' %
- (repr(e), checkArgName))
+ with guard_warnings_filter():
+ ignore_message_warning()
+ for exc, args, expected in exceptionList:
+ try:
+ raise exc(*args)
+ except BaseException, e:
+ if type(e) is not exc:
+ raise
+ # Verify module name
+ self.assertEquals(type(e).__module__, 'exceptions')
+ # Verify no ref leaks in Exc_str()
+ s = str(e)
+ for checkArgName in expected:
+ self.assertEquals(repr(getattr(e, checkArgName)),
+ repr(expected[checkArgName]),
+ 'exception "%s", attribute "%s"' %
+ (repr(e), checkArgName))
- # test for pickling support
- for p in pickle, cPickle:
- for protocol in range(p.HIGHEST_PROTOCOL + 1):
- new = p.loads(p.dumps(e, protocol))
- for checkArgName in expected:
- got = repr(getattr(new, checkArgName))
- want = repr(expected[checkArgName])
- self.assertEquals(got, want,
- 'pickled "%r", attribute "%s' %
- (e, checkArgName))
+ # test for pickling support
+ for p in pickle, cPickle:
+ for protocol in range(p.HIGHEST_PROTOCOL + 1):
+ new = p.loads(p.dumps(e, protocol))
+ for checkArgName in expected:
+ got = repr(getattr(new, checkArgName))
+ want = repr(expected[checkArgName])
+ self.assertEquals(got, want,
+ 'pickled "%r", attribute "%s' %
+ (e, checkArgName))
def testSlicing(self):
# Test that you can slice an exception directly instead of requiring
diff --git a/Lib/test/test_pep352.py b/Lib/test/test_pep352.py
index abed627..89b2fdc 100644
--- a/Lib/test/test_pep352.py
+++ b/Lib/test/test_pep352.py
@@ -6,6 +6,13 @@
import os
from platform import system as platform_system
+def ignore_message_warning():
+ """Ignore the DeprecationWarning for BaseException.message."""
+ warnings.resetwarnings()
+ warnings.filterwarnings("ignore", "BaseException.message",
+ DeprecationWarning)
+
+
class ExceptionClassTests(unittest.TestCase):
"""Tests for anything relating to exception objects themselves (e.g.,
@@ -15,9 +22,13 @@
self.failUnless(issubclass(Exception, object))
def verify_instance_interface(self, ins):
- for attr in ("args", "message", "__str__", "__repr__", "__getitem__"):
- self.failUnless(hasattr(ins, attr), "%s missing %s attribute" %
- (ins.__class__.__name__, attr))
+ with guard_warnings_filter():
+ ignore_message_warning()
+ for attr in ("args", "message", "__str__", "__repr__",
+ "__getitem__"):
+ self.failUnless(hasattr(ins, attr),
+ "%s missing %s attribute" %
+ (ins.__class__.__name__, attr))
def test_inheritance(self):
# Make sure the inheritance hierarchy matches the documentation
@@ -84,30 +95,61 @@
# Make sure interface works properly when given a single argument
arg = "spam"
exc = Exception(arg)
- results = ([len(exc.args), 1], [exc.args[0], arg], [exc.message, arg],
- [str(exc), str(arg)], [unicode(exc), unicode(arg)],
- [repr(exc), exc.__class__.__name__ + repr(exc.args)], [exc[0], arg])
- self.interface_test_driver(results)
+ with guard_warnings_filter():
+ ignore_message_warning()
+ results = ([len(exc.args), 1], [exc.args[0], arg],
+ [exc.message, arg],
+ [str(exc), str(arg)], [unicode(exc), unicode(arg)],
+ [repr(exc), exc.__class__.__name__ + repr(exc.args)], [exc[0],
+ arg])
+ self.interface_test_driver(results)
def test_interface_multi_arg(self):
# Make sure interface correct when multiple arguments given
arg_count = 3
args = tuple(range(arg_count))
exc = Exception(*args)
- results = ([len(exc.args), arg_count], [exc.args, args],
- [exc.message, ''], [str(exc), str(args)],
- [unicode(exc), unicode(args)],
- [repr(exc), exc.__class__.__name__ + repr(exc.args)],
- [exc[-1], args[-1]])
- self.interface_test_driver(results)
+ with guard_warnings_filter():
+ ignore_message_warning()
+ results = ([len(exc.args), arg_count], [exc.args, args],
+ [exc.message, ''], [str(exc), str(args)],
+ [unicode(exc), unicode(args)],
+ [repr(exc), exc.__class__.__name__ + repr(exc.args)],
+ [exc[-1], args[-1]])
+ self.interface_test_driver(results)
def test_interface_no_arg(self):
# Make sure that with no args that interface is correct
exc = Exception()
- results = ([len(exc.args), 0], [exc.args, tuple()], [exc.message, ''],
- [str(exc), ''], [unicode(exc), u''],
- [repr(exc), exc.__class__.__name__ + '()'], [True, True])
- self.interface_test_driver(results)
+ with guard_warnings_filter():
+ ignore_message_warning()
+ results = ([len(exc.args), 0], [exc.args, tuple()],
+ [exc.message, ''],
+ [str(exc), ''], [unicode(exc), u''],
+ [repr(exc), exc.__class__.__name__ + '()'], [True, True])
+ self.interface_test_driver(results)
+
+
+ def test_message_deprecation(self):
+ # As of Python 2.6, BaseException.message is deprecated.
+ with guard_warnings_filter():
+ warnings.resetwarnings()
+ warnings.filterwarnings('error')
+
+ try:
+ BaseException().message
+ except DeprecationWarning:
+ pass
+ else:
+ self.fail("BaseException.message not deprecated")
+
+ exc = BaseException()
+ try:
+ exc.message = ''
+ except DeprecationWarning:
+ pass
+ else:
+ self.fail("BaseException.message assignment not deprecated")
class UsageTests(unittest.TestCase):