| 1 |
""" |
| 2 |
Test cases for codeop.py |
| 3 |
Nick Mathewson |
| 4 |
""" |
| 5 |
import unittest |
| 6 |
import warnings |
| 7 |
from test.support import warnings_helper |
| 8 |
from textwrap import dedent |
| 9 |
|
| 10 |
from codeop import compile_command, PyCF_DONT_IMPLY_DEDENT |
| 11 |
|
| 12 |
class CodeopTests(unittest.TestCase): |
| 13 |
|
| 14 |
def assertValid(self, str, symbol='single'): |
| 15 |
'''succeed iff str is a valid piece of code''' |
| 16 |
expected = compile(str, "<input>", symbol, PyCF_DONT_IMPLY_DEDENT) |
| 17 |
self.assertEqual(compile_command(str, "<input>", symbol), expected) |
| 18 |
|
| 19 |
def assertIncomplete(self, str, symbol='single'): |
| 20 |
'''succeed iff str is the start of a valid piece of code''' |
| 21 |
self.assertEqual(compile_command(str, symbol=symbol), None) |
| 22 |
|
| 23 |
def assertInvalid(self, str, symbol='single', is_syntax=1): |
| 24 |
'''succeed iff str is the start of an invalid piece of code''' |
| 25 |
try: |
| 26 |
compile_command(str,symbol=symbol) |
| 27 |
self.fail("No exception raised for invalid code") |
| 28 |
except SyntaxError: |
| 29 |
self.assertTrue(is_syntax) |
| 30 |
except OverflowError: |
| 31 |
self.assertTrue(not is_syntax) |
| 32 |
|
| 33 |
def test_valid(self): |
| 34 |
av = self.assertValid |
| 35 |
|
| 36 |
# special case |
| 37 |
self.assertEqual(compile_command(""), |
| 38 |
compile("pass", "<input>", 'single', |
| 39 |
PyCF_DONT_IMPLY_DEDENT)) |
| 40 |
self.assertEqual(compile_command("\n"), |
| 41 |
compile("pass", "<input>", 'single', |
| 42 |
PyCF_DONT_IMPLY_DEDENT)) |
| 43 |
|
| 44 |
av("a = 1") |
| 45 |
av("\na = 1") |
| 46 |
av("a = 1\n") |
| 47 |
av("a = 1\n\n") |
| 48 |
av("\n\na = 1\n\n") |
| 49 |
|
| 50 |
av("def x():\n pass\n") |
| 51 |
av("if 1:\n pass\n") |
| 52 |
|
| 53 |
av("\n\nif 1: pass\n") |
| 54 |
av("\n\nif 1: pass\n\n") |
| 55 |
|
| 56 |
av("def x():\n\n pass\n") |
| 57 |
av("def x():\n pass\n \n") |
| 58 |
av("def x():\n pass\n \n") |
| 59 |
|
| 60 |
av("pass\n") |
| 61 |
av("3**3\n") |
| 62 |
|
| 63 |
av("if 9==3:\n pass\nelse:\n pass\n") |
| 64 |
av("if 1:\n pass\n if 1:\n pass\n else:\n pass\n") |
| 65 |
|
| 66 |
av("#a\n#b\na = 3\n") |
| 67 |
av("#a\n\n \na=3\n") |
| 68 |
av("a=3\n\n") |
| 69 |
av("a = 9+ \\\n3") |
| 70 |
|
| 71 |
av("3**3","eval") |
| 72 |
av("(lambda z: \n z**3)","eval") |
| 73 |
|
| 74 |
av("9+ \\\n3","eval") |
| 75 |
av("9+ \\\n3\n","eval") |
| 76 |
|
| 77 |
av("\n\na**3","eval") |
| 78 |
av("\n \na**3","eval") |
| 79 |
av("#a\n#b\na**3","eval") |
| 80 |
|
| 81 |
av("\n\na = 1\n\n") |
| 82 |
av("\n\nif 1: a=1\n\n") |
| 83 |
|
| 84 |
av("if 1:\n pass\n if 1:\n pass\n else:\n pass\n") |
| 85 |
av("#a\n\n \na=3\n\n") |
| 86 |
|
| 87 |
av("\n\na**3","eval") |
| 88 |
av("\n \na**3","eval") |
| 89 |
av("#a\n#b\na**3","eval") |
| 90 |
|
| 91 |
av("def f():\n try: pass\n finally: [x for x in (1,2)]\n") |
| 92 |
av("def f():\n pass\n#foo\n") |
| 93 |
av("@a.b.c\ndef f():\n pass\n") |
| 94 |
|
| 95 |
def test_incomplete(self): |
| 96 |
ai = self.assertIncomplete |
| 97 |
|
| 98 |
ai("(a **") |
| 99 |
ai("(a,b,") |
| 100 |
ai("(a,b,(") |
| 101 |
ai("(a,b,(") |
| 102 |
ai("a = (") |
| 103 |
ai("a = {") |
| 104 |
ai("b + {") |
| 105 |
|
| 106 |
ai("print([1,\n2,") |
| 107 |
ai("print({1:1,\n2:3,") |
| 108 |
ai("print((1,\n2,") |
| 109 |
|
| 110 |
ai("if 9==3:\n pass\nelse:") |
| 111 |
ai("if 9==3:\n pass\nelse:\n") |
| 112 |
ai("if 9==3:\n pass\nelse:\n pass") |
| 113 |
ai("if 1:") |
| 114 |
ai("if 1:\n") |
| 115 |
ai("if 1:\n pass\n if 1:\n pass\n else:") |
| 116 |
ai("if 1:\n pass\n if 1:\n pass\n else:\n") |
| 117 |
ai("if 1:\n pass\n if 1:\n pass\n else:\n pass") |
| 118 |
|
| 119 |
ai("def x():") |
| 120 |
ai("def x():\n") |
| 121 |
ai("def x():\n\n") |
| 122 |
|
| 123 |
ai("def x():\n pass") |
| 124 |
ai("def x():\n pass\n ") |
| 125 |
ai("def x():\n pass\n ") |
| 126 |
ai("\n\ndef x():\n pass") |
| 127 |
|
| 128 |
ai("a = 9+ \\") |
| 129 |
ai("a = 'a\\") |
| 130 |
ai("a = '''xy") |
| 131 |
|
| 132 |
ai("","eval") |
| 133 |
ai("\n","eval") |
| 134 |
ai("(","eval") |
| 135 |
ai("(9+","eval") |
| 136 |
ai("9+ \\","eval") |
| 137 |
ai("lambda z: \\","eval") |
| 138 |
|
| 139 |
ai("if True:\n if True:\n if True: \n") |
| 140 |
|
| 141 |
ai("@a(") |
| 142 |
ai("@a(b") |
| 143 |
ai("@a(b,") |
| 144 |
ai("@a(b,c") |
| 145 |
ai("@a(b,c,") |
| 146 |
|
| 147 |
ai("from a import (") |
| 148 |
ai("from a import (b") |
| 149 |
ai("from a import (b,") |
| 150 |
ai("from a import (b,c") |
| 151 |
ai("from a import (b,c,") |
| 152 |
|
| 153 |
ai("[") |
| 154 |
ai("[a") |
| 155 |
ai("[a,") |
| 156 |
ai("[a,b") |
| 157 |
ai("[a,b,") |
| 158 |
|
| 159 |
ai("{") |
| 160 |
ai("{a") |
| 161 |
ai("{a:") |
| 162 |
ai("{a:b") |
| 163 |
ai("{a:b,") |
| 164 |
ai("{a:b,c") |
| 165 |
ai("{a:b,c:") |
| 166 |
ai("{a:b,c:d") |
| 167 |
ai("{a:b,c:d,") |
| 168 |
|
| 169 |
ai("a(") |
| 170 |
ai("a(b") |
| 171 |
ai("a(b,") |
| 172 |
ai("a(b,c") |
| 173 |
ai("a(b,c,") |
| 174 |
|
| 175 |
ai("a[") |
| 176 |
ai("a[b") |
| 177 |
ai("a[b,") |
| 178 |
ai("a[b:") |
| 179 |
ai("a[b:c") |
| 180 |
ai("a[b:c:") |
| 181 |
ai("a[b:c:d") |
| 182 |
|
| 183 |
ai("def a(") |
| 184 |
ai("def a(b") |
| 185 |
ai("def a(b,") |
| 186 |
ai("def a(b,c") |
| 187 |
ai("def a(b,c,") |
| 188 |
|
| 189 |
ai("(") |
| 190 |
ai("(a") |
| 191 |
ai("(a,") |
| 192 |
ai("(a,b") |
| 193 |
ai("(a,b,") |
| 194 |
|
| 195 |
ai("if a:\n pass\nelif b:") |
| 196 |
ai("if a:\n pass\nelif b:\n pass\nelse:") |
| 197 |
|
| 198 |
ai("while a:") |
| 199 |
ai("while a:\n pass\nelse:") |
| 200 |
|
| 201 |
ai("for a in b:") |
| 202 |
ai("for a in b:\n pass\nelse:") |
| 203 |
|
| 204 |
ai("try:") |
| 205 |
ai("try:\n pass\nexcept:") |
| 206 |
ai("try:\n pass\nfinally:") |
| 207 |
ai("try:\n pass\nexcept:\n pass\nfinally:") |
| 208 |
|
| 209 |
ai("with a:") |
| 210 |
ai("with a as b:") |
| 211 |
|
| 212 |
ai("class a:") |
| 213 |
ai("class a(") |
| 214 |
ai("class a(b") |
| 215 |
ai("class a(b,") |
| 216 |
ai("class a():") |
| 217 |
|
| 218 |
ai("[x for") |
| 219 |
ai("[x for x in") |
| 220 |
ai("[x for x in (") |
| 221 |
|
| 222 |
ai("(x for") |
| 223 |
ai("(x for x in") |
| 224 |
ai("(x for x in (") |
| 225 |
|
| 226 |
ai('a = f"""') |
| 227 |
ai('a = \\') |
| 228 |
|
| 229 |
def test_invalid(self): |
| 230 |
ai = self.assertInvalid |
| 231 |
ai("a b") |
| 232 |
|
| 233 |
ai("a @") |
| 234 |
ai("a b @") |
| 235 |
ai("a ** @") |
| 236 |
|
| 237 |
ai("a = ") |
| 238 |
ai("a = 9 +") |
| 239 |
|
| 240 |
ai("def x():\n\npass\n") |
| 241 |
|
| 242 |
ai("\n\n if 1: pass\n\npass") |
| 243 |
|
| 244 |
ai("a = 9+ \\\n") |
| 245 |
ai("a = 'a\\ ") |
| 246 |
ai("a = 'a\\\n") |
| 247 |
|
| 248 |
ai("a = 1","eval") |
| 249 |
ai("]","eval") |
| 250 |
ai("())","eval") |
| 251 |
ai("[}","eval") |
| 252 |
ai("9+","eval") |
| 253 |
ai("lambda z:","eval") |
| 254 |
ai("a b","eval") |
| 255 |
|
| 256 |
ai("return 2.3") |
| 257 |
ai("if (a == 1 and b = 2): pass") |
| 258 |
|
| 259 |
ai("del 1") |
| 260 |
ai("del (1,)") |
| 261 |
ai("del [1]") |
| 262 |
ai("del '1'") |
| 263 |
|
| 264 |
ai("[i for i in range(10)] = (1, 2, 3)") |
| 265 |
|
| 266 |
def test_invalid_exec(self): |
| 267 |
ai = self.assertInvalid |
| 268 |
ai("raise = 4", symbol="exec") |
| 269 |
ai('def a-b', symbol='exec') |
| 270 |
ai('await?', symbol='exec') |
| 271 |
ai('=!=', symbol='exec') |
| 272 |
ai('a await raise b', symbol='exec') |
| 273 |
ai('a await raise b?+1', symbol='exec') |
| 274 |
|
| 275 |
def test_filename(self): |
| 276 |
self.assertEqual(compile_command("a = 1\n", "abc").co_filename, |
| 277 |
compile("a = 1\n", "abc", 'single').co_filename) |
| 278 |
self.assertNotEqual(compile_command("a = 1\n", "abc").co_filename, |
| 279 |
compile("a = 1\n", "def", 'single').co_filename) |
| 280 |
|
| 281 |
def test_warning(self): |
| 282 |
# Test that the warning is only returned once. |
| 283 |
with warnings_helper.check_warnings( |
| 284 |
('"is" with \'str\' literal', SyntaxWarning), |
| 285 |
('"\\\\e" is an invalid escape sequence', SyntaxWarning), |
| 286 |
) as w: |
| 287 |
compile_command(r"'\e' is 0") |
| 288 |
self.assertEqual(len(w.warnings), 2) |
| 289 |
|
| 290 |
# bpo-41520: check SyntaxWarning treated as an SyntaxError |
| 291 |
with warnings.catch_warnings(), self.assertRaises(SyntaxError): |
| 292 |
warnings.simplefilter('error', SyntaxWarning) |
| 293 |
compile_command('1 is 1', symbol='exec') |
| 294 |
|
| 295 |
# Check SyntaxWarning treated as an SyntaxError |
| 296 |
with warnings.catch_warnings(), self.assertRaises(SyntaxError): |
| 297 |
warnings.simplefilter('error', SyntaxWarning) |
| 298 |
compile_command(r"'\e'", symbol='exec') |
| 299 |
|
| 300 |
def test_incomplete_warning(self): |
| 301 |
with warnings.catch_warnings(record=True) as w: |
| 302 |
warnings.simplefilter('always') |
| 303 |
self.assertIncomplete("'\\e' + (") |
| 304 |
self.assertEqual(w, []) |
| 305 |
|
| 306 |
def test_invalid_warning(self): |
| 307 |
with warnings.catch_warnings(record=True) as w: |
| 308 |
warnings.simplefilter('always') |
| 309 |
self.assertInvalid("'\\e' 1") |
| 310 |
self.assertEqual(len(w), 1) |
| 311 |
self.assertEqual(w[0].category, SyntaxWarning) |
| 312 |
self.assertRegex(str(w[0].message), 'invalid escape sequence') |
| 313 |
self.assertEqual(w[0].filename, '<input>') |
| 314 |
|
| 315 |
def assertSyntaxErrorMatches(self, code, message): |
| 316 |
with self.subTest(code): |
| 317 |
with self.assertRaisesRegex(SyntaxError, message): |
| 318 |
compile_command(code, symbol='exec') |
| 319 |
|
| 320 |
def test_syntax_errors(self): |
| 321 |
self.assertSyntaxErrorMatches( |
| 322 |
dedent("""\ |
| 323 |
def foo(x,x): |
| 324 |
pass |
| 325 |
"""), "duplicate argument 'x' in function definition") |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
if __name__ == "__main__": |
| 330 |
unittest.main() |