summaryrefslogtreecommitdiff
path: root/Lib
diff options
context:
space:
mode:
authorBrett Cannon <bcannon@gmail.com>2007-02-23 14:28:25 +0000
committerBrett Cannon <bcannon@gmail.com>2007-02-23 14:28:25 +0000
commit6fbb96e69a047312a425caa7021dce31669b4076 (patch)
tree6e20579ee115663f80ccc80329c8f280aede6475 /Lib
parent764cf7ed82a5b7911e227353c5259bb908aeb7ef (diff)
downloadcpython-git-6fbb96e69a047312a425caa7021dce31669b4076.tar.gz
Refactor PEP 352 tests to make it easier in the future to make sure certain
things cannot be raised or caught.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_pep352.py52
1 files changed, 34 insertions, 18 deletions
diff --git a/Lib/test/test_pep352.py b/Lib/test/test_pep352.py
index 4ce25d68ee..abed627baa 100644
--- a/Lib/test/test_pep352.py
+++ b/Lib/test/test_pep352.py
@@ -113,6 +113,37 @@ class UsageTests(unittest.TestCase):
"""Test usage of exceptions"""
+ def raise_fails(self, object_):
+ """Make sure that raising 'object_' triggers a TypeError."""
+ try:
+ raise object_
+ except TypeError:
+ return # What is expected.
+ self.fail("TypeError expected for raising %s" % type(object_))
+
+ def catch_fails(self, object_):
+ """Catching 'object_' should raise a TypeError."""
+ try:
+ try:
+ raise StandardError
+ except object_:
+ pass
+ except TypeError:
+ pass
+ except StandardError:
+ self.fail("TypeError expected when catching %s" % type(object_))
+
+ try:
+ try:
+ raise StandardError
+ except (object_,):
+ pass
+ except TypeError:
+ return
+ except StandardError:
+ self.fail("TypeError expected when catching %s as specified in a "
+ "tuple" % type(object_))
+
def test_raise_classic(self):
# Raising a classic class is okay (for now).
class ClassicClass:
@@ -137,27 +168,12 @@ class UsageTests(unittest.TestCase):
# inherit from it.
class NewStyleClass(object):
pass
- try:
- raise NewStyleClass
- except TypeError:
- pass
- except:
- self.fail("able to raise new-style class")
- try:
- raise NewStyleClass()
- except TypeError:
- pass
- except:
- self.fail("able to raise new-style class instance")
+ self.raise_fails(NewStyleClass)
+ self.raise_fails(NewStyleClass())
def test_raise_string(self):
# Raising a string raises TypeError.
- try:
- raise "spam"
- except TypeError:
- pass
- except:
- self.fail("was able to raise a string exception")
+ self.raise_fails("spam")
def test_catch_string(self):
# Catching a string should trigger a DeprecationWarning.