blob: e880c3f1ac875e049683f7c801c166c8b75a7a27 [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:
Inada Naoki8bbfeb32021-04-02 12:53:46 +090027 with open(ann_module.__file__, encoding="utf-8") as f:
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070028 txt = f.read()
29 co = compile(txt, ann_module.__file__, 'exec')
Mark Shannon877df852020-11-12 09:43:29 +000030 self.assertEqual(co.co_firstlineno, 1)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070031 except OSError:
32 pass
33
larryhastings2f2b6982021-04-29 20:09:08 -070034 def test_default_annotations_exist(self):
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070035 class C: pass
larryhastings2f2b6982021-04-29 20:09:08 -070036 self.assertEqual(C.__annotations__, {})
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070037
38 def test_use_existing_annotations(self):
39 ns = {'__annotations__': {1: 2}}
40 exec('x: int', ns)
Pablo Galindob0544ba2021-04-21 12:41:19 +010041 self.assertEqual(ns['__annotations__'], {'x': int, 1: 2})
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070042
43 def test_do_not_recreate_annotations(self):
Serhiy Storchaka06ca3f02018-07-21 07:44:04 +030044 # Don't rely on the existence of the '__annotations__' global.
Serhiy Storchakac206f0d2018-07-24 15:05:28 +030045 with support.swap_item(globals(), '__annotations__', {}):
46 del globals()['__annotations__']
Serhiy Storchaka06ca3f02018-07-21 07:44:04 +030047 class C:
48 del __annotations__
Serhiy Storchakac206f0d2018-07-24 15:05:28 +030049 with self.assertRaises(NameError):
50 x: int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070051
Thomas Wouters89f507f2006-12-13 04:49:30 +000052 def test_raise_class_exceptions(self):
53
54 class AClass(Exception): pass
55 class BClass(AClass): pass
56 class CClass(Exception): pass
57 class DClass(AClass):
58 def __init__(self, ignore):
59 pass
60
61 try: raise AClass()
62 except: pass
63
64 try: raise AClass()
65 except AClass: pass
66
67 try: raise BClass()
68 except AClass: pass
69
70 try: raise BClass()
71 except CClass: self.fail()
72 except: pass
73
74 a = AClass()
75 b = BClass()
76
Collin Winter828f04a2007-08-31 00:04:24 +000077 try:
78 raise b
Guido van Rossumb940e112007-01-10 16:19:56 +000079 except AClass as v:
Collin Winter828f04a2007-08-31 00:04:24 +000080 self.assertEqual(v, b)
Thomas Wouters89f507f2006-12-13 04:49:30 +000081 else:
82 self.fail("no exception")
83
84 # not enough arguments
85 ##try: raise BClass, a
86 ##except TypeError: pass
87 ##else: self.fail("no exception")
88
Collin Winter828f04a2007-08-31 00:04:24 +000089 try: raise DClass(a)
Guido van Rossumb940e112007-01-10 16:19:56 +000090 except DClass as v:
Ezio Melottie9615932010-01-24 19:26:24 +000091 self.assertIsInstance(v, DClass)
Thomas Wouters89f507f2006-12-13 04:49:30 +000092 else:
93 self.fail("no exception")
94
95 def test_compare_function_objects(self):
96
97 f = eval('lambda: None')
98 g = eval('lambda: None')
Ezio Melottib3aedd42010-11-20 19:04:17 +000099 self.assertNotEqual(f, g)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000100
101 f = eval('lambda a: a')
102 g = eval('lambda a: a')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000103 self.assertNotEqual(f, g)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000104
105 f = eval('lambda a=1: a')
106 g = eval('lambda a=1: a')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000107 self.assertNotEqual(f, g)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000108
109 f = eval('lambda: 0')
110 g = eval('lambda: 1')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000111 self.assertNotEqual(f, g)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000112
113 f = eval('lambda: None')
114 g = eval('lambda a: None')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000115 self.assertNotEqual(f, g)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000116
117 f = eval('lambda a: None')
118 g = eval('lambda b: None')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000119 self.assertNotEqual(f, g)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000120
121 f = eval('lambda a: None')
122 g = eval('lambda a=None: None')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000123 self.assertNotEqual(f, g)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000124
125 f = eval('lambda a=0: None')
126 g = eval('lambda a=1: None')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000127 self.assertNotEqual(f, g)
Guido van Rossum3bead091992-01-27 17:00:37 +0000128
Benjamin Petersonefb06b02009-02-26 18:55:48 +0000129 def test_modulo_of_string_subclasses(self):
130 class MyString(str):
131 def __mod__(self, value):
132 return 42
133 self.assertEqual(MyString() % 3, 42)
134
Guido van Rossum3bead091992-01-27 17:00:37 +0000135
Thomas Wouters89f507f2006-12-13 04:49:30 +0000136if __name__ == '__main__':
Zachary Ware38c707e2015-04-13 15:00:43 -0500137 unittest.main()