| 1 | n/a | import parser |
|---|
| 2 | n/a | import unittest |
|---|
| 3 | n/a | import operator |
|---|
| 4 | n/a | import struct |
|---|
| 5 | n/a | from test import support |
|---|
| 6 | n/a | from test.support.script_helper import assert_python_failure |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | # |
|---|
| 9 | n/a | # First, we test that we can generate trees from valid source fragments, |
|---|
| 10 | n/a | # and that these valid trees are indeed allowed by the tree-loading side |
|---|
| 11 | n/a | # of the parser module. |
|---|
| 12 | n/a | # |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | class RoundtripLegalSyntaxTestCase(unittest.TestCase): |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | def roundtrip(self, f, s): |
|---|
| 17 | n/a | st1 = f(s) |
|---|
| 18 | n/a | t = st1.totuple() |
|---|
| 19 | n/a | try: |
|---|
| 20 | n/a | st2 = parser.sequence2st(t) |
|---|
| 21 | n/a | except parser.ParserError as why: |
|---|
| 22 | n/a | self.fail("could not roundtrip %r: %s" % (s, why)) |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | self.assertEqual(t, st2.totuple(), |
|---|
| 25 | n/a | "could not re-generate syntax tree") |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | def check_expr(self, s): |
|---|
| 28 | n/a | self.roundtrip(parser.expr, s) |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | def test_flags_passed(self): |
|---|
| 31 | n/a | # The unicode literals flags has to be passed from the paser to AST |
|---|
| 32 | n/a | # generation. |
|---|
| 33 | n/a | suite = parser.suite("from __future__ import unicode_literals; x = ''") |
|---|
| 34 | n/a | code = suite.compile() |
|---|
| 35 | n/a | scope = {} |
|---|
| 36 | n/a | exec(code, {}, scope) |
|---|
| 37 | n/a | self.assertIsInstance(scope["x"], str) |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | def check_suite(self, s): |
|---|
| 40 | n/a | self.roundtrip(parser.suite, s) |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | def test_yield_statement(self): |
|---|
| 43 | n/a | self.check_suite("def f(): yield 1") |
|---|
| 44 | n/a | self.check_suite("def f(): yield") |
|---|
| 45 | n/a | self.check_suite("def f(): x += yield") |
|---|
| 46 | n/a | self.check_suite("def f(): x = yield 1") |
|---|
| 47 | n/a | self.check_suite("def f(): x = y = yield 1") |
|---|
| 48 | n/a | self.check_suite("def f(): x = yield") |
|---|
| 49 | n/a | self.check_suite("def f(): x = y = yield") |
|---|
| 50 | n/a | self.check_suite("def f(): 1 + (yield)*2") |
|---|
| 51 | n/a | self.check_suite("def f(): (yield 1)*2") |
|---|
| 52 | n/a | self.check_suite("def f(): return; yield 1") |
|---|
| 53 | n/a | self.check_suite("def f(): yield 1; return") |
|---|
| 54 | n/a | self.check_suite("def f(): yield from 1") |
|---|
| 55 | n/a | self.check_suite("def f(): x = yield from 1") |
|---|
| 56 | n/a | self.check_suite("def f(): f((yield from 1))") |
|---|
| 57 | n/a | self.check_suite("def f(): yield 1; return 1") |
|---|
| 58 | n/a | self.check_suite("def f():\n" |
|---|
| 59 | n/a | " for x in range(30):\n" |
|---|
| 60 | n/a | " yield x\n") |
|---|
| 61 | n/a | self.check_suite("def f():\n" |
|---|
| 62 | n/a | " if (yield):\n" |
|---|
| 63 | n/a | " yield x\n") |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | def test_await_statement(self): |
|---|
| 66 | n/a | self.check_suite("async def f():\n await smth()") |
|---|
| 67 | n/a | self.check_suite("async def f():\n foo = await smth()") |
|---|
| 68 | n/a | self.check_suite("async def f():\n foo, bar = await smth()") |
|---|
| 69 | n/a | self.check_suite("async def f():\n (await smth())") |
|---|
| 70 | n/a | self.check_suite("async def f():\n foo((await smth()))") |
|---|
| 71 | n/a | self.check_suite("async def f():\n await foo(); return 42") |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | def test_async_with_statement(self): |
|---|
| 74 | n/a | self.check_suite("async def f():\n async with 1: pass") |
|---|
| 75 | n/a | self.check_suite("async def f():\n async with a as b, c as d: pass") |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | def test_async_for_statement(self): |
|---|
| 78 | n/a | self.check_suite("async def f():\n async for i in (): pass") |
|---|
| 79 | n/a | self.check_suite("async def f():\n async for i, b in (): pass") |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | def test_nonlocal_statement(self): |
|---|
| 82 | n/a | self.check_suite("def f():\n" |
|---|
| 83 | n/a | " x = 0\n" |
|---|
| 84 | n/a | " def g():\n" |
|---|
| 85 | n/a | " nonlocal x\n") |
|---|
| 86 | n/a | self.check_suite("def f():\n" |
|---|
| 87 | n/a | " x = y = 0\n" |
|---|
| 88 | n/a | " def g():\n" |
|---|
| 89 | n/a | " nonlocal x, y\n") |
|---|
| 90 | n/a | |
|---|
| 91 | n/a | def test_expressions(self): |
|---|
| 92 | n/a | self.check_expr("foo(1)") |
|---|
| 93 | n/a | self.check_expr("[1, 2, 3]") |
|---|
| 94 | n/a | self.check_expr("[x**3 for x in range(20)]") |
|---|
| 95 | n/a | self.check_expr("[x**3 for x in range(20) if x % 3]") |
|---|
| 96 | n/a | self.check_expr("[x**3 for x in range(20) if x % 2 if x % 3]") |
|---|
| 97 | n/a | self.check_expr("list(x**3 for x in range(20))") |
|---|
| 98 | n/a | self.check_expr("list(x**3 for x in range(20) if x % 3)") |
|---|
| 99 | n/a | self.check_expr("list(x**3 for x in range(20) if x % 2 if x % 3)") |
|---|
| 100 | n/a | self.check_expr("foo(*args)") |
|---|
| 101 | n/a | self.check_expr("foo(*args, **kw)") |
|---|
| 102 | n/a | self.check_expr("foo(**kw)") |
|---|
| 103 | n/a | self.check_expr("foo(key=value)") |
|---|
| 104 | n/a | self.check_expr("foo(key=value, *args)") |
|---|
| 105 | n/a | self.check_expr("foo(key=value, *args, **kw)") |
|---|
| 106 | n/a | self.check_expr("foo(key=value, **kw)") |
|---|
| 107 | n/a | self.check_expr("foo(a, b, c, *args)") |
|---|
| 108 | n/a | self.check_expr("foo(a, b, c, *args, **kw)") |
|---|
| 109 | n/a | self.check_expr("foo(a, b, c, **kw)") |
|---|
| 110 | n/a | self.check_expr("foo(a, *args, keyword=23)") |
|---|
| 111 | n/a | self.check_expr("foo + bar") |
|---|
| 112 | n/a | self.check_expr("foo - bar") |
|---|
| 113 | n/a | self.check_expr("foo * bar") |
|---|
| 114 | n/a | self.check_expr("foo / bar") |
|---|
| 115 | n/a | self.check_expr("foo // bar") |
|---|
| 116 | n/a | self.check_expr("lambda: 0") |
|---|
| 117 | n/a | self.check_expr("lambda x: 0") |
|---|
| 118 | n/a | self.check_expr("lambda *y: 0") |
|---|
| 119 | n/a | self.check_expr("lambda *y, **z: 0") |
|---|
| 120 | n/a | self.check_expr("lambda **z: 0") |
|---|
| 121 | n/a | self.check_expr("lambda x, y: 0") |
|---|
| 122 | n/a | self.check_expr("lambda foo=bar: 0") |
|---|
| 123 | n/a | self.check_expr("lambda foo=bar, spaz=nifty+spit: 0") |
|---|
| 124 | n/a | self.check_expr("lambda foo=bar, **z: 0") |
|---|
| 125 | n/a | self.check_expr("lambda foo=bar, blaz=blat+2, **z: 0") |
|---|
| 126 | n/a | self.check_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0") |
|---|
| 127 | n/a | self.check_expr("lambda x, *y, **z: 0") |
|---|
| 128 | n/a | self.check_expr("(x for x in range(10))") |
|---|
| 129 | n/a | self.check_expr("foo(x for x in range(10))") |
|---|
| 130 | n/a | self.check_expr("...") |
|---|
| 131 | n/a | self.check_expr("a[...]") |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | def test_simple_expression(self): |
|---|
| 134 | n/a | # expr_stmt |
|---|
| 135 | n/a | self.check_suite("a") |
|---|
| 136 | n/a | |
|---|
| 137 | n/a | def test_simple_assignments(self): |
|---|
| 138 | n/a | self.check_suite("a = b") |
|---|
| 139 | n/a | self.check_suite("a = b = c = d = e") |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | def test_var_annot(self): |
|---|
| 142 | n/a | self.check_suite("x: int = 5") |
|---|
| 143 | n/a | self.check_suite("y: List[T] = []; z: [list] = fun()") |
|---|
| 144 | n/a | self.check_suite("x: tuple = (1, 2)") |
|---|
| 145 | n/a | self.check_suite("d[f()]: int = 42") |
|---|
| 146 | n/a | self.check_suite("f(d[x]): str = 'abc'") |
|---|
| 147 | n/a | self.check_suite("x.y.z.w: complex = 42j") |
|---|
| 148 | n/a | self.check_suite("x: int") |
|---|
| 149 | n/a | self.check_suite("def f():\n" |
|---|
| 150 | n/a | " x: str\n" |
|---|
| 151 | n/a | " y: int = 5\n") |
|---|
| 152 | n/a | self.check_suite("class C:\n" |
|---|
| 153 | n/a | " x: str\n" |
|---|
| 154 | n/a | " y: int = 5\n") |
|---|
| 155 | n/a | self.check_suite("class C:\n" |
|---|
| 156 | n/a | " def __init__(self, x: int) -> None:\n" |
|---|
| 157 | n/a | " self.x: int = x\n") |
|---|
| 158 | n/a | # double check for nonsense |
|---|
| 159 | n/a | with self.assertRaises(SyntaxError): |
|---|
| 160 | n/a | exec("2+2: int", {}, {}) |
|---|
| 161 | n/a | with self.assertRaises(SyntaxError): |
|---|
| 162 | n/a | exec("[]: int = 5", {}, {}) |
|---|
| 163 | n/a | with self.assertRaises(SyntaxError): |
|---|
| 164 | n/a | exec("x, *y, z: int = range(5)", {}, {}) |
|---|
| 165 | n/a | with self.assertRaises(SyntaxError): |
|---|
| 166 | n/a | exec("t: tuple = 1, 2", {}, {}) |
|---|
| 167 | n/a | with self.assertRaises(SyntaxError): |
|---|
| 168 | n/a | exec("u = v: int", {}, {}) |
|---|
| 169 | n/a | with self.assertRaises(SyntaxError): |
|---|
| 170 | n/a | exec("False: int", {}, {}) |
|---|
| 171 | n/a | with self.assertRaises(SyntaxError): |
|---|
| 172 | n/a | exec("x.False: int", {}, {}) |
|---|
| 173 | n/a | with self.assertRaises(SyntaxError): |
|---|
| 174 | n/a | exec("x.y,: int", {}, {}) |
|---|
| 175 | n/a | with self.assertRaises(SyntaxError): |
|---|
| 176 | n/a | exec("[0]: int", {}, {}) |
|---|
| 177 | n/a | with self.assertRaises(SyntaxError): |
|---|
| 178 | n/a | exec("f(): int", {}, {}) |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | def test_simple_augmented_assignments(self): |
|---|
| 181 | n/a | self.check_suite("a += b") |
|---|
| 182 | n/a | self.check_suite("a -= b") |
|---|
| 183 | n/a | self.check_suite("a *= b") |
|---|
| 184 | n/a | self.check_suite("a /= b") |
|---|
| 185 | n/a | self.check_suite("a //= b") |
|---|
| 186 | n/a | self.check_suite("a %= b") |
|---|
| 187 | n/a | self.check_suite("a &= b") |
|---|
| 188 | n/a | self.check_suite("a |= b") |
|---|
| 189 | n/a | self.check_suite("a ^= b") |
|---|
| 190 | n/a | self.check_suite("a <<= b") |
|---|
| 191 | n/a | self.check_suite("a >>= b") |
|---|
| 192 | n/a | self.check_suite("a **= b") |
|---|
| 193 | n/a | |
|---|
| 194 | n/a | def test_function_defs(self): |
|---|
| 195 | n/a | self.check_suite("def f(): pass") |
|---|
| 196 | n/a | self.check_suite("def f(*args): pass") |
|---|
| 197 | n/a | self.check_suite("def f(*args, **kw): pass") |
|---|
| 198 | n/a | self.check_suite("def f(**kw): pass") |
|---|
| 199 | n/a | self.check_suite("def f(foo=bar): pass") |
|---|
| 200 | n/a | self.check_suite("def f(foo=bar, *args): pass") |
|---|
| 201 | n/a | self.check_suite("def f(foo=bar, *args, **kw): pass") |
|---|
| 202 | n/a | self.check_suite("def f(foo=bar, **kw): pass") |
|---|
| 203 | n/a | |
|---|
| 204 | n/a | self.check_suite("def f(a, b): pass") |
|---|
| 205 | n/a | self.check_suite("def f(a, b, *args): pass") |
|---|
| 206 | n/a | self.check_suite("def f(a, b, *args, **kw): pass") |
|---|
| 207 | n/a | self.check_suite("def f(a, b, **kw): pass") |
|---|
| 208 | n/a | self.check_suite("def f(a, b, foo=bar): pass") |
|---|
| 209 | n/a | self.check_suite("def f(a, b, foo=bar, *args): pass") |
|---|
| 210 | n/a | self.check_suite("def f(a, b, foo=bar, *args, **kw): pass") |
|---|
| 211 | n/a | self.check_suite("def f(a, b, foo=bar, **kw): pass") |
|---|
| 212 | n/a | |
|---|
| 213 | n/a | self.check_suite("@staticmethod\n" |
|---|
| 214 | n/a | "def f(): pass") |
|---|
| 215 | n/a | self.check_suite("@staticmethod\n" |
|---|
| 216 | n/a | "@funcattrs(x, y)\n" |
|---|
| 217 | n/a | "def f(): pass") |
|---|
| 218 | n/a | self.check_suite("@funcattrs()\n" |
|---|
| 219 | n/a | "def f(): pass") |
|---|
| 220 | n/a | |
|---|
| 221 | n/a | # keyword-only arguments |
|---|
| 222 | n/a | self.check_suite("def f(*, a): pass") |
|---|
| 223 | n/a | self.check_suite("def f(*, a = 5): pass") |
|---|
| 224 | n/a | self.check_suite("def f(*, a = 5, b): pass") |
|---|
| 225 | n/a | self.check_suite("def f(*, a, b = 5): pass") |
|---|
| 226 | n/a | self.check_suite("def f(*, a, b = 5, **kwds): pass") |
|---|
| 227 | n/a | self.check_suite("def f(*args, a): pass") |
|---|
| 228 | n/a | self.check_suite("def f(*args, a = 5): pass") |
|---|
| 229 | n/a | self.check_suite("def f(*args, a = 5, b): pass") |
|---|
| 230 | n/a | self.check_suite("def f(*args, a, b = 5): pass") |
|---|
| 231 | n/a | self.check_suite("def f(*args, a, b = 5, **kwds): pass") |
|---|
| 232 | n/a | |
|---|
| 233 | n/a | # function annotations |
|---|
| 234 | n/a | self.check_suite("def f(a: int): pass") |
|---|
| 235 | n/a | self.check_suite("def f(a: int = 5): pass") |
|---|
| 236 | n/a | self.check_suite("def f(*args: list): pass") |
|---|
| 237 | n/a | self.check_suite("def f(**kwds: dict): pass") |
|---|
| 238 | n/a | self.check_suite("def f(*, a: int): pass") |
|---|
| 239 | n/a | self.check_suite("def f(*, a: int = 5): pass") |
|---|
| 240 | n/a | self.check_suite("def f() -> int: pass") |
|---|
| 241 | n/a | |
|---|
| 242 | n/a | def test_class_defs(self): |
|---|
| 243 | n/a | self.check_suite("class foo():pass") |
|---|
| 244 | n/a | self.check_suite("class foo(object):pass") |
|---|
| 245 | n/a | self.check_suite("@class_decorator\n" |
|---|
| 246 | n/a | "class foo():pass") |
|---|
| 247 | n/a | self.check_suite("@class_decorator(arg)\n" |
|---|
| 248 | n/a | "class foo():pass") |
|---|
| 249 | n/a | self.check_suite("@decorator1\n" |
|---|
| 250 | n/a | "@decorator2\n" |
|---|
| 251 | n/a | "class foo():pass") |
|---|
| 252 | n/a | |
|---|
| 253 | n/a | def test_import_from_statement(self): |
|---|
| 254 | n/a | self.check_suite("from sys.path import *") |
|---|
| 255 | n/a | self.check_suite("from sys.path import dirname") |
|---|
| 256 | n/a | self.check_suite("from sys.path import (dirname)") |
|---|
| 257 | n/a | self.check_suite("from sys.path import (dirname,)") |
|---|
| 258 | n/a | self.check_suite("from sys.path import dirname as my_dirname") |
|---|
| 259 | n/a | self.check_suite("from sys.path import (dirname as my_dirname)") |
|---|
| 260 | n/a | self.check_suite("from sys.path import (dirname as my_dirname,)") |
|---|
| 261 | n/a | self.check_suite("from sys.path import dirname, basename") |
|---|
| 262 | n/a | self.check_suite("from sys.path import (dirname, basename)") |
|---|
| 263 | n/a | self.check_suite("from sys.path import (dirname, basename,)") |
|---|
| 264 | n/a | self.check_suite( |
|---|
| 265 | n/a | "from sys.path import dirname as my_dirname, basename") |
|---|
| 266 | n/a | self.check_suite( |
|---|
| 267 | n/a | "from sys.path import (dirname as my_dirname, basename)") |
|---|
| 268 | n/a | self.check_suite( |
|---|
| 269 | n/a | "from sys.path import (dirname as my_dirname, basename,)") |
|---|
| 270 | n/a | self.check_suite( |
|---|
| 271 | n/a | "from sys.path import dirname, basename as my_basename") |
|---|
| 272 | n/a | self.check_suite( |
|---|
| 273 | n/a | "from sys.path import (dirname, basename as my_basename)") |
|---|
| 274 | n/a | self.check_suite( |
|---|
| 275 | n/a | "from sys.path import (dirname, basename as my_basename,)") |
|---|
| 276 | n/a | self.check_suite("from .bogus import x") |
|---|
| 277 | n/a | |
|---|
| 278 | n/a | def test_basic_import_statement(self): |
|---|
| 279 | n/a | self.check_suite("import sys") |
|---|
| 280 | n/a | self.check_suite("import sys as system") |
|---|
| 281 | n/a | self.check_suite("import sys, math") |
|---|
| 282 | n/a | self.check_suite("import sys as system, math") |
|---|
| 283 | n/a | self.check_suite("import sys, math as my_math") |
|---|
| 284 | n/a | |
|---|
| 285 | n/a | def test_relative_imports(self): |
|---|
| 286 | n/a | self.check_suite("from . import name") |
|---|
| 287 | n/a | self.check_suite("from .. import name") |
|---|
| 288 | n/a | # check all the way up to '....', since '...' is tokenized |
|---|
| 289 | n/a | # differently from '.' (it's an ellipsis token). |
|---|
| 290 | n/a | self.check_suite("from ... import name") |
|---|
| 291 | n/a | self.check_suite("from .... import name") |
|---|
| 292 | n/a | self.check_suite("from .pkg import name") |
|---|
| 293 | n/a | self.check_suite("from ..pkg import name") |
|---|
| 294 | n/a | self.check_suite("from ...pkg import name") |
|---|
| 295 | n/a | self.check_suite("from ....pkg import name") |
|---|
| 296 | n/a | |
|---|
| 297 | n/a | def test_pep263(self): |
|---|
| 298 | n/a | self.check_suite("# -*- coding: iso-8859-1 -*-\n" |
|---|
| 299 | n/a | "pass\n") |
|---|
| 300 | n/a | |
|---|
| 301 | n/a | def test_assert(self): |
|---|
| 302 | n/a | self.check_suite("assert alo < ahi and blo < bhi\n") |
|---|
| 303 | n/a | |
|---|
| 304 | n/a | def test_with(self): |
|---|
| 305 | n/a | self.check_suite("with open('x'): pass\n") |
|---|
| 306 | n/a | self.check_suite("with open('x') as f: pass\n") |
|---|
| 307 | n/a | self.check_suite("with open('x') as f, open('y') as g: pass\n") |
|---|
| 308 | n/a | |
|---|
| 309 | n/a | def test_try_stmt(self): |
|---|
| 310 | n/a | self.check_suite("try: pass\nexcept: pass\n") |
|---|
| 311 | n/a | self.check_suite("try: pass\nfinally: pass\n") |
|---|
| 312 | n/a | self.check_suite("try: pass\nexcept A: pass\nfinally: pass\n") |
|---|
| 313 | n/a | self.check_suite("try: pass\nexcept A: pass\nexcept: pass\n" |
|---|
| 314 | n/a | "finally: pass\n") |
|---|
| 315 | n/a | self.check_suite("try: pass\nexcept: pass\nelse: pass\n") |
|---|
| 316 | n/a | self.check_suite("try: pass\nexcept: pass\nelse: pass\n" |
|---|
| 317 | n/a | "finally: pass\n") |
|---|
| 318 | n/a | |
|---|
| 319 | n/a | def test_position(self): |
|---|
| 320 | n/a | # An absolutely minimal test of position information. Better |
|---|
| 321 | n/a | # tests would be a big project. |
|---|
| 322 | n/a | code = "def f(x):\n return x + 1" |
|---|
| 323 | n/a | st1 = parser.suite(code) |
|---|
| 324 | n/a | st2 = st1.totuple(line_info=1, col_info=1) |
|---|
| 325 | n/a | |
|---|
| 326 | n/a | def walk(tree): |
|---|
| 327 | n/a | node_type = tree[0] |
|---|
| 328 | n/a | next = tree[1] |
|---|
| 329 | n/a | if isinstance(next, tuple): |
|---|
| 330 | n/a | for elt in tree[1:]: |
|---|
| 331 | n/a | for x in walk(elt): |
|---|
| 332 | n/a | yield x |
|---|
| 333 | n/a | else: |
|---|
| 334 | n/a | yield tree |
|---|
| 335 | n/a | |
|---|
| 336 | n/a | terminals = list(walk(st2)) |
|---|
| 337 | n/a | self.assertEqual([ |
|---|
| 338 | n/a | (1, 'def', 1, 0), |
|---|
| 339 | n/a | (1, 'f', 1, 4), |
|---|
| 340 | n/a | (7, '(', 1, 5), |
|---|
| 341 | n/a | (1, 'x', 1, 6), |
|---|
| 342 | n/a | (8, ')', 1, 7), |
|---|
| 343 | n/a | (11, ':', 1, 8), |
|---|
| 344 | n/a | (4, '', 1, 9), |
|---|
| 345 | n/a | (5, '', 2, -1), |
|---|
| 346 | n/a | (1, 'return', 2, 4), |
|---|
| 347 | n/a | (1, 'x', 2, 11), |
|---|
| 348 | n/a | (14, '+', 2, 13), |
|---|
| 349 | n/a | (2, '1', 2, 15), |
|---|
| 350 | n/a | (4, '', 2, 16), |
|---|
| 351 | n/a | (6, '', 2, -1), |
|---|
| 352 | n/a | (4, '', 2, -1), |
|---|
| 353 | n/a | (0, '', 2, -1)], |
|---|
| 354 | n/a | terminals) |
|---|
| 355 | n/a | |
|---|
| 356 | n/a | def test_extended_unpacking(self): |
|---|
| 357 | n/a | self.check_suite("*a = y") |
|---|
| 358 | n/a | self.check_suite("x, *b, = m") |
|---|
| 359 | n/a | self.check_suite("[*a, *b] = y") |
|---|
| 360 | n/a | self.check_suite("for [*x, b] in x: pass") |
|---|
| 361 | n/a | |
|---|
| 362 | n/a | def test_raise_statement(self): |
|---|
| 363 | n/a | self.check_suite("raise\n") |
|---|
| 364 | n/a | self.check_suite("raise e\n") |
|---|
| 365 | n/a | self.check_suite("try:\n" |
|---|
| 366 | n/a | " suite\n" |
|---|
| 367 | n/a | "except Exception as e:\n" |
|---|
| 368 | n/a | " raise ValueError from e\n") |
|---|
| 369 | n/a | |
|---|
| 370 | n/a | def test_list_displays(self): |
|---|
| 371 | n/a | self.check_expr('[]') |
|---|
| 372 | n/a | self.check_expr('[*{2}, 3, *[4]]') |
|---|
| 373 | n/a | |
|---|
| 374 | n/a | def test_set_displays(self): |
|---|
| 375 | n/a | self.check_expr('{*{2}, 3, *[4]}') |
|---|
| 376 | n/a | self.check_expr('{2}') |
|---|
| 377 | n/a | self.check_expr('{2,}') |
|---|
| 378 | n/a | self.check_expr('{2, 3}') |
|---|
| 379 | n/a | self.check_expr('{2, 3,}') |
|---|
| 380 | n/a | |
|---|
| 381 | n/a | def test_dict_displays(self): |
|---|
| 382 | n/a | self.check_expr('{}') |
|---|
| 383 | n/a | self.check_expr('{a:b}') |
|---|
| 384 | n/a | self.check_expr('{a:b,}') |
|---|
| 385 | n/a | self.check_expr('{a:b, c:d}') |
|---|
| 386 | n/a | self.check_expr('{a:b, c:d,}') |
|---|
| 387 | n/a | self.check_expr('{**{}}') |
|---|
| 388 | n/a | self.check_expr('{**{}, 3:4, **{5:6, 7:8}}') |
|---|
| 389 | n/a | |
|---|
| 390 | n/a | def test_argument_unpacking(self): |
|---|
| 391 | n/a | self.check_expr("f(*a, **b)") |
|---|
| 392 | n/a | self.check_expr('f(a, *b, *c, *d)') |
|---|
| 393 | n/a | self.check_expr('f(**a, **b)') |
|---|
| 394 | n/a | self.check_expr('f(2, *a, *b, **b, **c, **d)') |
|---|
| 395 | n/a | self.check_expr("f(*b, *() or () and (), **{} and {}, **() or {})") |
|---|
| 396 | n/a | |
|---|
| 397 | n/a | def test_set_comprehensions(self): |
|---|
| 398 | n/a | self.check_expr('{x for x in seq}') |
|---|
| 399 | n/a | self.check_expr('{f(x) for x in seq}') |
|---|
| 400 | n/a | self.check_expr('{f(x) for x in seq if condition(x)}') |
|---|
| 401 | n/a | |
|---|
| 402 | n/a | def test_dict_comprehensions(self): |
|---|
| 403 | n/a | self.check_expr('{x:x for x in seq}') |
|---|
| 404 | n/a | self.check_expr('{x**2:x[3] for x in seq if condition(x)}') |
|---|
| 405 | n/a | self.check_expr('{x:x for x in seq1 for y in seq2 if condition(x, y)}') |
|---|
| 406 | n/a | |
|---|
| 407 | n/a | |
|---|
| 408 | n/a | # |
|---|
| 409 | n/a | # Second, we take *invalid* trees and make sure we get ParserError |
|---|
| 410 | n/a | # rejections for them. |
|---|
| 411 | n/a | # |
|---|
| 412 | n/a | |
|---|
| 413 | n/a | class IllegalSyntaxTestCase(unittest.TestCase): |
|---|
| 414 | n/a | |
|---|
| 415 | n/a | def check_bad_tree(self, tree, label): |
|---|
| 416 | n/a | try: |
|---|
| 417 | n/a | parser.sequence2st(tree) |
|---|
| 418 | n/a | except parser.ParserError: |
|---|
| 419 | n/a | pass |
|---|
| 420 | n/a | else: |
|---|
| 421 | n/a | self.fail("did not detect invalid tree for %r" % label) |
|---|
| 422 | n/a | |
|---|
| 423 | n/a | def test_junk(self): |
|---|
| 424 | n/a | # not even remotely valid: |
|---|
| 425 | n/a | self.check_bad_tree((1, 2, 3), "<junk>") |
|---|
| 426 | n/a | |
|---|
| 427 | n/a | def test_illegal_yield_1(self): |
|---|
| 428 | n/a | # Illegal yield statement: def f(): return 1; yield 1 |
|---|
| 429 | n/a | tree = \ |
|---|
| 430 | n/a | (257, |
|---|
| 431 | n/a | (264, |
|---|
| 432 | n/a | (285, |
|---|
| 433 | n/a | (259, |
|---|
| 434 | n/a | (1, 'def'), |
|---|
| 435 | n/a | (1, 'f'), |
|---|
| 436 | n/a | (260, (7, '('), (8, ')')), |
|---|
| 437 | n/a | (11, ':'), |
|---|
| 438 | n/a | (291, |
|---|
| 439 | n/a | (4, ''), |
|---|
| 440 | n/a | (5, ''), |
|---|
| 441 | n/a | (264, |
|---|
| 442 | n/a | (265, |
|---|
| 443 | n/a | (266, |
|---|
| 444 | n/a | (272, |
|---|
| 445 | n/a | (275, |
|---|
| 446 | n/a | (1, 'return'), |
|---|
| 447 | n/a | (313, |
|---|
| 448 | n/a | (292, |
|---|
| 449 | n/a | (293, |
|---|
| 450 | n/a | (294, |
|---|
| 451 | n/a | (295, |
|---|
| 452 | n/a | (297, |
|---|
| 453 | n/a | (298, |
|---|
| 454 | n/a | (299, |
|---|
| 455 | n/a | (300, |
|---|
| 456 | n/a | (301, |
|---|
| 457 | n/a | (302, (303, (304, (305, (2, '1')))))))))))))))))), |
|---|
| 458 | n/a | (264, |
|---|
| 459 | n/a | (265, |
|---|
| 460 | n/a | (266, |
|---|
| 461 | n/a | (272, |
|---|
| 462 | n/a | (276, |
|---|
| 463 | n/a | (1, 'yield'), |
|---|
| 464 | n/a | (313, |
|---|
| 465 | n/a | (292, |
|---|
| 466 | n/a | (293, |
|---|
| 467 | n/a | (294, |
|---|
| 468 | n/a | (295, |
|---|
| 469 | n/a | (297, |
|---|
| 470 | n/a | (298, |
|---|
| 471 | n/a | (299, |
|---|
| 472 | n/a | (300, |
|---|
| 473 | n/a | (301, |
|---|
| 474 | n/a | (302, |
|---|
| 475 | n/a | (303, (304, (305, (2, '1')))))))))))))))))), |
|---|
| 476 | n/a | (4, ''))), |
|---|
| 477 | n/a | (6, ''))))), |
|---|
| 478 | n/a | (4, ''), |
|---|
| 479 | n/a | (0, '')))) |
|---|
| 480 | n/a | self.check_bad_tree(tree, "def f():\n return 1\n yield 1") |
|---|
| 481 | n/a | |
|---|
| 482 | n/a | def test_illegal_yield_2(self): |
|---|
| 483 | n/a | # Illegal return in generator: def f(): return 1; yield 1 |
|---|
| 484 | n/a | tree = \ |
|---|
| 485 | n/a | (257, |
|---|
| 486 | n/a | (264, |
|---|
| 487 | n/a | (265, |
|---|
| 488 | n/a | (266, |
|---|
| 489 | n/a | (278, |
|---|
| 490 | n/a | (1, 'from'), |
|---|
| 491 | n/a | (281, (1, '__future__')), |
|---|
| 492 | n/a | (1, 'import'), |
|---|
| 493 | n/a | (279, (1, 'generators')))), |
|---|
| 494 | n/a | (4, ''))), |
|---|
| 495 | n/a | (264, |
|---|
| 496 | n/a | (285, |
|---|
| 497 | n/a | (259, |
|---|
| 498 | n/a | (1, 'def'), |
|---|
| 499 | n/a | (1, 'f'), |
|---|
| 500 | n/a | (260, (7, '('), (8, ')')), |
|---|
| 501 | n/a | (11, ':'), |
|---|
| 502 | n/a | (291, |
|---|
| 503 | n/a | (4, ''), |
|---|
| 504 | n/a | (5, ''), |
|---|
| 505 | n/a | (264, |
|---|
| 506 | n/a | (265, |
|---|
| 507 | n/a | (266, |
|---|
| 508 | n/a | (272, |
|---|
| 509 | n/a | (275, |
|---|
| 510 | n/a | (1, 'return'), |
|---|
| 511 | n/a | (313, |
|---|
| 512 | n/a | (292, |
|---|
| 513 | n/a | (293, |
|---|
| 514 | n/a | (294, |
|---|
| 515 | n/a | (295, |
|---|
| 516 | n/a | (297, |
|---|
| 517 | n/a | (298, |
|---|
| 518 | n/a | (299, |
|---|
| 519 | n/a | (300, |
|---|
| 520 | n/a | (301, |
|---|
| 521 | n/a | (302, (303, (304, (305, (2, '1')))))))))))))))))), |
|---|
| 522 | n/a | (264, |
|---|
| 523 | n/a | (265, |
|---|
| 524 | n/a | (266, |
|---|
| 525 | n/a | (272, |
|---|
| 526 | n/a | (276, |
|---|
| 527 | n/a | (1, 'yield'), |
|---|
| 528 | n/a | (313, |
|---|
| 529 | n/a | (292, |
|---|
| 530 | n/a | (293, |
|---|
| 531 | n/a | (294, |
|---|
| 532 | n/a | (295, |
|---|
| 533 | n/a | (297, |
|---|
| 534 | n/a | (298, |
|---|
| 535 | n/a | (299, |
|---|
| 536 | n/a | (300, |
|---|
| 537 | n/a | (301, |
|---|
| 538 | n/a | (302, |
|---|
| 539 | n/a | (303, (304, (305, (2, '1')))))))))))))))))), |
|---|
| 540 | n/a | (4, ''))), |
|---|
| 541 | n/a | (6, ''))))), |
|---|
| 542 | n/a | (4, ''), |
|---|
| 543 | n/a | (0, '')))) |
|---|
| 544 | n/a | self.check_bad_tree(tree, "def f():\n return 1\n yield 1") |
|---|
| 545 | n/a | |
|---|
| 546 | n/a | def test_a_comma_comma_c(self): |
|---|
| 547 | n/a | # Illegal input: a,,c |
|---|
| 548 | n/a | tree = \ |
|---|
| 549 | n/a | (258, |
|---|
| 550 | n/a | (311, |
|---|
| 551 | n/a | (290, |
|---|
| 552 | n/a | (291, |
|---|
| 553 | n/a | (292, |
|---|
| 554 | n/a | (293, |
|---|
| 555 | n/a | (295, |
|---|
| 556 | n/a | (296, |
|---|
| 557 | n/a | (297, |
|---|
| 558 | n/a | (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))), |
|---|
| 559 | n/a | (12, ','), |
|---|
| 560 | n/a | (12, ','), |
|---|
| 561 | n/a | (290, |
|---|
| 562 | n/a | (291, |
|---|
| 563 | n/a | (292, |
|---|
| 564 | n/a | (293, |
|---|
| 565 | n/a | (295, |
|---|
| 566 | n/a | (296, |
|---|
| 567 | n/a | (297, |
|---|
| 568 | n/a | (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))), |
|---|
| 569 | n/a | (4, ''), |
|---|
| 570 | n/a | (0, '')) |
|---|
| 571 | n/a | self.check_bad_tree(tree, "a,,c") |
|---|
| 572 | n/a | |
|---|
| 573 | n/a | def test_illegal_operator(self): |
|---|
| 574 | n/a | # Illegal input: a $= b |
|---|
| 575 | n/a | tree = \ |
|---|
| 576 | n/a | (257, |
|---|
| 577 | n/a | (264, |
|---|
| 578 | n/a | (265, |
|---|
| 579 | n/a | (266, |
|---|
| 580 | n/a | (267, |
|---|
| 581 | n/a | (312, |
|---|
| 582 | n/a | (291, |
|---|
| 583 | n/a | (292, |
|---|
| 584 | n/a | (293, |
|---|
| 585 | n/a | (294, |
|---|
| 586 | n/a | (296, |
|---|
| 587 | n/a | (297, |
|---|
| 588 | n/a | (298, |
|---|
| 589 | n/a | (299, |
|---|
| 590 | n/a | (300, (301, (302, (303, (304, (1, 'a'))))))))))))))), |
|---|
| 591 | n/a | (268, (37, '$=')), |
|---|
| 592 | n/a | (312, |
|---|
| 593 | n/a | (291, |
|---|
| 594 | n/a | (292, |
|---|
| 595 | n/a | (293, |
|---|
| 596 | n/a | (294, |
|---|
| 597 | n/a | (296, |
|---|
| 598 | n/a | (297, |
|---|
| 599 | n/a | (298, |
|---|
| 600 | n/a | (299, |
|---|
| 601 | n/a | (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))), |
|---|
| 602 | n/a | (4, ''))), |
|---|
| 603 | n/a | (0, '')) |
|---|
| 604 | n/a | self.check_bad_tree(tree, "a $= b") |
|---|
| 605 | n/a | |
|---|
| 606 | n/a | def test_malformed_global(self): |
|---|
| 607 | n/a | #doesn't have global keyword in ast |
|---|
| 608 | n/a | tree = (257, |
|---|
| 609 | n/a | (264, |
|---|
| 610 | n/a | (265, |
|---|
| 611 | n/a | (266, |
|---|
| 612 | n/a | (282, (1, 'foo'))), (4, ''))), |
|---|
| 613 | n/a | (4, ''), |
|---|
| 614 | n/a | (0, '')) |
|---|
| 615 | n/a | self.check_bad_tree(tree, "malformed global ast") |
|---|
| 616 | n/a | |
|---|
| 617 | n/a | def test_missing_import_source(self): |
|---|
| 618 | n/a | # from import fred |
|---|
| 619 | n/a | tree = \ |
|---|
| 620 | n/a | (257, |
|---|
| 621 | n/a | (268, |
|---|
| 622 | n/a | (269, |
|---|
| 623 | n/a | (270, |
|---|
| 624 | n/a | (282, |
|---|
| 625 | n/a | (284, (1, 'from'), (1, 'import'), |
|---|
| 626 | n/a | (287, (285, (1, 'fred')))))), |
|---|
| 627 | n/a | (4, ''))), |
|---|
| 628 | n/a | (4, ''), (0, '')) |
|---|
| 629 | n/a | self.check_bad_tree(tree, "from import fred") |
|---|
| 630 | n/a | |
|---|
| 631 | n/a | |
|---|
| 632 | n/a | class CompileTestCase(unittest.TestCase): |
|---|
| 633 | n/a | |
|---|
| 634 | n/a | # These tests are very minimal. :-( |
|---|
| 635 | n/a | |
|---|
| 636 | n/a | def test_compile_expr(self): |
|---|
| 637 | n/a | st = parser.expr('2 + 3') |
|---|
| 638 | n/a | code = parser.compilest(st) |
|---|
| 639 | n/a | self.assertEqual(eval(code), 5) |
|---|
| 640 | n/a | |
|---|
| 641 | n/a | def test_compile_suite(self): |
|---|
| 642 | n/a | st = parser.suite('x = 2; y = x + 3') |
|---|
| 643 | n/a | code = parser.compilest(st) |
|---|
| 644 | n/a | globs = {} |
|---|
| 645 | n/a | exec(code, globs) |
|---|
| 646 | n/a | self.assertEqual(globs['y'], 5) |
|---|
| 647 | n/a | |
|---|
| 648 | n/a | def test_compile_error(self): |
|---|
| 649 | n/a | st = parser.suite('1 = 3 + 4') |
|---|
| 650 | n/a | self.assertRaises(SyntaxError, parser.compilest, st) |
|---|
| 651 | n/a | |
|---|
| 652 | n/a | def test_compile_badunicode(self): |
|---|
| 653 | n/a | st = parser.suite('a = "\\U12345678"') |
|---|
| 654 | n/a | self.assertRaises(SyntaxError, parser.compilest, st) |
|---|
| 655 | n/a | st = parser.suite('a = "\\u1"') |
|---|
| 656 | n/a | self.assertRaises(SyntaxError, parser.compilest, st) |
|---|
| 657 | n/a | |
|---|
| 658 | n/a | def test_issue_9011(self): |
|---|
| 659 | n/a | # Issue 9011: compilation of an unary minus expression changed |
|---|
| 660 | n/a | # the meaning of the ST, so that a second compilation produced |
|---|
| 661 | n/a | # incorrect results. |
|---|
| 662 | n/a | st = parser.expr('-3') |
|---|
| 663 | n/a | code1 = parser.compilest(st) |
|---|
| 664 | n/a | self.assertEqual(eval(code1), -3) |
|---|
| 665 | n/a | code2 = parser.compilest(st) |
|---|
| 666 | n/a | self.assertEqual(eval(code2), -3) |
|---|
| 667 | n/a | |
|---|
| 668 | n/a | def test_compile_filename(self): |
|---|
| 669 | n/a | st = parser.expr('a + 5') |
|---|
| 670 | n/a | code = parser.compilest(st) |
|---|
| 671 | n/a | self.assertEqual(code.co_filename, '<syntax-tree>') |
|---|
| 672 | n/a | code = st.compile() |
|---|
| 673 | n/a | self.assertEqual(code.co_filename, '<syntax-tree>') |
|---|
| 674 | n/a | for filename in 'file.py', b'file.py': |
|---|
| 675 | n/a | code = parser.compilest(st, filename) |
|---|
| 676 | n/a | self.assertEqual(code.co_filename, 'file.py') |
|---|
| 677 | n/a | code = st.compile(filename) |
|---|
| 678 | n/a | self.assertEqual(code.co_filename, 'file.py') |
|---|
| 679 | n/a | for filename in bytearray(b'file.py'), memoryview(b'file.py'): |
|---|
| 680 | n/a | with self.assertWarns(DeprecationWarning): |
|---|
| 681 | n/a | code = parser.compilest(st, filename) |
|---|
| 682 | n/a | self.assertEqual(code.co_filename, 'file.py') |
|---|
| 683 | n/a | with self.assertWarns(DeprecationWarning): |
|---|
| 684 | n/a | code = st.compile(filename) |
|---|
| 685 | n/a | self.assertEqual(code.co_filename, 'file.py') |
|---|
| 686 | n/a | self.assertRaises(TypeError, parser.compilest, st, list(b'file.py')) |
|---|
| 687 | n/a | self.assertRaises(TypeError, st.compile, list(b'file.py')) |
|---|
| 688 | n/a | |
|---|
| 689 | n/a | |
|---|
| 690 | n/a | class ParserStackLimitTestCase(unittest.TestCase): |
|---|
| 691 | n/a | """try to push the parser to/over its limits. |
|---|
| 692 | n/a | see http://bugs.python.org/issue1881 for a discussion |
|---|
| 693 | n/a | """ |
|---|
| 694 | n/a | def _nested_expression(self, level): |
|---|
| 695 | n/a | return "["*level+"]"*level |
|---|
| 696 | n/a | |
|---|
| 697 | n/a | def test_deeply_nested_list(self): |
|---|
| 698 | n/a | # XXX used to be 99 levels in 2.x |
|---|
| 699 | n/a | e = self._nested_expression(93) |
|---|
| 700 | n/a | st = parser.expr(e) |
|---|
| 701 | n/a | st.compile() |
|---|
| 702 | n/a | |
|---|
| 703 | n/a | def test_trigger_memory_error(self): |
|---|
| 704 | n/a | e = self._nested_expression(100) |
|---|
| 705 | n/a | rc, out, err = assert_python_failure('-c', e) |
|---|
| 706 | n/a | # parsing the expression will result in an error message |
|---|
| 707 | n/a | # followed by a MemoryError (see #11963) |
|---|
| 708 | n/a | self.assertIn(b's_push: parser stack overflow', err) |
|---|
| 709 | n/a | self.assertIn(b'MemoryError', err) |
|---|
| 710 | n/a | |
|---|
| 711 | n/a | class STObjectTestCase(unittest.TestCase): |
|---|
| 712 | n/a | """Test operations on ST objects themselves""" |
|---|
| 713 | n/a | |
|---|
| 714 | n/a | def test_comparisons(self): |
|---|
| 715 | n/a | # ST objects should support order and equality comparisons |
|---|
| 716 | n/a | st1 = parser.expr('2 + 3') |
|---|
| 717 | n/a | st2 = parser.suite('x = 2; y = x + 3') |
|---|
| 718 | n/a | st3 = parser.expr('list(x**3 for x in range(20))') |
|---|
| 719 | n/a | st1_copy = parser.expr('2 + 3') |
|---|
| 720 | n/a | st2_copy = parser.suite('x = 2; y = x + 3') |
|---|
| 721 | n/a | st3_copy = parser.expr('list(x**3 for x in range(20))') |
|---|
| 722 | n/a | |
|---|
| 723 | n/a | # exercise fast path for object identity |
|---|
| 724 | n/a | self.assertEqual(st1 == st1, True) |
|---|
| 725 | n/a | self.assertEqual(st2 == st2, True) |
|---|
| 726 | n/a | self.assertEqual(st3 == st3, True) |
|---|
| 727 | n/a | # slow path equality |
|---|
| 728 | n/a | self.assertEqual(st1, st1_copy) |
|---|
| 729 | n/a | self.assertEqual(st2, st2_copy) |
|---|
| 730 | n/a | self.assertEqual(st3, st3_copy) |
|---|
| 731 | n/a | self.assertEqual(st1 == st2, False) |
|---|
| 732 | n/a | self.assertEqual(st1 == st3, False) |
|---|
| 733 | n/a | self.assertEqual(st2 == st3, False) |
|---|
| 734 | n/a | self.assertEqual(st1 != st1, False) |
|---|
| 735 | n/a | self.assertEqual(st2 != st2, False) |
|---|
| 736 | n/a | self.assertEqual(st3 != st3, False) |
|---|
| 737 | n/a | self.assertEqual(st1 != st1_copy, False) |
|---|
| 738 | n/a | self.assertEqual(st2 != st2_copy, False) |
|---|
| 739 | n/a | self.assertEqual(st3 != st3_copy, False) |
|---|
| 740 | n/a | self.assertEqual(st2 != st1, True) |
|---|
| 741 | n/a | self.assertEqual(st1 != st3, True) |
|---|
| 742 | n/a | self.assertEqual(st3 != st2, True) |
|---|
| 743 | n/a | # we don't particularly care what the ordering is; just that |
|---|
| 744 | n/a | # it's usable and self-consistent |
|---|
| 745 | n/a | self.assertEqual(st1 < st2, not (st2 <= st1)) |
|---|
| 746 | n/a | self.assertEqual(st1 < st3, not (st3 <= st1)) |
|---|
| 747 | n/a | self.assertEqual(st2 < st3, not (st3 <= st2)) |
|---|
| 748 | n/a | self.assertEqual(st1 < st2, st2 > st1) |
|---|
| 749 | n/a | self.assertEqual(st1 < st3, st3 > st1) |
|---|
| 750 | n/a | self.assertEqual(st2 < st3, st3 > st2) |
|---|
| 751 | n/a | self.assertEqual(st1 <= st2, st2 >= st1) |
|---|
| 752 | n/a | self.assertEqual(st3 <= st1, st1 >= st3) |
|---|
| 753 | n/a | self.assertEqual(st2 <= st3, st3 >= st2) |
|---|
| 754 | n/a | # transitivity |
|---|
| 755 | n/a | bottom = min(st1, st2, st3) |
|---|
| 756 | n/a | top = max(st1, st2, st3) |
|---|
| 757 | n/a | mid = sorted([st1, st2, st3])[1] |
|---|
| 758 | n/a | self.assertTrue(bottom < mid) |
|---|
| 759 | n/a | self.assertTrue(bottom < top) |
|---|
| 760 | n/a | self.assertTrue(mid < top) |
|---|
| 761 | n/a | self.assertTrue(bottom <= mid) |
|---|
| 762 | n/a | self.assertTrue(bottom <= top) |
|---|
| 763 | n/a | self.assertTrue(mid <= top) |
|---|
| 764 | n/a | self.assertTrue(bottom <= bottom) |
|---|
| 765 | n/a | self.assertTrue(mid <= mid) |
|---|
| 766 | n/a | self.assertTrue(top <= top) |
|---|
| 767 | n/a | # interaction with other types |
|---|
| 768 | n/a | self.assertEqual(st1 == 1588.602459, False) |
|---|
| 769 | n/a | self.assertEqual('spanish armada' != st2, True) |
|---|
| 770 | n/a | self.assertRaises(TypeError, operator.ge, st3, None) |
|---|
| 771 | n/a | self.assertRaises(TypeError, operator.le, False, st1) |
|---|
| 772 | n/a | self.assertRaises(TypeError, operator.lt, st1, 1815) |
|---|
| 773 | n/a | self.assertRaises(TypeError, operator.gt, b'waterloo', st2) |
|---|
| 774 | n/a | |
|---|
| 775 | n/a | check_sizeof = support.check_sizeof |
|---|
| 776 | n/a | |
|---|
| 777 | n/a | @support.cpython_only |
|---|
| 778 | n/a | def test_sizeof(self): |
|---|
| 779 | n/a | def XXXROUNDUP(n): |
|---|
| 780 | n/a | if n <= 1: |
|---|
| 781 | n/a | return n |
|---|
| 782 | n/a | if n <= 128: |
|---|
| 783 | n/a | return (n + 3) & ~3 |
|---|
| 784 | n/a | return 1 << (n - 1).bit_length() |
|---|
| 785 | n/a | |
|---|
| 786 | n/a | basesize = support.calcobjsize('Pii') |
|---|
| 787 | n/a | nodesize = struct.calcsize('hP3iP0h') |
|---|
| 788 | n/a | def sizeofchildren(node): |
|---|
| 789 | n/a | if node is None: |
|---|
| 790 | n/a | return 0 |
|---|
| 791 | n/a | res = 0 |
|---|
| 792 | n/a | hasstr = len(node) > 1 and isinstance(node[-1], str) |
|---|
| 793 | n/a | if hasstr: |
|---|
| 794 | n/a | res += len(node[-1]) + 1 |
|---|
| 795 | n/a | children = node[1:-1] if hasstr else node[1:] |
|---|
| 796 | n/a | if children: |
|---|
| 797 | n/a | res += XXXROUNDUP(len(children)) * nodesize |
|---|
| 798 | n/a | for child in children: |
|---|
| 799 | n/a | res += sizeofchildren(child) |
|---|
| 800 | n/a | return res |
|---|
| 801 | n/a | |
|---|
| 802 | n/a | def check_st_sizeof(st): |
|---|
| 803 | n/a | self.check_sizeof(st, basesize + nodesize + |
|---|
| 804 | n/a | sizeofchildren(st.totuple())) |
|---|
| 805 | n/a | |
|---|
| 806 | n/a | check_st_sizeof(parser.expr('2 + 3')) |
|---|
| 807 | n/a | check_st_sizeof(parser.expr('2 + 3 + 4')) |
|---|
| 808 | n/a | check_st_sizeof(parser.suite('x = 2 + 3')) |
|---|
| 809 | n/a | check_st_sizeof(parser.suite('')) |
|---|
| 810 | n/a | check_st_sizeof(parser.suite('# -*- coding: utf-8 -*-')) |
|---|
| 811 | n/a | check_st_sizeof(parser.expr('[' + '2,' * 1000 + ']')) |
|---|
| 812 | n/a | |
|---|
| 813 | n/a | |
|---|
| 814 | n/a | # XXX tests for pickling and unpickling of ST objects should go here |
|---|
| 815 | n/a | |
|---|
| 816 | n/a | class OtherParserCase(unittest.TestCase): |
|---|
| 817 | n/a | |
|---|
| 818 | n/a | def test_two_args_to_expr(self): |
|---|
| 819 | n/a | # See bug #12264 |
|---|
| 820 | n/a | with self.assertRaises(TypeError): |
|---|
| 821 | n/a | parser.expr("a", "b") |
|---|
| 822 | n/a | |
|---|
| 823 | n/a | if __name__ == "__main__": |
|---|
| 824 | n/a | unittest.main() |
|---|