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

Python code coverage for Lib/test/test_syntax.py

#countcontent
1n/a"""This module tests SyntaxErrors.
2n/a
3n/aHere's an example of the sort of thing that is tested.
4n/a
5n/a>>> def f(x):
6n/a... global x
7n/aTraceback (most recent call last):
8n/aSyntaxError: name 'x' is parameter and global
9n/a
10n/aThe tests are all raise SyntaxErrors. They were created by checking
11n/aeach C call that raises SyntaxError. There are several modules that
12n/araise these exceptions-- ast.c, compile.c, future.c, pythonrun.c, and
13n/asymtable.c.
14n/a
15n/aThe parser itself outlaws a lot of invalid syntax. None of these
16n/aerrors are tested here at the moment. We should add some tests; since
17n/athere are infinitely many programs with invalid syntax, we would need
18n/ato be judicious in selecting some.
19n/a
20n/aThe compiler generates a synthetic module name for code executed by
21n/adoctest. Since all the code comes from the same module, a suffix like
22n/a[1] is appended to the module name, As a consequence, changing the
23n/aorder of tests in this module means renumbering all the errors after
24n/ait. (Maybe we should enable the ellipsis option for these tests.)
25n/a
26n/aIn ast.c, syntax errors are raised by calling ast_error().
27n/a
28n/aErrors from set_context():
29n/a
30n/a>>> obj.None = 1
31n/aTraceback (most recent call last):
32n/aSyntaxError: invalid syntax
33n/a
34n/a>>> None = 1
35n/aTraceback (most recent call last):
36n/aSyntaxError: can't assign to keyword
37n/a
38n/a>>> f() = 1
39n/aTraceback (most recent call last):
40n/aSyntaxError: can't assign to function call
41n/a
42n/a>>> del f()
43n/aTraceback (most recent call last):
44n/aSyntaxError: can't delete function call
45n/a
46n/a>>> a + 1 = 2
47n/aTraceback (most recent call last):
48n/aSyntaxError: can't assign to operator
49n/a
50n/a>>> (x for x in x) = 1
51n/aTraceback (most recent call last):
52n/aSyntaxError: can't assign to generator expression
53n/a
54n/a>>> 1 = 1
55n/aTraceback (most recent call last):
56n/aSyntaxError: can't assign to literal
57n/a
58n/a>>> "abc" = 1
59n/aTraceback (most recent call last):
60n/aSyntaxError: can't assign to literal
61n/a
62n/a>>> b"" = 1
63n/aTraceback (most recent call last):
64n/aSyntaxError: can't assign to literal
65n/a
66n/a>>> `1` = 1
67n/aTraceback (most recent call last):
68n/aSyntaxError: invalid syntax
69n/a
70n/aIf the left-hand side of an assignment is a list or tuple, an illegal
71n/aexpression inside that contain should still cause a syntax error.
72n/aThis test just checks a couple of cases rather than enumerating all of
73n/athem.
74n/a
75n/a>>> (a, "b", c) = (1, 2, 3)
76n/aTraceback (most recent call last):
77n/aSyntaxError: can't assign to literal
78n/a
79n/a>>> [a, b, c + 1] = [1, 2, 3]
80n/aTraceback (most recent call last):
81n/aSyntaxError: can't assign to operator
82n/a
83n/a>>> a if 1 else b = 1
84n/aTraceback (most recent call last):
85n/aSyntaxError: can't assign to conditional expression
86n/a
87n/aFrom compiler_complex_args():
88n/a
89n/a>>> def f(None=1):
90n/a... pass
91n/aTraceback (most recent call last):
92n/aSyntaxError: invalid syntax
93n/a
94n/a
95n/aFrom ast_for_arguments():
96n/a
97n/a>>> def f(x, y=1, z):
98n/a... pass
99n/aTraceback (most recent call last):
100n/aSyntaxError: non-default argument follows default argument
101n/a
102n/a>>> def f(x, None):
103n/a... pass
104n/aTraceback (most recent call last):
105n/aSyntaxError: invalid syntax
106n/a
107n/a>>> def f(*None):
108n/a... pass
109n/aTraceback (most recent call last):
110n/aSyntaxError: invalid syntax
111n/a
112n/a>>> def f(**None):
113n/a... pass
114n/aTraceback (most recent call last):
115n/aSyntaxError: invalid syntax
116n/a
117n/a
118n/aFrom ast_for_funcdef():
119n/a
120n/a>>> def None(x):
121n/a... pass
122n/aTraceback (most recent call last):
123n/aSyntaxError: invalid syntax
124n/a
125n/a
126n/aFrom ast_for_call():
127n/a
128n/a>>> def f(it, *varargs):
129n/a... return list(it)
130n/a>>> L = range(10)
131n/a>>> f(x for x in L)
132n/a[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
133n/a>>> f(x for x in L, 1)
134n/aTraceback (most recent call last):
135n/aSyntaxError: Generator expression must be parenthesized if not sole argument
136n/a>>> f(x for x in L, y for y in L)
137n/aTraceback (most recent call last):
138n/aSyntaxError: Generator expression must be parenthesized if not sole argument
139n/a>>> f((x for x in L), 1)
140n/a[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
141n/a
142n/a>>> def g(*args, **kwargs):
143n/a... print(args, sorted(kwargs.items()))
144n/a>>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
145n/a... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
146n/a... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
147n/a... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
148n/a... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
149n/a... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
150n/a... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
151n/a... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
152n/a... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
153n/a... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
154n/a... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
155n/a... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
156n/a... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
157n/a... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
158n/a... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
159n/a... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
160n/a... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
161n/a... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
162n/a... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
163n/a... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
164n/a(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299) []
165n/a
166n/a>>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8,
167n/a... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16,
168n/a... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24,
169n/a... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32,
170n/a... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40,
171n/a... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48,
172n/a... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56,
173n/a... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64,
174n/a... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72,
175n/a... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80,
176n/a... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88,
177n/a... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96,
178n/a... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103,
179n/a... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110,
180n/a... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117,
181n/a... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124,
182n/a... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131,
183n/a... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138,
184n/a... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145,
185n/a... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152,
186n/a... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159,
187n/a... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166,
188n/a... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173,
189n/a... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180,
190n/a... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187,
191n/a... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194,
192n/a... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201,
193n/a... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208,
194n/a... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215,
195n/a... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222,
196n/a... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229,
197n/a... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236,
198n/a... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243,
199n/a... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250,
200n/a... a251=251, a252=252, a253=253, a254=254, a255=255, a256=256, a257=257,
201n/a... a258=258, a259=259, a260=260, a261=261, a262=262, a263=263, a264=264,
202n/a... a265=265, a266=266, a267=267, a268=268, a269=269, a270=270, a271=271,
203n/a... a272=272, a273=273, a274=274, a275=275, a276=276, a277=277, a278=278,
204n/a... a279=279, a280=280, a281=281, a282=282, a283=283, a284=284, a285=285,
205n/a... a286=286, a287=287, a288=288, a289=289, a290=290, a291=291, a292=292,
206n/a... a293=293, a294=294, a295=295, a296=296, a297=297, a298=298, a299=299)
207n/a... # doctest: +ELLIPSIS
208n/a() [('a000', 0), ('a001', 1), ('a002', 2), ..., ('a298', 298), ('a299', 299)]
209n/a
210n/a>>> class C:
211n/a... def meth(self, *args):
212n/a... return args
213n/a>>> obj = C()
214n/a>>> obj.meth(
215n/a... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
216n/a... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
217n/a... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
218n/a... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
219n/a... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
220n/a... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
221n/a... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
222n/a... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
223n/a... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
224n/a... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
225n/a... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
226n/a... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
227n/a... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
228n/a... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
229n/a... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
230n/a... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
231n/a... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
232n/a... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
233n/a... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
234n/a... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
235n/a(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299)
236n/a
237n/a>>> f(lambda x: x[0] = 3)
238n/aTraceback (most recent call last):
239n/aSyntaxError: lambda cannot contain assignment
240n/a
241n/aThe grammar accepts any test (basically, any expression) in the
242n/akeyword slot of a call site. Test a few different options.
243n/a
244n/a>>> f(x()=2)
245n/aTraceback (most recent call last):
246n/aSyntaxError: keyword can't be an expression
247n/a>>> f(a or b=1)
248n/aTraceback (most recent call last):
249n/aSyntaxError: keyword can't be an expression
250n/a>>> f(x.y=1)
251n/aTraceback (most recent call last):
252n/aSyntaxError: keyword can't be an expression
253n/a
254n/a
255n/aMore set_context():
256n/a
257n/a>>> (x for x in x) += 1
258n/aTraceback (most recent call last):
259n/aSyntaxError: can't assign to generator expression
260n/a>>> None += 1
261n/aTraceback (most recent call last):
262n/aSyntaxError: can't assign to keyword
263n/a>>> f() += 1
264n/aTraceback (most recent call last):
265n/aSyntaxError: can't assign to function call
266n/a
267n/a
268n/aTest continue in finally in weird combinations.
269n/a
270n/acontinue in for loop under finally should be ok.
271n/a
272n/a >>> def test():
273n/a ... try:
274n/a ... pass
275n/a ... finally:
276n/a ... for abc in range(10):
277n/a ... continue
278n/a ... print(abc)
279n/a >>> test()
280n/a 9
281n/a
282n/aStart simple, a continue in a finally should not be allowed.
283n/a
284n/a >>> def test():
285n/a ... for abc in range(10):
286n/a ... try:
287n/a ... pass
288n/a ... finally:
289n/a ... continue
290n/a Traceback (most recent call last):
291n/a ...
292n/a SyntaxError: 'continue' not supported inside 'finally' clause
293n/a
294n/aThis is essentially a continue in a finally which should not be allowed.
295n/a
296n/a >>> def test():
297n/a ... for abc in range(10):
298n/a ... try:
299n/a ... pass
300n/a ... finally:
301n/a ... try:
302n/a ... continue
303n/a ... except:
304n/a ... pass
305n/a Traceback (most recent call last):
306n/a ...
307n/a SyntaxError: 'continue' not supported inside 'finally' clause
308n/a
309n/a >>> def foo():
310n/a ... try:
311n/a ... pass
312n/a ... finally:
313n/a ... continue
314n/a Traceback (most recent call last):
315n/a ...
316n/a SyntaxError: 'continue' not supported inside 'finally' clause
317n/a
318n/a >>> def foo():
319n/a ... for a in ():
320n/a ... try:
321n/a ... pass
322n/a ... finally:
323n/a ... continue
324n/a Traceback (most recent call last):
325n/a ...
326n/a SyntaxError: 'continue' not supported inside 'finally' clause
327n/a
328n/a >>> def foo():
329n/a ... for a in ():
330n/a ... try:
331n/a ... pass
332n/a ... finally:
333n/a ... try:
334n/a ... continue
335n/a ... finally:
336n/a ... pass
337n/a Traceback (most recent call last):
338n/a ...
339n/a SyntaxError: 'continue' not supported inside 'finally' clause
340n/a
341n/a >>> def foo():
342n/a ... for a in ():
343n/a ... try: pass
344n/a ... finally:
345n/a ... try:
346n/a ... pass
347n/a ... except:
348n/a ... continue
349n/a Traceback (most recent call last):
350n/a ...
351n/a SyntaxError: 'continue' not supported inside 'finally' clause
352n/a
353n/aThere is one test for a break that is not in a loop. The compiler
354n/auses a single data structure to keep track of try-finally and loops,
355n/aso we need to be sure that a break is actually inside a loop. If it
356n/aisn't, there should be a syntax error.
357n/a
358n/a >>> try:
359n/a ... print(1)
360n/a ... break
361n/a ... print(2)
362n/a ... finally:
363n/a ... print(3)
364n/a Traceback (most recent call last):
365n/a ...
366n/a SyntaxError: 'break' outside loop
367n/a
368n/aThis raises a SyntaxError, it used to raise a SystemError.
369n/aContext for this change can be found on issue #27514
370n/a
371n/aIn 2.5 there was a missing exception and an assert was triggered in a debug
372n/abuild. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514
373n/a
374n/a >>> while 1:
375n/a ... while 2:
376n/a ... while 3:
377n/a ... while 4:
378n/a ... while 5:
379n/a ... while 6:
380n/a ... while 8:
381n/a ... while 9:
382n/a ... while 10:
383n/a ... while 11:
384n/a ... while 12:
385n/a ... while 13:
386n/a ... while 14:
387n/a ... while 15:
388n/a ... while 16:
389n/a ... while 17:
390n/a ... while 18:
391n/a ... while 19:
392n/a ... while 20:
393n/a ... while 21:
394n/a ... while 22:
395n/a ... break
396n/a Traceback (most recent call last):
397n/a ...
398n/a SyntaxError: too many statically nested blocks
399n/a
400n/aMisuse of the nonlocal and global statement can lead to a few unique syntax errors.
401n/a
402n/a >>> def f():
403n/a ... x = 1
404n/a ... global x
405n/a Traceback (most recent call last):
406n/a ...
407n/a SyntaxError: name 'x' is assigned to before global declaration
408n/a
409n/a >>> def f():
410n/a ... x = 1
411n/a ... def g():
412n/a ... print(x)
413n/a ... nonlocal x
414n/a Traceback (most recent call last):
415n/a ...
416n/a SyntaxError: name 'x' is used prior to nonlocal declaration
417n/a
418n/a >>> def f(x):
419n/a ... nonlocal x
420n/a Traceback (most recent call last):
421n/a ...
422n/a SyntaxError: name 'x' is parameter and nonlocal
423n/a
424n/a >>> def f():
425n/a ... global x
426n/a ... nonlocal x
427n/a Traceback (most recent call last):
428n/a ...
429n/a SyntaxError: name 'x' is nonlocal and global
430n/a
431n/a >>> def f():
432n/a ... nonlocal x
433n/a Traceback (most recent call last):
434n/a ...
435n/a SyntaxError: no binding for nonlocal 'x' found
436n/a
437n/aFrom SF bug #1705365
438n/a >>> nonlocal x
439n/a Traceback (most recent call last):
440n/a ...
441n/a SyntaxError: nonlocal declaration not allowed at module level
442n/a
443n/aTODO(jhylton): Figure out how to test SyntaxWarning with doctest.
444n/a
445n/a## >>> def f(x):
446n/a## ... def f():
447n/a## ... print(x)
448n/a## ... nonlocal x
449n/a## Traceback (most recent call last):
450n/a## ...
451n/a## SyntaxWarning: name 'x' is assigned to before nonlocal declaration
452n/a
453n/a## >>> def f():
454n/a## ... x = 1
455n/a## ... nonlocal x
456n/a## Traceback (most recent call last):
457n/a## ...
458n/a## SyntaxWarning: name 'x' is assigned to before nonlocal declaration
459n/a
460n/a From https://bugs.python.org/issue25973
461n/a >>> class A:
462n/a ... def f(self):
463n/a ... nonlocal __x
464n/a Traceback (most recent call last):
465n/a ...
466n/a SyntaxError: no binding for nonlocal '_A__x' found
467n/a
468n/a
469n/aThis tests assignment-context; there was a bug in Python 2.5 where compiling
470n/aa complex 'if' (one with 'elif') would fail to notice an invalid suite,
471n/aleading to spurious errors.
472n/a
473n/a >>> if 1:
474n/a ... x() = 1
475n/a ... elif 1:
476n/a ... pass
477n/a Traceback (most recent call last):
478n/a ...
479n/a SyntaxError: can't assign to function call
480n/a
481n/a >>> if 1:
482n/a ... pass
483n/a ... elif 1:
484n/a ... x() = 1
485n/a Traceback (most recent call last):
486n/a ...
487n/a SyntaxError: can't assign to function call
488n/a
489n/a >>> if 1:
490n/a ... x() = 1
491n/a ... elif 1:
492n/a ... pass
493n/a ... else:
494n/a ... pass
495n/a Traceback (most recent call last):
496n/a ...
497n/a SyntaxError: can't assign to function call
498n/a
499n/a >>> if 1:
500n/a ... pass
501n/a ... elif 1:
502n/a ... x() = 1
503n/a ... else:
504n/a ... pass
505n/a Traceback (most recent call last):
506n/a ...
507n/a SyntaxError: can't assign to function call
508n/a
509n/a >>> if 1:
510n/a ... pass
511n/a ... elif 1:
512n/a ... pass
513n/a ... else:
514n/a ... x() = 1
515n/a Traceback (most recent call last):
516n/a ...
517n/a SyntaxError: can't assign to function call
518n/a
519n/aMake sure that the old "raise X, Y[, Z]" form is gone:
520n/a >>> raise X, Y
521n/a Traceback (most recent call last):
522n/a ...
523n/a SyntaxError: invalid syntax
524n/a >>> raise X, Y, Z
525n/a Traceback (most recent call last):
526n/a ...
527n/a SyntaxError: invalid syntax
528n/a
529n/a
530n/a>>> f(a=23, a=234)
531n/aTraceback (most recent call last):
532n/a ...
533n/aSyntaxError: keyword argument repeated
534n/a
535n/a>>> {1, 2, 3} = 42
536n/aTraceback (most recent call last):
537n/aSyntaxError: can't assign to literal
538n/a
539n/aCorner-cases that used to fail to raise the correct error:
540n/a
541n/a >>> def f(*, x=lambda __debug__:0): pass
542n/a Traceback (most recent call last):
543n/a SyntaxError: assignment to keyword
544n/a
545n/a >>> def f(*args:(lambda __debug__:0)): pass
546n/a Traceback (most recent call last):
547n/a SyntaxError: assignment to keyword
548n/a
549n/a >>> def f(**kwargs:(lambda __debug__:0)): pass
550n/a Traceback (most recent call last):
551n/a SyntaxError: assignment to keyword
552n/a
553n/a >>> with (lambda *:0): pass
554n/a Traceback (most recent call last):
555n/a SyntaxError: named arguments must follow bare *
556n/a
557n/aCorner-cases that used to crash:
558n/a
559n/a >>> def f(**__debug__): pass
560n/a Traceback (most recent call last):
561n/a SyntaxError: assignment to keyword
562n/a
563n/a >>> def f(*xx, __debug__): pass
564n/a Traceback (most recent call last):
565n/a SyntaxError: assignment to keyword
566n/a
567n/a"""
568n/a
569n/aimport re
570n/aimport unittest
571n/aimport warnings
572n/a
573n/afrom test import support
574n/a
575n/aclass SyntaxTestCase(unittest.TestCase):
576n/a
577n/a def _check_error(self, code, errtext,
578n/a filename="<testcase>", mode="exec", subclass=None, lineno=None, offset=None):
579n/a """Check that compiling code raises SyntaxError with errtext.
580n/a
581n/a errtest is a regular expression that must be present in the
582n/a test of the exception raised. If subclass is specified it
583n/a is the expected subclass of SyntaxError (e.g. IndentationError).
584n/a """
585n/a try:
586n/a compile(code, filename, mode)
587n/a except SyntaxError as err:
588n/a if subclass and not isinstance(err, subclass):
589n/a self.fail("SyntaxError is not a %s" % subclass.__name__)
590n/a mo = re.search(errtext, str(err))
591n/a if mo is None:
592n/a self.fail("SyntaxError did not contain '%r'" % (errtext,))
593n/a self.assertEqual(err.filename, filename)
594n/a if lineno is not None:
595n/a self.assertEqual(err.lineno, lineno)
596n/a if offset is not None:
597n/a self.assertEqual(err.offset, offset)
598n/a else:
599n/a self.fail("compile() did not raise SyntaxError")
600n/a
601n/a def test_assign_call(self):
602n/a self._check_error("f() = 1", "assign")
603n/a
604n/a def test_assign_del(self):
605n/a self._check_error("del f()", "delete")
606n/a
607n/a def test_global_err_then_warn(self):
608n/a # Bug #763201: The SyntaxError raised for one global statement
609n/a # shouldn't be clobbered by a SyntaxWarning issued for a later one.
610n/a source = """if 1:
611n/a def error(a):
612n/a global a # SyntaxError
613n/a def warning():
614n/a b = 1
615n/a global b # SyntaxWarning
616n/a """
617n/a warnings.filterwarnings(action='ignore', category=SyntaxWarning)
618n/a self._check_error(source, "global")
619n/a warnings.filters.pop(0)
620n/a
621n/a def test_break_outside_loop(self):
622n/a self._check_error("break", "outside loop")
623n/a
624n/a def test_unexpected_indent(self):
625n/a self._check_error("foo()\n bar()\n", "unexpected indent",
626n/a subclass=IndentationError)
627n/a
628n/a def test_no_indent(self):
629n/a self._check_error("if 1:\nfoo()", "expected an indented block",
630n/a subclass=IndentationError)
631n/a
632n/a def test_bad_outdent(self):
633n/a self._check_error("if 1:\n foo()\n bar()",
634n/a "unindent does not match .* level",
635n/a subclass=IndentationError)
636n/a
637n/a def test_kwargs_last(self):
638n/a self._check_error("int(base=10, '2')",
639n/a "positional argument follows keyword argument")
640n/a
641n/a def test_kwargs_last2(self):
642n/a self._check_error("int(**{base: 10}, '2')",
643n/a "positional argument follows "
644n/a "keyword argument unpacking")
645n/a
646n/a def test_kwargs_last3(self):
647n/a self._check_error("int(**{base: 10}, *['2'])",
648n/a "iterable argument unpacking follows "
649n/a "keyword argument unpacking")
650n/a
651n/adef test_main():
652n/a support.run_unittest(SyntaxTestCase)
653n/a from test import test_syntax
654n/a support.run_doctest(test_syntax, verbosity=True)
655n/a
656n/aif __name__ == "__main__":
657n/a test_main()