ยปCore Development>Code coverage>Lib/test/test_parser.py

Python code coverage for Lib/test/test_parser.py

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