blob: 527aca664d38e853efd69d732915bd6aff64d2d6 [file] [log] [blame]
Guido van Rossum3bead091992-01-27 17:00:37 +00001# Python test set -- part 2, opcodes
2
Thomas Wouters89f507f2006-12-13 04:49:30 +00003import unittest
Serhiy Storchaka06ca3f02018-07-21 07:44:04 +03004from test import ann_module, support
Thomas Wouters89f507f2006-12-13 04:49:30 +00005
6class 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
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070024 def test_setup_annotations_line(self):
25 # check that SETUP_ANNOTATIONS does not create spurious line numbers
26 try:
27 with open(ann_module.__file__) as f:
28 txt = f.read()
29 co = compile(txt, ann_module.__file__, 'exec')
Anthony Sottile995d9b92019-01-12 20:05:13 -080030 self.assertEqual(co.co_firstlineno, 3)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070031 except OSError:
32 pass
33
34 def test_no_annotations_if_not_needed(self):
35 class C: pass
36 with self.assertRaises(AttributeError):
37 C.__annotations__
38
39 def test_use_existing_annotations(self):
40 ns = {'__annotations__': {1: 2}}
41 exec('x: int', ns)
42 self.assertEqual(ns['__annotations__'], {'x': int, 1: 2})
43
44 def test_do_not_recreate_annotations(self):
Serhiy Storchaka06ca3f02018-07-21 07:44:04 +030045 # Don't rely on the existence of the '__annotations__' global.
Serhiy Storchakac206f0d2018-07-24 15:05:28 +030046 with support.swap_item(globals(), '__annotations__', {}):
47 del globals()['__annotations__']
Serhiy Storchaka06ca3f02018-07-21 07:44:04 +030048 class C:
49 del __annotations__
Serhiy Storchakac206f0d2018-07-24 15:05:28 +030050 with self.assertRaises(NameError):
51 x: int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070052
Thomas Wouters89f507f2006-12-13 04:49:30 +000053 def test_raise_class_exceptions(self):
54
55 class AClass(Exception): pass
56 class BClass(AClass): pass
57 class CClass(Exception): pass
58 class DClass(AClass):
59 def __init__(self, ignore):
60 pass
61
62 try: raise AClass()
63 except: pass
64
65 try: raise AClass()
66 except AClass: pass
67
68 try: raise BClass()
69 except AClass: pass
70
71 try: raise BClass()
72 except CClass: self.fail()
73 except: pass
74
75 a = AClass()
76 b = BClass()
77
Collin Winter828f04a2007-08-31 00:04:24 +000078 try:
79 raise b
Guido van Rossumb940e112007-01-10 16:19:56 +000080 except AClass as v:
Collin Winter828f04a2007-08-31 00:04:24 +000081 self.assertEqual(v, b)
Thomas Wouters89f507f2006-12-13 04:49:30 +000082 else:
83 self.fail("no exception")
84
85 # not enough arguments
86 ##try: raise BClass, a
87 ##except TypeError: pass
88 ##else: self.fail("no exception")
89
Collin Winter828f04a2007-08-31 00:04:24 +000090 try: raise DClass(a)
Guido van Rossumb940e112007-01-10 16:19:56 +000091 except DClass as v:
Ezio Melottie9615932010-01-24 19:26:24 +000092 self.assertIsInstance(v, DClass)
Thomas Wouters89f507f2006-12-13 04:49:30 +000093 else:
94 self.fail("no exception")
95
96 def test_compare_function_objects(self):
97
98 f = eval('lambda: None')
99 g = eval('lambda: None')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000100 self.assertNotEqual(f, g)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000101
102 f = eval('lambda a: a')
103 g = eval('lambda a: a')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000104 self.assertNotEqual(f, g)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000105
106 f = eval('lambda a=1: a')
107 g = eval('lambda a=1: a')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000108 self.assertNotEqual(f, g)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000109
110 f = eval('lambda: 0')
111 g = eval('lambda: 1')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000112 self.assertNotEqual(f, g)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000113
114 f = eval('lambda: None')
115 g = eval('lambda a: None')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000116 self.assertNotEqual(f, g)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000117
118 f = eval('lambda a: None')
119 g = eval('lambda b: None')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000120 self.assertNotEqual(f, g)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000121
122 f = eval('lambda a: None')
123 g = eval('lambda a=None: None')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000124 self.assertNotEqual(f, g)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000125
126 f = eval('lambda a=0: None')
127 g = eval('lambda a=1: None')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000128 self.assertNotEqual(f, g)
Guido van Rossum3bead091992-01-27 17:00:37 +0000129
Benjamin Petersonefb06b02009-02-26 18:55:48 +0000130 def test_modulo_of_string_subclasses(self):
131 class MyString(str):
132 def __mod__(self, value):
133 return 42
134 self.assertEqual(MyString() % 3, 42)
135
Guido van Rossum3bead091992-01-27 17:00:37 +0000136
Thomas Wouters89f507f2006-12-13 04:49:30 +0000137if __name__ == '__main__':
Zachary Ware38c707e2015-04-13 15:00:43 -0500138 unittest.main()