| 1 | """Test comparisons of Exceptions in except clauses. |
| 2 | |
| 3 | New for PyPy - Could be incorporated into CPython regression tests. |
| 4 | """ |
| 5 | |
| 6 | class AppTestExceptionComp: |
| 7 | |
| 8 | def test_exception(self): |
| 9 | try: |
| 10 | raise TypeError("nothing") |
| 11 | except TypeError: |
| 12 | pass |
| 13 | except: |
| 14 | self.fail("Identical exceptions do not match.") |
| 15 | |
| 16 | def test_exceptionfail(self): |
| 17 | try: |
| 18 | raise TypeError("nothing") |
| 19 | except KeyError: |
| 20 | self.fail("Different exceptions match.") |
| 21 | except TypeError: |
| 22 | pass |
| 23 | except: |
| 24 | self.fail("Unanticipated value for exception raise.") |
| 25 | |
| 26 | |
| 27 | def test_called(self): |
| 28 | try: |
| 29 | raise SyntaxError("Invalid") |
| 30 | except SyntaxError: |
| 31 | pass |
| 32 | except: |
| 33 | self.fail("Instantiated exception does not match parent class.") |
| 34 | |
| 35 | def test_calledfail(self): |
| 36 | try: |
| 37 | raise SyntaxError("Invalid") |
| 38 | except ZeroDivisionError: |
| 39 | self.fail("Instantiated exception matches different parent class.") |
| 40 | except SyntaxError: |
| 41 | pass |
| 42 | except: |
| 43 | self.fail("Unanticpated value for exception raise.") |
| 44 | |
| 45 | |
| 46 | def test_userclass(self): |
| 47 | class UserExcept(Exception): |
| 48 | pass |
| 49 | try: |
| 50 | raise UserExcept("nothing") |
| 51 | except UserExcept: |
| 52 | pass |
| 53 | except: |
| 54 | self.fail("User defined class exceptions do not match.") |
| 55 | |
| 56 | def test_subclass(self): |
| 57 | try: |
| 58 | raise KeyError("key") |
| 59 | except LookupError: |
| 60 | pass |
| 61 | except: |
| 62 | self.fail("Exception does not match parent class.") |
| 63 | |
| 64 | def test_deepsubclass(self): |
| 65 | try: |
| 66 | raise FloatingPointError("1.2r") |
| 67 | except Exception: |
| 68 | pass |
| 69 | except: |
| 70 | self.fail("Exception does not match grandparent class.") |
| 71 | |
| 72 | def test_tuple(self): |
| 73 | try: |
| 74 | raise ArithmeticError("2+jack") |
| 75 | except (ZeroDivisionError, ArithmeticError): |
| 76 | pass |
| 77 | except: |
| 78 | self.fail("Exception does not match self in tuple.") |
| 79 | |
| 80 | def test_parenttuple(self): |
| 81 | try: |
| 82 | raise ZeroDivisionError("0") |
| 83 | except (Exception, SystemExit): |
| 84 | pass |
| 85 | except: |
| 86 | self.fail("Exception does not match parent in tuple.") |