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