diff options
author | Brett Cannon <bcannon@gmail.com> | 2007-01-30 21:34:36 (GMT) |
---|---|---|
committer | Brett Cannon <bcannon@gmail.com> | 2007-01-30 21:34:36 (GMT) |
commit | 129bd52146c2380a4887c6bbf066113a195c60da (patch) | |
tree | 9f4a250df8167eae4bb877cb2a3e85bb83b32b0b /Lib | |
parent | a05153683c0712ff62d3bdcc33c879dd49ed7ddc (diff) | |
download | cpython-129bd52146c2380a4887c6bbf066113a195c60da.zip cpython-129bd52146c2380a4887c6bbf066113a195c60da.tar.gz cpython-129bd52146c2380a4887c6bbf066113a195c60da.tar.bz2 |
No more raising of string exceptions!
The next step of PEP 352 (for 2.6) causes raising a string exception to trigger
a TypeError. Trying to catch a string exception raises a DeprecationWarning.
References to string exceptions has been removed from the docs since they are
now just an error.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_pep352.py | 62 |
1 files changed, 39 insertions, 23 deletions
diff --git a/Lib/test/test_pep352.py b/Lib/test/test_pep352.py index b2322b0..4ce25d6 100644 --- a/Lib/test/test_pep352.py +++ b/Lib/test/test_pep352.py @@ -2,7 +2,7 @@ import unittest import __builtin__ import exceptions import warnings -from test.test_support import run_unittest +from test.test_support import run_unittest, guard_warnings_filter import os from platform import system as platform_system @@ -113,13 +113,8 @@ class UsageTests(unittest.TestCase): """Test usage of exceptions""" - def setUp(self): - self._filters = warnings.filters[:] - - def tearDown(self): - warnings.filters = self._filters[:] - def test_raise_classic(self): + # Raising a classic class is okay (for now). class ClassicClass: pass try: @@ -136,6 +131,10 @@ class UsageTests(unittest.TestCase): self.fail("unable to raise class class instance") def test_raise_new_style_non_exception(self): + # You cannot raise a new-style class that does not inherit from + # BaseException; the ability was not possible until BaseException's + # introduction so no need to support new-style objects that do not + # inherit from it. class NewStyleClass(object): pass try: @@ -143,35 +142,52 @@ class UsageTests(unittest.TestCase): except TypeError: pass except: - self.fail("unable to raise new-style class") + self.fail("able to raise new-style class") try: raise NewStyleClass() except TypeError: pass except: - self.fail("unable to raise new-style class instance") + self.fail("able to raise new-style class instance") def test_raise_string(self): - warnings.resetwarnings() - warnings.filterwarnings("error") + # Raising a string raises TypeError. try: raise "spam" - except DeprecationWarning: + except TypeError: pass except: - self.fail("raising a string did not cause a DeprecationWarning") + self.fail("was able to raise a string exception") def test_catch_string(self): - # Test will be pertinent when catching exceptions raises a - # DeprecationWarning - warnings.filterwarnings("ignore", "raising") - str_exc = "spam" - try: - raise str_exc - except str_exc: - pass - except: - self.fail("catching a string exception failed") + # Catching a string should trigger a DeprecationWarning. + with guard_warnings_filter(): + warnings.resetwarnings() + warnings.filterwarnings("error") + str_exc = "spam" + try: + 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: + 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(): run_unittest(ExceptionClassTests, UsageTests) |