Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1 | # Python test set -- part 2, opcodes |
| 2 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | from test.support import run_unittest |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4 | import unittest |
| 5 | |
| 6 | class OpcodeTest(unittest.TestCase): |
| 7 | |
| 8 | def test_try_inside_for_loop(self): |
| 9 | n = 0 |
| 10 | for i in range(10): |
| 11 | n = n+i |
| 12 | try: 1/0 |
| 13 | except NameError: pass |
| 14 | except ZeroDivisionError: pass |
| 15 | except TypeError: pass |
| 16 | try: pass |
| 17 | except: pass |
| 18 | try: pass |
| 19 | finally: pass |
| 20 | n = n+i |
| 21 | if n != 90: |
| 22 | self.fail('try inside for') |
| 23 | |
| 24 | def test_raise_class_exceptions(self): |
| 25 | |
| 26 | class AClass(Exception): pass |
| 27 | class BClass(AClass): pass |
| 28 | class CClass(Exception): pass |
| 29 | class DClass(AClass): |
| 30 | def __init__(self, ignore): |
| 31 | pass |
| 32 | |
| 33 | try: raise AClass() |
| 34 | except: pass |
| 35 | |
| 36 | try: raise AClass() |
| 37 | except AClass: pass |
| 38 | |
| 39 | try: raise BClass() |
| 40 | except AClass: pass |
| 41 | |
| 42 | try: raise BClass() |
| 43 | except CClass: self.fail() |
| 44 | except: pass |
| 45 | |
| 46 | a = AClass() |
| 47 | b = BClass() |
| 48 | |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 49 | try: |
| 50 | raise b |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 51 | except AClass as v: |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 52 | self.assertEqual(v, b) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 53 | else: |
| 54 | self.fail("no exception") |
| 55 | |
| 56 | # not enough arguments |
| 57 | ##try: raise BClass, a |
| 58 | ##except TypeError: pass |
| 59 | ##else: self.fail("no exception") |
| 60 | |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 61 | try: raise DClass(a) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 62 | except DClass as v: |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 63 | self.assertTrue(isinstance(v, DClass)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 64 | else: |
| 65 | self.fail("no exception") |
| 66 | |
| 67 | def test_compare_function_objects(self): |
| 68 | |
| 69 | f = eval('lambda: None') |
| 70 | g = eval('lambda: None') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 71 | self.assertNotEquals(f, g) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 72 | |
| 73 | f = eval('lambda a: a') |
| 74 | g = eval('lambda a: a') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 75 | self.assertNotEquals(f, g) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 76 | |
| 77 | f = eval('lambda a=1: a') |
| 78 | g = eval('lambda a=1: a') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 79 | self.assertNotEquals(f, g) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 80 | |
| 81 | f = eval('lambda: 0') |
| 82 | g = eval('lambda: 1') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 83 | self.assertNotEquals(f, g) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 84 | |
| 85 | f = eval('lambda: None') |
| 86 | g = eval('lambda a: None') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 87 | self.assertNotEquals(f, g) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 88 | |
| 89 | f = eval('lambda a: None') |
| 90 | g = eval('lambda b: None') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 91 | self.assertNotEquals(f, g) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 92 | |
| 93 | f = eval('lambda a: None') |
| 94 | g = eval('lambda a=None: None') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 95 | self.assertNotEquals(f, g) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 96 | |
| 97 | f = eval('lambda a=0: None') |
| 98 | g = eval('lambda a=1: None') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 99 | self.assertNotEquals(f, g) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 100 | |
Benjamin Peterson | efb06b0 | 2009-02-26 18:55:48 +0000 | [diff] [blame] | 101 | def test_modulo_of_string_subclasses(self): |
| 102 | class MyString(str): |
| 103 | def __mod__(self, value): |
| 104 | return 42 |
| 105 | self.assertEqual(MyString() % 3, 42) |
| 106 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 107 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 108 | def test_main(): |
| 109 | run_unittest(OpcodeTest) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 110 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 111 | if __name__ == '__main__': |
| 112 | test_main() |