Replace catch_warnings with check_warnings when it makes sense. Use assertRaises context manager to simplify some tests.
diff --git a/Lib/test/test_pep352.py b/Lib/test/test_pep352.py
index 4654e26..88b3136 100644
--- a/Lib/test/test_pep352.py
+++ b/Lib/test/test_pep352.py
@@ -2,7 +2,7 @@
import __builtin__
import exceptions
import warnings
-from test.test_support import run_unittest
+from test.test_support import run_unittest, check_warnings
import os
import sys
from platform import system as platform_system
@@ -15,14 +15,13 @@
"catching classes that don't inherit from BaseException is not allowed",
"__get(item|slice)__ not supported for exception classes"])
+_deprecations = [(msg, DeprecationWarning) for msg in DEPRECATION_WARNINGS]
+
# Silence Py3k and other deprecation warnings
def ignore_deprecation_warnings(func):
"""Ignore the known DeprecationWarnings."""
def wrapper(*args, **kw):
- with warnings.catch_warnings():
- warnings.resetwarnings()
- for text in DEPRECATION_WARNINGS:
- warnings.filterwarnings("ignore", text, DeprecationWarning)
+ with check_warnings(*_deprecations, quiet=True):
return func(*args, **kw)
return wrapper
@@ -139,16 +138,8 @@
def test_message_deprecation(self):
# As of Python 2.6, BaseException.message is deprecated.
- with warnings.catch_warnings():
- warnings.resetwarnings()
- warnings.filterwarnings('error')
-
- try:
- BaseException().message
- except DeprecationWarning:
- pass
- else:
- self.fail("BaseException.message not deprecated")
+ with check_warnings(("", DeprecationWarning)):
+ BaseException().message
class UsageTests(unittest.TestCase):
@@ -224,28 +215,19 @@
warnings.resetwarnings()
warnings.filterwarnings("error")
str_exc = "spam"
- try:
+ with self.assertRaises(DeprecationWarning):
try:
raise StandardError
except str_exc:
pass
- except DeprecationWarning:
- pass
- except StandardError:
- self.fail("catching a string exception did not raise "
- "DeprecationWarning")
+
# Make sure that even if the string exception is listed in a tuple
# that a warning is raised.
- try:
+ with self.assertRaises(DeprecationWarning):
try:
raise StandardError
except (AssertionError, str_exc):
pass
- except DeprecationWarning:
- pass
- except StandardError:
- self.fail("catching a string exception specified in a tuple did "
- "not raise DeprecationWarning")
def test_main():