Martin v. Löwis | 3bd071e | 2001-07-30 12:30:08 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test cases for codeop.py |
| 3 | Nick Mathewson |
| 4 | """ |
| 5 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 6 | from test.support import run_unittest, is_jython |
Martin v. Löwis | 3bd071e | 2001-07-30 12:30:08 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 4b499dd3 | 2003-02-13 22:07:59 +0000 | [diff] [blame] | 8 | from codeop import compile_command, PyCF_DONT_IMPLY_DEDENT |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 9 | import io |
Martin v. Löwis | 3bd071e | 2001-07-30 12:30:08 +0000 | [diff] [blame] | 10 | |
Samuele Pedroni | de9a0d3 | 2003-05-17 02:39:52 +0000 | [diff] [blame] | 11 | if is_jython: |
| 12 | import sys |
Samuele Pedroni | de9a0d3 | 2003-05-17 02:39:52 +0000 | [diff] [blame] | 13 | |
| 14 | def unify_callables(d): |
| 15 | for n,v in d.items(): |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 16 | if hasattr(v, '__call__'): |
| 17 | d[n] = True |
Samuele Pedroni | de9a0d3 | 2003-05-17 02:39:52 +0000 | [diff] [blame] | 18 | return d |
| 19 | |
Martin v. Löwis | 3bd071e | 2001-07-30 12:30:08 +0000 | [diff] [blame] | 20 | class CodeopTests(unittest.TestCase): |
| 21 | |
| 22 | def assertValid(self, str, symbol='single'): |
| 23 | '''succeed iff str is a valid piece of code''' |
Samuele Pedroni | de9a0d3 | 2003-05-17 02:39:52 +0000 | [diff] [blame] | 24 | if is_jython: |
| 25 | code = compile_command(str, "<input>", symbol) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 26 | self.assertTrue(code) |
Samuele Pedroni | de9a0d3 | 2003-05-17 02:39:52 +0000 | [diff] [blame] | 27 | if symbol == "single": |
| 28 | d,r = {},{} |
Samuele Pedroni | 72c5c77 | 2003-05-17 12:51:10 +0000 | [diff] [blame] | 29 | saved_stdout = sys.stdout |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 30 | sys.stdout = io.StringIO() |
Samuele Pedroni | de9a0d3 | 2003-05-17 02:39:52 +0000 | [diff] [blame] | 31 | try: |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 32 | exec(code, d) |
| 33 | exec(compile(str,"<input>","single"), r) |
Samuele Pedroni | de9a0d3 | 2003-05-17 02:39:52 +0000 | [diff] [blame] | 34 | finally: |
Samuele Pedroni | 72c5c77 | 2003-05-17 12:51:10 +0000 | [diff] [blame] | 35 | sys.stdout = saved_stdout |
Samuele Pedroni | de9a0d3 | 2003-05-17 02:39:52 +0000 | [diff] [blame] | 36 | 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öwis | 3bd071e | 2001-07-30 12:30:08 +0000 | [diff] [blame] | 44 | |
| 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 Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 55 | self.assertTrue(is_syntax) |
Martin v. Löwis | 3bd071e | 2001-07-30 12:30:08 +0000 | [diff] [blame] | 56 | except OverflowError: |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 57 | self.assertTrue(not is_syntax) |
Martin v. Löwis | 3bd071e | 2001-07-30 12:30:08 +0000 | [diff] [blame] | 58 | |
| 59 | def test_valid(self): |
| 60 | av = self.assertValid |
Samuele Pedroni | de9a0d3 | 2003-05-17 02:39:52 +0000 | [diff] [blame] | 61 | |
| 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 Peters | f545baa | 2003-06-15 23:26:30 +0000 | [diff] [blame] | 68 | compile("pass", "<input>", 'single', |
Samuele Pedroni | de9a0d3 | 2003-05-17 02:39:52 +0000 | [diff] [blame] | 69 | PyCF_DONT_IMPLY_DEDENT)) |
| 70 | else: |
| 71 | av("") |
| 72 | av("\n") |
Tim Peters | f545baa | 2003-06-15 23:26:30 +0000 | [diff] [blame] | 73 | |
Samuele Pedroni | de9a0d3 | 2003-05-17 02:39:52 +0000 | [diff] [blame] | 74 | av("a = 1") |
| 75 | av("\na = 1") |
Martin v. Löwis | 3bd071e | 2001-07-30 12:30:08 +0000 | [diff] [blame] | 76 | av("a = 1\n") |
Samuele Pedroni | de9a0d3 | 2003-05-17 02:39:52 +0000 | [diff] [blame] | 77 | av("a = 1\n\n") |
| 78 | av("\n\na = 1\n\n") |
| 79 | |
Martin v. Löwis | 3bd071e | 2001-07-30 12:30:08 +0000 | [diff] [blame] | 80 | av("def x():\n pass\n") |
Samuele Pedroni | de9a0d3 | 2003-05-17 02:39:52 +0000 | [diff] [blame] | 81 | 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öwis | 3bd071e | 2001-07-30 12:30:08 +0000 | [diff] [blame] | 90 | av("pass\n") |
| 91 | av("3**3\n") |
Samuele Pedroni | de9a0d3 | 2003-05-17 02:39:52 +0000 | [diff] [blame] | 92 | |
Martin v. Löwis | 3bd071e | 2001-07-30 12:30:08 +0000 | [diff] [blame] | 93 | av("if 9==3:\n pass\nelse:\n pass\n") |
Samuele Pedroni | de9a0d3 | 2003-05-17 02:39:52 +0000 | [diff] [blame] | 94 | av("if 1:\n pass\n if 1:\n pass\n else:\n pass\n") |
| 95 | |
Martin v. Löwis | 3bd071e | 2001-07-30 12:30:08 +0000 | [diff] [blame] | 96 | av("#a\n#b\na = 3\n") |
| 97 | av("#a\n\n \na=3\n") |
| 98 | av("a=3\n\n") |
Samuele Pedroni | de9a0d3 | 2003-05-17 02:39:52 +0000 | [diff] [blame] | 99 | av("a = 9+ \\\n3") |
Martin v. Löwis | 3bd071e | 2001-07-30 12:30:08 +0000 | [diff] [blame] | 100 | |
| 101 | av("3**3","eval") |
| 102 | av("(lambda z: \n z**3)","eval") |
Samuele Pedroni | de9a0d3 | 2003-05-17 02:39:52 +0000 | [diff] [blame] | 103 | |
| 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öwis | 3bd071e | 2001-07-30 12:30:08 +0000 | [diff] [blame] | 109 | av("#a\n#b\na**3","eval") |
Tim Peters | ab9ba27 | 2001-08-09 21:40:30 +0000 | [diff] [blame] | 110 | |
Frank Wierzbicki | 8e7b0e8 | 2009-08-02 23:03:31 +0000 | [diff] [blame] | 111 | 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öwis | 3bd071e | 2001-07-30 12:30:08 +0000 | [diff] [blame] | 125 | def test_incomplete(self): |
| 126 | ai = self.assertIncomplete |
Samuele Pedroni | de9a0d3 | 2003-05-17 02:39:52 +0000 | [diff] [blame] | 127 | |
Martin v. Löwis | 3bd071e | 2001-07-30 12:30:08 +0000 | [diff] [blame] | 128 | ai("(a **") |
Martin v. Löwis | 3bd071e | 2001-07-30 12:30:08 +0000 | [diff] [blame] | 129 | ai("(a,b,") |
| 130 | ai("(a,b,(") |
| 131 | ai("(a,b,(") |
Samuele Pedroni | de9a0d3 | 2003-05-17 02:39:52 +0000 | [diff] [blame] | 132 | ai("a = (") |
| 133 | ai("a = {") |
| 134 | ai("b + {") |
| 135 | |
| 136 | ai("if 9==3:\n pass\nelse:") |
Martin v. Löwis | 3bd071e | 2001-07-30 12:30:08 +0000 | [diff] [blame] | 137 | ai("if 9==3:\n pass\nelse:\n") |
| 138 | ai("if 9==3:\n pass\nelse:\n pass") |
Samuele Pedroni | de9a0d3 | 2003-05-17 02:39:52 +0000 | [diff] [blame] | 139 | ai("if 1:") |
| 140 | ai("if 1:\n") |
| 141 | ai("if 1:\n pass\n if 1:\n pass\n else:") |
Tim Peters | f545baa | 2003-06-15 23:26:30 +0000 | [diff] [blame] | 142 | 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 Pedroni | de9a0d3 | 2003-05-17 02:39:52 +0000 | [diff] [blame] | 145 | ai("def x():") |
| 146 | ai("def x():\n") |
| 147 | ai("def x():\n\n") |
Tim Peters | ab9ba27 | 2001-08-09 21:40:30 +0000 | [diff] [blame] | 148 | |
Samuele Pedroni | de9a0d3 | 2003-05-17 02:39:52 +0000 | [diff] [blame] | 149 | 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öwis | 3bd071e | 2001-07-30 12:30:08 +0000 | [diff] [blame] | 160 | ai("(","eval") |
| 161 | ai("(\n\n\n","eval") |
| 162 | ai("(9+","eval") |
| 163 | ai("9+ \\","eval") |
| 164 | ai("lambda z: \\","eval") |
| 165 | |
Frank Wierzbicki | 8e7b0e8 | 2009-08-02 23:03:31 +0000 | [diff] [blame] | 166 | 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öwis | 3bd071e | 2001-07-30 12:30:08 +0000 | [diff] [blame] | 253 | def test_invalid(self): |
| 254 | ai = self.assertInvalid |
| 255 | ai("a b") |
Samuele Pedroni | de9a0d3 | 2003-05-17 02:39:52 +0000 | [diff] [blame] | 256 | |
| 257 | ai("a @") |
| 258 | ai("a b @") |
| 259 | ai("a ** @") |
Tim Peters | f545baa | 2003-06-15 23:26:30 +0000 | [diff] [blame] | 260 | |
Martin v. Löwis | 3bd071e | 2001-07-30 12:30:08 +0000 | [diff] [blame] | 261 | ai("a = ") |
| 262 | ai("a = 9 +") |
| 263 | |
Samuele Pedroni | de9a0d3 | 2003-05-17 02:39:52 +0000 | [diff] [blame] | 264 | 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öwis | 3bd071e | 2001-07-30 12:30:08 +0000 | [diff] [blame] | 272 | 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 Wierzbicki | 8e7b0e8 | 2009-08-02 23:03:31 +0000 | [diff] [blame] | 281 | 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öwis | 3bd071e | 2001-07-30 12:30:08 +0000 | [diff] [blame] | 292 | 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 Wierzbicki | 8e7b0e8 | 2009-08-02 23:03:31 +0000 | [diff] [blame] | 298 | def test_no_universal_newlines(self): |
| 299 | code = compile_command("'\rfoo\r'", symbol='eval') |
| 300 | self.assertEqual(eval(code), '\rfoo\r') |
| 301 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 302 | |
| 303 | def test_main(): |
| 304 | run_unittest(CodeopTests) |
| 305 | |
| 306 | |
| 307 | if __name__ == "__main__": |
| 308 | test_main() |