blob: ad4412196552fe066ad7087e2c3c37b902c29c7b [file] [log] [blame]
Martin v. Löwis3bd071e2001-07-30 12:30:08 +00001"""
2 Test cases for codeop.py
3 Nick Mathewson
4"""
5import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00006from test.support import run_unittest, is_jython
Martin v. Löwis3bd071e2001-07-30 12:30:08 +00007
Guido van Rossum4b499dd32003-02-13 22:07:59 +00008from codeop import compile_command, PyCF_DONT_IMPLY_DEDENT
Guido van Rossum34d19282007-08-09 01:03:29 +00009import io
Martin v. Löwis3bd071e2001-07-30 12:30:08 +000010
Samuele Pedronide9a0d32003-05-17 02:39:52 +000011if is_jython:
12 import sys
Samuele Pedronide9a0d32003-05-17 02:39:52 +000013
14 def unify_callables(d):
15 for n,v in d.items():
Guido van Rossumd59da4b2007-05-22 18:11:13 +000016 if hasattr(v, '__call__'):
17 d[n] = True
Samuele Pedronide9a0d32003-05-17 02:39:52 +000018 return d
19
Martin v. Löwis3bd071e2001-07-30 12:30:08 +000020class CodeopTests(unittest.TestCase):
21
22 def assertValid(self, str, symbol='single'):
23 '''succeed iff str is a valid piece of code'''
Samuele Pedronide9a0d32003-05-17 02:39:52 +000024 if is_jython:
25 code = compile_command(str, "<input>", symbol)
Georg Brandlab91fde2009-08-13 08:51:18 +000026 self.assertTrue(code)
Samuele Pedronide9a0d32003-05-17 02:39:52 +000027 if symbol == "single":
28 d,r = {},{}
Samuele Pedroni72c5c772003-05-17 12:51:10 +000029 saved_stdout = sys.stdout
Guido van Rossum34d19282007-08-09 01:03:29 +000030 sys.stdout = io.StringIO()
Samuele Pedronide9a0d32003-05-17 02:39:52 +000031 try:
Georg Brandl7cae87c2006-09-06 06:51:57 +000032 exec(code, d)
33 exec(compile(str,"<input>","single"), r)
Samuele Pedronide9a0d32003-05-17 02:39:52 +000034 finally:
Samuele Pedroni72c5c772003-05-17 12:51:10 +000035 sys.stdout = saved_stdout
Samuele Pedronide9a0d32003-05-17 02:39:52 +000036 elif symbol == 'eval':
37 ctx = {'a': 2}
38 d = { 'value': eval(code,ctx) }
39 r = { 'value': eval(str,ctx) }
40 self.assertEquals(unify_callables(r),unify_callables(d))
41 else:
42 expected = compile(str, "<input>", symbol, PyCF_DONT_IMPLY_DEDENT)
43 self.assertEquals( compile_command(str, "<input>", symbol), expected)
Martin v. Löwis3bd071e2001-07-30 12:30:08 +000044
45 def assertIncomplete(self, str, symbol='single'):
46 '''succeed iff str is the start of a valid piece of code'''
47 self.assertEquals( compile_command(str, symbol=symbol), None)
48
49 def assertInvalid(self, str, symbol='single', is_syntax=1):
50 '''succeed iff str is the start of an invalid piece of code'''
51 try:
52 compile_command(str,symbol=symbol)
53 self.fail("No exception thrown for invalid code")
54 except SyntaxError:
Georg Brandlab91fde2009-08-13 08:51:18 +000055 self.assertTrue(is_syntax)
Martin v. Löwis3bd071e2001-07-30 12:30:08 +000056 except OverflowError:
Georg Brandlab91fde2009-08-13 08:51:18 +000057 self.assertTrue(not is_syntax)
Martin v. Löwis3bd071e2001-07-30 12:30:08 +000058
59 def test_valid(self):
60 av = self.assertValid
Samuele Pedronide9a0d32003-05-17 02:39:52 +000061
62 # special case
63 if not is_jython:
64 self.assertEquals(compile_command(""),
65 compile("pass", "<input>", 'single',
66 PyCF_DONT_IMPLY_DEDENT))
67 self.assertEquals(compile_command("\n"),
Tim Petersf545baa2003-06-15 23:26:30 +000068 compile("pass", "<input>", 'single',
Samuele Pedronide9a0d32003-05-17 02:39:52 +000069 PyCF_DONT_IMPLY_DEDENT))
70 else:
71 av("")
72 av("\n")
Tim Petersf545baa2003-06-15 23:26:30 +000073
Samuele Pedronide9a0d32003-05-17 02:39:52 +000074 av("a = 1")
75 av("\na = 1")
Martin v. Löwis3bd071e2001-07-30 12:30:08 +000076 av("a = 1\n")
Samuele Pedronide9a0d32003-05-17 02:39:52 +000077 av("a = 1\n\n")
78 av("\n\na = 1\n\n")
79
Martin v. Löwis3bd071e2001-07-30 12:30:08 +000080 av("def x():\n pass\n")
Samuele Pedronide9a0d32003-05-17 02:39:52 +000081 av("if 1:\n pass\n")
82
83 av("\n\nif 1: pass\n")
84 av("\n\nif 1: pass\n\n")
85
86 av("def x():\n\n pass\n")
87 av("def x():\n pass\n \n")
88 av("def x():\n pass\n \n")
89
Martin v. Löwis3bd071e2001-07-30 12:30:08 +000090 av("pass\n")
91 av("3**3\n")
Samuele Pedronide9a0d32003-05-17 02:39:52 +000092
Martin v. Löwis3bd071e2001-07-30 12:30:08 +000093 av("if 9==3:\n pass\nelse:\n pass\n")
Samuele Pedronide9a0d32003-05-17 02:39:52 +000094 av("if 1:\n pass\n if 1:\n pass\n else:\n pass\n")
95
Martin v. Löwis3bd071e2001-07-30 12:30:08 +000096 av("#a\n#b\na = 3\n")
97 av("#a\n\n \na=3\n")
98 av("a=3\n\n")
Samuele Pedronide9a0d32003-05-17 02:39:52 +000099 av("a = 9+ \\\n3")
Martin v. Löwis3bd071e2001-07-30 12:30:08 +0000100
101 av("3**3","eval")
102 av("(lambda z: \n z**3)","eval")
Samuele Pedronide9a0d32003-05-17 02:39:52 +0000103
104 av("9+ \\\n3","eval")
105 av("9+ \\\n3\n","eval")
106
107 av("\n\na**3","eval")
108 av("\n \na**3","eval")
Martin v. Löwis3bd071e2001-07-30 12:30:08 +0000109 av("#a\n#b\na**3","eval")
Tim Petersab9ba272001-08-09 21:40:30 +0000110
Frank Wierzbicki8e7b0e82009-08-02 23:03:31 +0000111 av("\n\na = 1\n\n")
112 av("\n\nif 1: a=1\n\n")
113
114 av("if 1:\n pass\n if 1:\n pass\n else:\n pass\n")
115 av("#a\n\n \na=3\n\n")
116
117 av("\n\na**3","eval")
118 av("\n \na**3","eval")
119 av("#a\n#b\na**3","eval")
120
121 av("def f():\n try: pass\n finally: [x for x in (1,2)]\n")
122 av("def f():\n pass\n#foo\n")
123 av("@a.b.c\ndef f():\n pass\n")
124
Martin v. Löwis3bd071e2001-07-30 12:30:08 +0000125 def test_incomplete(self):
126 ai = self.assertIncomplete
Samuele Pedronide9a0d32003-05-17 02:39:52 +0000127
Martin v. Löwis3bd071e2001-07-30 12:30:08 +0000128 ai("(a **")
Martin v. Löwis3bd071e2001-07-30 12:30:08 +0000129 ai("(a,b,")
130 ai("(a,b,(")
131 ai("(a,b,(")
Samuele Pedronide9a0d32003-05-17 02:39:52 +0000132 ai("a = (")
133 ai("a = {")
134 ai("b + {")
135
136 ai("if 9==3:\n pass\nelse:")
Martin v. Löwis3bd071e2001-07-30 12:30:08 +0000137 ai("if 9==3:\n pass\nelse:\n")
138 ai("if 9==3:\n pass\nelse:\n pass")
Samuele Pedronide9a0d32003-05-17 02:39:52 +0000139 ai("if 1:")
140 ai("if 1:\n")
141 ai("if 1:\n pass\n if 1:\n pass\n else:")
Tim Petersf545baa2003-06-15 23:26:30 +0000142 ai("if 1:\n pass\n if 1:\n pass\n else:\n")
143 ai("if 1:\n pass\n if 1:\n pass\n else:\n pass")
144
Samuele Pedronide9a0d32003-05-17 02:39:52 +0000145 ai("def x():")
146 ai("def x():\n")
147 ai("def x():\n\n")
Tim Petersab9ba272001-08-09 21:40:30 +0000148
Samuele Pedronide9a0d32003-05-17 02:39:52 +0000149 ai("def x():\n pass")
150 ai("def x():\n pass\n ")
151 ai("def x():\n pass\n ")
152 ai("\n\ndef x():\n pass")
153
154 ai("a = 9+ \\")
155 ai("a = 'a\\")
156 ai("a = '''xy")
157
158 ai("","eval")
159 ai("\n","eval")
Martin v. Löwis3bd071e2001-07-30 12:30:08 +0000160 ai("(","eval")
161 ai("(\n\n\n","eval")
162 ai("(9+","eval")
163 ai("9+ \\","eval")
164 ai("lambda z: \\","eval")
165
Frank Wierzbicki8e7b0e82009-08-02 23:03:31 +0000166 ai("if True:\n if True:\n if True: \n")
167
168 ai("@a(")
169 ai("@a(b")
170 ai("@a(b,")
171 ai("@a(b,c")
172 ai("@a(b,c,")
173
174 ai("from a import (")
175 ai("from a import (b")
176 ai("from a import (b,")
177 ai("from a import (b,c")
178 ai("from a import (b,c,")
179
180 ai("[");
181 ai("[a");
182 ai("[a,");
183 ai("[a,b");
184 ai("[a,b,");
185
186 ai("{");
187 ai("{a");
188 ai("{a:");
189 ai("{a:b");
190 ai("{a:b,");
191 ai("{a:b,c");
192 ai("{a:b,c:");
193 ai("{a:b,c:d");
194 ai("{a:b,c:d,");
195
196 ai("a(")
197 ai("a(b")
198 ai("a(b,")
199 ai("a(b,c")
200 ai("a(b,c,")
201
202 ai("a[")
203 ai("a[b")
204 ai("a[b,")
205 ai("a[b:")
206 ai("a[b:c")
207 ai("a[b:c:")
208 ai("a[b:c:d")
209
210 ai("def a(")
211 ai("def a(b")
212 ai("def a(b,")
213 ai("def a(b,c")
214 ai("def a(b,c,")
215
216 ai("(")
217 ai("(a")
218 ai("(a,")
219 ai("(a,b")
220 ai("(a,b,")
221
222 ai("if a:\n pass\nelif b:")
223 ai("if a:\n pass\nelif b:\n pass\nelse:")
224
225 ai("while a:")
226 ai("while a:\n pass\nelse:")
227
228 ai("for a in b:")
229 ai("for a in b:\n pass\nelse:")
230
231 ai("try:")
232 ai("try:\n pass\nexcept:")
233 ai("try:\n pass\nfinally:")
234 ai("try:\n pass\nexcept:\n pass\nfinally:")
235
236 ai("with a:")
237 ai("with a as b:")
238
239 ai("class a:")
240 ai("class a(")
241 ai("class a(b")
242 ai("class a(b,")
243 ai("class a():")
244
245 ai("[x for")
246 ai("[x for x in")
247 ai("[x for x in (")
248
249 ai("(x for")
250 ai("(x for x in")
251 ai("(x for x in (")
252
Martin v. Löwis3bd071e2001-07-30 12:30:08 +0000253 def test_invalid(self):
254 ai = self.assertInvalid
255 ai("a b")
Samuele Pedronide9a0d32003-05-17 02:39:52 +0000256
257 ai("a @")
258 ai("a b @")
259 ai("a ** @")
Tim Petersf545baa2003-06-15 23:26:30 +0000260
Martin v. Löwis3bd071e2001-07-30 12:30:08 +0000261 ai("a = ")
262 ai("a = 9 +")
263
Samuele Pedronide9a0d32003-05-17 02:39:52 +0000264 ai("def x():\n\npass\n")
265
266 ai("\n\n if 1: pass\n\npass")
267
268 ai("a = 9+ \\\n")
269 ai("a = 'a\\ ")
270 ai("a = 'a\\\n")
271
Martin v. Löwis3bd071e2001-07-30 12:30:08 +0000272 ai("a = 1","eval")
273 ai("a = (","eval")
274 ai("]","eval")
275 ai("())","eval")
276 ai("[}","eval")
277 ai("9+","eval")
278 ai("lambda z:","eval")
279 ai("a b","eval")
280
Frank Wierzbicki8e7b0e82009-08-02 23:03:31 +0000281 ai("return 2.3")
282 ai("if (a == 1 and b = 2): pass")
283
284 ai("del 1")
285 ai("del ()")
286 ai("del (1,)")
287 ai("del [1]")
288 ai("del '1'")
289
290 ai("[i for i in range(10)] = (1, 2, 3)")
291
Martin v. Löwis3bd071e2001-07-30 12:30:08 +0000292 def test_filename(self):
293 self.assertEquals(compile_command("a = 1\n", "abc").co_filename,
294 compile("a = 1\n", "abc", 'single').co_filename)
295 self.assertNotEquals(compile_command("a = 1\n", "abc").co_filename,
296 compile("a = 1\n", "def", 'single').co_filename)
297
Frank Wierzbicki8e7b0e82009-08-02 23:03:31 +0000298 def test_no_universal_newlines(self):
299 code = compile_command("'\rfoo\r'", symbol='eval')
300 self.assertEqual(eval(code), '\rfoo\r')
301
Fred Drake2e2be372001-09-20 21:33:42 +0000302
303def test_main():
304 run_unittest(CodeopTests)
305
306
307if __name__ == "__main__":
308 test_main()