summaryrefslogtreecommitdiff
path: root/Lib/test/test_exceptions.py
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2009-09-16 20:30:09 +0000
committerGeorg Brandl <georg@python.org>2009-09-16 20:30:09 +0000
commit0674d3fb5fb9adde412c15ea12b6b473e8895126 (patch)
tree6e441bb57c8f1a7c90f29e2a060ca03ed57d0b80 /Lib/test/test_exceptions.py
parent4676048b437a696b42ecd4cf2c3b050e7d5154cc (diff)
downloadcpython-git-0674d3fb5fb9adde412c15ea12b6b473e8895126.tar.gz
#6844: do not emit DeprecationWarnings on access if Exception.message has been set by the user.
This works by always setting it in __dict__, except when it's implicitly set in __init__.
Diffstat (limited to 'Lib/test/test_exceptions.py')
-rw-r--r--Lib/test/test_exceptions.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index 6cfbd09966..f547eb2070 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -303,6 +303,46 @@ class ExceptionTests(unittest.TestCase):
'pickled "%r", attribute "%s"' %
(e, checkArgName))
+
+ def testDeprecatedMessageAttribute(self):
+ # Accessing BaseException.message and relying on its value set by
+ # BaseException.__init__ triggers a deprecation warning.
+ exc = BaseException("foo")
+ with warnings.catch_warnings(record=True) as w:
+ self.assertEquals(exc.message, "foo")
+ self.assertEquals(len(w), 1)
+ self.assertEquals(w[0].category, DeprecationWarning)
+ self.assertEquals(
+ str(w[0].message),
+ "BaseException.message has been deprecated as of Python 2.6")
+
+
+ def testRegularMessageAttribute(self):
+ # Accessing BaseException.message after explicitly setting a value
+ # for it does not trigger a deprecation warning.
+ exc = BaseException("foo")
+ exc.message = "bar"
+ with warnings.catch_warnings(record=True) as w:
+ self.assertEquals(exc.message, "bar")
+ self.assertEquals(len(w), 0)
+ # Deleting the message is supported, too.
+ del exc.message
+ with self.assertRaises(AttributeError):
+ exc.message
+
+ def testPickleMessageAttribute(self):
+ # Pickling with message attribute must work, as well.
+ e = Exception("foo")
+ f = Exception("foo")
+ f.message = "bar"
+ for p in pickle, cPickle:
+ ep = p.loads(p.dumps(e))
+ with warnings.catch_warnings():
+ ignore_message_warning()
+ self.assertEqual(ep.message, "foo")
+ fp = p.loads(p.dumps(f))
+ self.assertEqual(fp.message, "bar")
+
def testSlicing(self):
# Test that you can slice an exception directly instead of requiring
# going through the 'args' attribute.