Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1 | # Python test set -- part 1, grammar. |
| 2 | # This just tests whether the parser accepts them all. |
| 3 | |
Guido van Rossum | f0253f2 | 2002-08-29 14:57:26 +0000 | [diff] [blame] | 4 | # NOTE: When you run this test as a script from the command line, you |
| 5 | # get warnings about certain hex/oct constants. Since those are |
| 6 | # issued by the parser, you can't suppress them by adding a |
| 7 | # filterwarnings() call to this module. Therefore, to shut up the |
| 8 | # regression test, the filterwarnings() call has been added to |
| 9 | # regrtest.py. |
| 10 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 11 | from test.test_support import TestFailed, verify, vereq, check_syntax |
Jeremy Hylton | 7d3dff2 | 2001-10-10 01:45:02 +0000 | [diff] [blame] | 12 | import sys |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 13 | |
| 14 | print '1. Parser' |
| 15 | |
| 16 | print '1.1 Tokens' |
| 17 | |
| 18 | print '1.1.1 Backslashes' |
| 19 | |
| 20 | # Backslash means line continuation: |
| 21 | x = 1 \ |
| 22 | + 1 |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 23 | if x != 2: raise TestFailed, 'backslash for line continuation' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 24 | |
| 25 | # Backslash does not means continuation in comments :\ |
| 26 | x = 0 |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 27 | if x != 0: raise TestFailed, 'backslash ending comment' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 28 | |
| 29 | print '1.1.2 Numeric literals' |
| 30 | |
| 31 | print '1.1.2.1 Plain integers' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 32 | if 0xff != 255: raise TestFailed, 'hex int' |
| 33 | if 0377 != 255: raise TestFailed, 'octal int' |
Guido van Rossum | dd8cb44 | 1993-12-29 15:33:08 +0000 | [diff] [blame] | 34 | if 2147483647 != 017777777777: raise TestFailed, 'large positive int' |
| 35 | try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 36 | from sys import maxint |
Guido van Rossum | dd8cb44 | 1993-12-29 15:33:08 +0000 | [diff] [blame] | 37 | except ImportError: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 38 | maxint = 2147483647 |
Guido van Rossum | dd8cb44 | 1993-12-29 15:33:08 +0000 | [diff] [blame] | 39 | if maxint == 2147483647: |
Guido van Rossum | 66b1259 | 2003-02-12 16:57:47 +0000 | [diff] [blame] | 40 | # The following test will start to fail in Python 2.4; |
| 41 | # change the 020000000000 to -020000000000 |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 42 | if -2147483647-1 != -020000000000: raise TestFailed, 'max negative int' |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 43 | # XXX -2147483648 |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 44 | if 037777777777 < 0: raise TestFailed, 'large oct' |
| 45 | if 0xffffffff < 0: raise TestFailed, 'large hex' |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 46 | for s in '2147483648', '040000000000', '0x100000000': |
| 47 | try: |
| 48 | x = eval(s) |
| 49 | except OverflowError: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 50 | print "OverflowError on huge integer literal " + repr(s) |
Guido van Rossum | dd8cb44 | 1993-12-29 15:33:08 +0000 | [diff] [blame] | 51 | elif eval('maxint == 9223372036854775807'): |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 52 | if eval('-9223372036854775807-1 != -01000000000000000000000'): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 53 | raise TestFailed, 'max negative int' |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 54 | if eval('01777777777777777777777') < 0: raise TestFailed, 'large oct' |
| 55 | if eval('0xffffffffffffffff') < 0: raise TestFailed, 'large hex' |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 56 | for s in '9223372036854775808', '02000000000000000000000', \ |
| 57 | '0x10000000000000000': |
| 58 | try: |
| 59 | x = eval(s) |
| 60 | except OverflowError: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 61 | print "OverflowError on huge integer literal " + repr(s) |
Guido van Rossum | dd8cb44 | 1993-12-29 15:33:08 +0000 | [diff] [blame] | 62 | else: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 63 | print 'Weird maxint value', maxint |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 64 | |
| 65 | print '1.1.2.2 Long integers' |
| 66 | x = 0L |
| 67 | x = 0l |
| 68 | x = 0xffffffffffffffffL |
| 69 | x = 0xffffffffffffffffl |
| 70 | x = 077777777777777777L |
| 71 | x = 077777777777777777l |
| 72 | x = 123456789012345678901234567890L |
| 73 | x = 123456789012345678901234567890l |
| 74 | |
| 75 | print '1.1.2.3 Floating point' |
| 76 | x = 3.14 |
| 77 | x = 314. |
| 78 | x = 0.314 |
| 79 | # XXX x = 000.314 |
| 80 | x = .314 |
| 81 | x = 3e14 |
| 82 | x = 3E14 |
| 83 | x = 3e-14 |
| 84 | x = 3e+14 |
| 85 | x = 3.e14 |
| 86 | x = .3e14 |
| 87 | x = 3.1e4 |
| 88 | |
Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 89 | print '1.1.3 String literals' |
| 90 | |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 91 | x = ''; y = ""; verify(len(x) == 0 and x == y) |
| 92 | x = '\''; y = "'"; verify(len(x) == 1 and x == y and ord(x) == 39) |
| 93 | x = '"'; y = "\""; verify(len(x) == 1 and x == y and ord(x) == 34) |
Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 94 | x = "doesn't \"shrink\" does it" |
| 95 | y = 'doesn\'t "shrink" does it' |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 96 | verify(len(x) == 24 and x == y) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 97 | x = "does \"shrink\" doesn't it" |
| 98 | y = 'does "shrink" doesn\'t it' |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 99 | verify(len(x) == 24 and x == y) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 100 | x = """ |
| 101 | The "quick" |
| 102 | brown fox |
| 103 | jumps over |
| 104 | the 'lazy' dog. |
| 105 | """ |
| 106 | y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n' |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 107 | verify(x == y) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 108 | y = ''' |
| 109 | The "quick" |
| 110 | brown fox |
| 111 | jumps over |
| 112 | the 'lazy' dog. |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 113 | '''; verify(x == y) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 114 | y = "\n\ |
| 115 | The \"quick\"\n\ |
| 116 | brown fox\n\ |
| 117 | jumps over\n\ |
| 118 | the 'lazy' dog.\n\ |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 119 | "; verify(x == y) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 120 | y = '\n\ |
| 121 | The \"quick\"\n\ |
| 122 | brown fox\n\ |
| 123 | jumps over\n\ |
| 124 | the \'lazy\' dog.\n\ |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 125 | '; verify(x == y) |
Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 126 | |
| 127 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 128 | print '1.2 Grammar' |
| 129 | |
| 130 | print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE |
| 131 | # XXX can't test in a script -- this rule is only used when interactive |
| 132 | |
| 133 | print 'file_input' # (NEWLINE | stmt)* ENDMARKER |
| 134 | # Being tested as this very moment this very module |
| 135 | |
| 136 | print 'expr_input' # testlist NEWLINE |
| 137 | # XXX Hard to test -- used only in calls to input() |
| 138 | |
| 139 | print 'eval_input' # testlist ENDMARKER |
| 140 | x = eval('1, 0 or 1') |
| 141 | |
| 142 | print 'funcdef' |
| 143 | ### 'def' NAME parameters ':' suite |
| 144 | ### parameters: '(' [varargslist] ')' |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 145 | ### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME] |
| 146 | ### | ('**'|'*' '*') NAME) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 147 | ### | fpdef ['=' test] (',' fpdef ['=' test])* [','] |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 148 | ### fpdef: NAME | '(' fplist ')' |
| 149 | ### fplist: fpdef (',' fpdef)* [','] |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 150 | ### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 151 | ### argument: [test '='] test # Really [keyword '='] test |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 152 | def f1(): pass |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 153 | f1() |
| 154 | f1(*()) |
| 155 | f1(*(), **{}) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 156 | def f2(one_argument): pass |
| 157 | def f3(two, arguments): pass |
| 158 | def f4(two, (compound, (argument, list))): pass |
Jeremy Hylton | 92e9f29 | 2001-01-25 17:03:37 +0000 | [diff] [blame] | 159 | def f5((compound, first), two): pass |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 160 | vereq(f2.func_code.co_varnames, ('one_argument',)) |
| 161 | vereq(f3.func_code.co_varnames, ('two', 'arguments')) |
Jeremy Hylton | 7d3dff2 | 2001-10-10 01:45:02 +0000 | [diff] [blame] | 162 | if sys.platform.startswith('java'): |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 163 | vereq(f4.func_code.co_varnames, |
Finn Bock | 4ab7adb | 2001-12-09 09:12:34 +0000 | [diff] [blame] | 164 | ('two', '(compound, (argument, list))', 'compound', 'argument', |
| 165 | 'list',)) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 166 | vereq(f5.func_code.co_varnames, |
Jeremy Hylton | 7d3dff2 | 2001-10-10 01:45:02 +0000 | [diff] [blame] | 167 | ('(compound, first)', 'two', 'compound', 'first')) |
| 168 | else: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 169 | vereq(f4.func_code.co_varnames, |
| 170 | ('two', '.1', 'compound', 'argument', 'list')) |
| 171 | vereq(f5.func_code.co_varnames, |
| 172 | ('.0', 'two', 'compound', 'first')) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 173 | def a1(one_arg,): pass |
| 174 | def a2(two, args,): pass |
| 175 | def v0(*rest): pass |
| 176 | def v1(a, *rest): pass |
| 177 | def v2(a, b, *rest): pass |
Jeremy Hylton | 92e9f29 | 2001-01-25 17:03:37 +0000 | [diff] [blame] | 178 | def v3(a, (b, c), *rest): return a, b, c, rest |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 179 | # ceval unpacks the formal arguments into the first argcount names; |
| 180 | # thus, the names nested inside tuples must appear after these names. |
Jeremy Hylton | 7d3dff2 | 2001-10-10 01:45:02 +0000 | [diff] [blame] | 181 | if sys.platform.startswith('java'): |
| 182 | verify(v3.func_code.co_varnames == ('a', '(b, c)', 'rest', 'b', 'c')) |
| 183 | else: |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 184 | vereq(v3.func_code.co_varnames, ('a', '.1', 'rest', 'b', 'c')) |
Jeremy Hylton | 92e9f29 | 2001-01-25 17:03:37 +0000 | [diff] [blame] | 185 | verify(v3(1, (2, 3), 4) == (1, 2, 3, (4,))) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 186 | def d01(a=1): pass |
| 187 | d01() |
| 188 | d01(1) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 189 | d01(*(1,)) |
| 190 | d01(**{'a':2}) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 191 | def d11(a, b=1): pass |
| 192 | d11(1) |
| 193 | d11(1, 2) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 194 | d11(1, **{'b':2}) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 195 | def d21(a, b, c=1): pass |
| 196 | d21(1, 2) |
| 197 | d21(1, 2, 3) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 198 | d21(*(1, 2, 3)) |
| 199 | d21(1, *(2, 3)) |
| 200 | d21(1, 2, *(3,)) |
| 201 | d21(1, 2, **{'c':3}) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 202 | def d02(a=1, b=2): pass |
| 203 | d02() |
| 204 | d02(1) |
| 205 | d02(1, 2) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 206 | d02(*(1, 2)) |
| 207 | d02(1, *(2,)) |
| 208 | d02(1, **{'b':2}) |
| 209 | d02(**{'a': 1, 'b': 2}) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 210 | def d12(a, b=1, c=2): pass |
| 211 | d12(1) |
| 212 | d12(1, 2) |
| 213 | d12(1, 2, 3) |
| 214 | def d22(a, b, c=1, d=2): pass |
| 215 | d22(1, 2) |
| 216 | d22(1, 2, 3) |
| 217 | d22(1, 2, 3, 4) |
| 218 | def d01v(a=1, *rest): pass |
| 219 | d01v() |
| 220 | d01v(1) |
| 221 | d01v(1, 2) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 222 | d01v(*(1, 2, 3, 4)) |
| 223 | d01v(*(1,)) |
| 224 | d01v(**{'a':2}) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 225 | def d11v(a, b=1, *rest): pass |
| 226 | d11v(1) |
| 227 | d11v(1, 2) |
| 228 | d11v(1, 2, 3) |
| 229 | def d21v(a, b, c=1, *rest): pass |
| 230 | d21v(1, 2) |
| 231 | d21v(1, 2, 3) |
| 232 | d21v(1, 2, 3, 4) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 233 | d21v(*(1, 2, 3, 4)) |
| 234 | d21v(1, 2, **{'c': 3}) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 235 | def d02v(a=1, b=2, *rest): pass |
| 236 | d02v() |
| 237 | d02v(1) |
| 238 | d02v(1, 2) |
| 239 | d02v(1, 2, 3) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 240 | d02v(1, *(2, 3, 4)) |
| 241 | d02v(**{'a': 1, 'b': 2}) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 242 | def d12v(a, b=1, c=2, *rest): pass |
| 243 | d12v(1) |
| 244 | d12v(1, 2) |
| 245 | d12v(1, 2, 3) |
| 246 | d12v(1, 2, 3, 4) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 247 | d12v(*(1, 2, 3, 4)) |
| 248 | d12v(1, 2, *(3, 4, 5)) |
| 249 | d12v(1, *(2,), **{'c': 3}) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 250 | def d22v(a, b, c=1, d=2, *rest): pass |
| 251 | d22v(1, 2) |
| 252 | d22v(1, 2, 3) |
| 253 | d22v(1, 2, 3, 4) |
| 254 | d22v(1, 2, 3, 4, 5) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 255 | d22v(*(1, 2, 3, 4)) |
| 256 | d22v(1, 2, *(3, 4, 5)) |
| 257 | d22v(1, *(2, 3), **{'d': 4}) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 258 | |
Jeremy Hylton | 619eea6 | 2001-01-25 20:12:27 +0000 | [diff] [blame] | 259 | ### lambdef: 'lambda' [varargslist] ':' test |
| 260 | print 'lambdef' |
| 261 | l1 = lambda : 0 |
| 262 | verify(l1() == 0) |
| 263 | l2 = lambda : a[d] # XXX just testing the expression |
| 264 | l3 = lambda : [2 < x for x in [-1, 3, 0L]] |
| 265 | verify(l3() == [0, 1, 0]) |
| 266 | l4 = lambda x = lambda y = lambda z=1 : z : y() : x() |
| 267 | verify(l4() == 1) |
| 268 | l5 = lambda x, y, z=2: x + y + z |
| 269 | verify(l5(1, 2) == 5) |
| 270 | verify(l5(1, 2, 3) == 6) |
| 271 | check_syntax("lambda x: x = 2") |
| 272 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 273 | ### stmt: simple_stmt | compound_stmt |
| 274 | # Tested below |
| 275 | |
| 276 | ### simple_stmt: small_stmt (';' small_stmt)* [';'] |
| 277 | print 'simple_stmt' |
| 278 | x = 1; pass; del x |
Neal Norwitz | f8d403d | 2005-12-11 20:12:40 +0000 | [diff] [blame] | 279 | def foo(): |
| 280 | # verify statments that end with semi-colons |
| 281 | x = 1; pass; del x; |
| 282 | foo() |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 283 | |
Guido van Rossum | b3b09c9 | 1993-10-22 14:24:22 +0000 | [diff] [blame] | 284 | ### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 285 | # Tested below |
| 286 | |
| 287 | print 'expr_stmt' # (exprlist '=')* exprlist |
| 288 | 1 |
| 289 | 1, 2, 3 |
| 290 | x = 1 |
| 291 | x = 1, 2, 3 |
| 292 | x = y = z = 1, 2, 3 |
| 293 | x, y, z = 1, 2, 3 |
| 294 | abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4) |
| 295 | # NB these variables are deleted below |
| 296 | |
Jeremy Hylton | 4779399 | 2001-02-19 15:35:26 +0000 | [diff] [blame] | 297 | check_syntax("x + 1 = 1") |
| 298 | check_syntax("a + 1 = b + 2") |
| 299 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 300 | print 'print_stmt' # 'print' (test ',')* [test] |
| 301 | print 1, 2, 3 |
| 302 | print 1, 2, 3, |
| 303 | print |
| 304 | print 0 or 1, 0 or 1, |
| 305 | print 0 or 1 |
| 306 | |
Barry Warsaw | efc92ee | 2000-08-21 15:46:50 +0000 | [diff] [blame] | 307 | print 'extended print_stmt' # 'print' '>>' test ',' |
| 308 | import sys |
| 309 | print >> sys.stdout, 1, 2, 3 |
| 310 | print >> sys.stdout, 1, 2, 3, |
| 311 | print >> sys.stdout |
| 312 | print >> sys.stdout, 0 or 1, 0 or 1, |
| 313 | print >> sys.stdout, 0 or 1 |
| 314 | |
Barry Warsaw | 7e3e1c1 | 2000-10-11 21:26:03 +0000 | [diff] [blame] | 315 | # test printing to an instance |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 316 | class Gulp: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 317 | def write(self, msg): pass |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 318 | |
Barry Warsaw | 7e3e1c1 | 2000-10-11 21:26:03 +0000 | [diff] [blame] | 319 | gulp = Gulp() |
| 320 | print >> gulp, 1, 2, 3 |
| 321 | print >> gulp, 1, 2, 3, |
| 322 | print >> gulp |
| 323 | print >> gulp, 0 or 1, 0 or 1, |
| 324 | print >> gulp, 0 or 1 |
| 325 | |
| 326 | # test print >> None |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 327 | def driver(): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 328 | oldstdout = sys.stdout |
| 329 | sys.stdout = Gulp() |
| 330 | try: |
| 331 | tellme(Gulp()) |
| 332 | tellme() |
| 333 | finally: |
| 334 | sys.stdout = oldstdout |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 335 | |
| 336 | # we should see this once |
| 337 | def tellme(file=sys.stdout): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 338 | print >> file, 'hello world' |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 339 | |
| 340 | driver() |
| 341 | |
| 342 | # we should not see this at all |
| 343 | def tellme(file=None): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 344 | print >> file, 'goodbye universe' |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 345 | |
| 346 | driver() |
| 347 | |
Barry Warsaw | efc92ee | 2000-08-21 15:46:50 +0000 | [diff] [blame] | 348 | # syntax errors |
Barry Warsaw | efc92ee | 2000-08-21 15:46:50 +0000 | [diff] [blame] | 349 | check_syntax('print ,') |
| 350 | check_syntax('print >> x,') |
| 351 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 352 | print 'del_stmt' # 'del' exprlist |
| 353 | del abc |
| 354 | del x, y, (z, xyz) |
| 355 | |
| 356 | print 'pass_stmt' # 'pass' |
| 357 | pass |
| 358 | |
| 359 | print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt |
| 360 | # Tested below |
| 361 | |
| 362 | print 'break_stmt' # 'break' |
| 363 | while 1: break |
| 364 | |
| 365 | print 'continue_stmt' # 'continue' |
| 366 | i = 1 |
| 367 | while i: i = 0; continue |
| 368 | |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 369 | msg = "" |
| 370 | while not msg: |
| 371 | msg = "continue + try/except ok" |
| 372 | try: |
| 373 | continue |
| 374 | msg = "continue failed to continue inside try" |
| 375 | except: |
| 376 | msg = "continue inside try called except block" |
| 377 | print msg |
| 378 | |
| 379 | msg = "" |
| 380 | while not msg: |
| 381 | msg = "finally block not called" |
| 382 | try: |
| 383 | continue |
| 384 | finally: |
| 385 | msg = "continue + try/finally ok" |
| 386 | print msg |
Tim Peters | 10fb386 | 2001-02-09 20:17:14 +0000 | [diff] [blame] | 387 | |
Thomas Wouters | 80d373c | 2001-09-26 12:43:39 +0000 | [diff] [blame] | 388 | |
| 389 | # This test warrants an explanation. It is a test specifically for SF bugs |
| 390 | # #463359 and #462937. The bug is that a 'break' statement executed or |
| 391 | # exception raised inside a try/except inside a loop, *after* a continue |
| 392 | # statement has been executed in that loop, will cause the wrong number of |
| 393 | # arguments to be popped off the stack and the instruction pointer reset to |
| 394 | # a very small number (usually 0.) Because of this, the following test |
| 395 | # *must* written as a function, and the tracking vars *must* be function |
| 396 | # arguments with default values. Otherwise, the test will loop and loop. |
| 397 | |
| 398 | print "testing continue and break in try/except in loop" |
| 399 | def test_break_continue_loop(extra_burning_oil = 1, count=0): |
| 400 | big_hippo = 2 |
| 401 | while big_hippo: |
| 402 | count += 1 |
| 403 | try: |
| 404 | if extra_burning_oil and big_hippo == 1: |
| 405 | extra_burning_oil -= 1 |
| 406 | break |
| 407 | big_hippo -= 1 |
| 408 | continue |
| 409 | except: |
| 410 | raise |
| 411 | if count > 2 or big_hippo <> 1: |
| 412 | print "continue then break in try/except in loop broken!" |
| 413 | test_break_continue_loop() |
| 414 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 415 | print 'return_stmt' # 'return' [testlist] |
| 416 | def g1(): return |
| 417 | def g2(): return 1 |
| 418 | g1() |
| 419 | x = g2() |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 420 | check_syntax("class foo:return 1") |
| 421 | |
| 422 | print 'yield_stmt' |
| 423 | check_syntax("class foo:yield 1") |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 424 | |
| 425 | print 'raise_stmt' # 'raise' test [',' test] |
| 426 | try: raise RuntimeError, 'just testing' |
| 427 | except RuntimeError: pass |
| 428 | try: raise KeyboardInterrupt |
| 429 | except KeyboardInterrupt: pass |
| 430 | |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 431 | print 'import_name' # 'import' dotted_as_names |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 432 | import sys |
Guido van Rossum | 51b1c1c | 1995-03-04 22:30:54 +0000 | [diff] [blame] | 433 | import time, sys |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 434 | print 'import_from' # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 435 | from time import time |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 436 | from time import (time) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 437 | from sys import * |
Guido van Rossum | 51b1c1c | 1995-03-04 22:30:54 +0000 | [diff] [blame] | 438 | from sys import path, argv |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 439 | from sys import (path, argv) |
| 440 | from sys import (path, argv,) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 441 | |
| 442 | print 'global_stmt' # 'global' NAME (',' NAME)* |
| 443 | def f(): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 444 | global a |
| 445 | global a, b |
| 446 | global one, two, three, four, five, six, seven, eight, nine, ten |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 447 | |
Guido van Rossum | b3b09c9 | 1993-10-22 14:24:22 +0000 | [diff] [blame] | 448 | print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]] |
| 449 | def f(): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 450 | z = None |
| 451 | del z |
| 452 | exec 'z=1+1\n' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 453 | if z != 2: raise TestFailed, 'exec \'z=1+1\'\\n' |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 454 | del z |
| 455 | exec 'z=1+1' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 456 | if z != 2: raise TestFailed, 'exec \'z=1+1\'' |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 457 | z = None |
| 458 | del z |
Guido van Rossum | dbb718f | 2001-09-21 19:22:34 +0000 | [diff] [blame] | 459 | import types |
| 460 | if hasattr(types, "UnicodeType"): |
| 461 | exec r"""if 1: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 462 | exec u'z=1+1\n' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 463 | if z != 2: raise TestFailed, 'exec u\'z=1+1\'\\n' |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 464 | del z |
| 465 | exec u'z=1+1' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 466 | if z != 2: raise TestFailed, 'exec u\'z=1+1\'' |
Guido van Rossum | dbb718f | 2001-09-21 19:22:34 +0000 | [diff] [blame] | 467 | """ |
Guido van Rossum | b3b09c9 | 1993-10-22 14:24:22 +0000 | [diff] [blame] | 468 | f() |
| 469 | g = {} |
| 470 | exec 'z = 1' in g |
Guido van Rossum | 1f97612 | 1995-01-10 10:34:21 +0000 | [diff] [blame] | 471 | if g.has_key('__builtins__'): del g['__builtins__'] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 472 | if g != {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g' |
Guido van Rossum | b3b09c9 | 1993-10-22 14:24:22 +0000 | [diff] [blame] | 473 | g = {} |
| 474 | l = {} |
Jeremy Hylton | 2922ea8 | 2001-02-28 23:49:19 +0000 | [diff] [blame] | 475 | |
| 476 | import warnings |
| 477 | warnings.filterwarnings("ignore", "global statement", module="<string>") |
Guido van Rossum | b3b09c9 | 1993-10-22 14:24:22 +0000 | [diff] [blame] | 478 | exec 'global a; a = 1; b = 2' in g, l |
Guido van Rossum | 1f97612 | 1995-01-10 10:34:21 +0000 | [diff] [blame] | 479 | if g.has_key('__builtins__'): del g['__builtins__'] |
| 480 | if l.has_key('__builtins__'): del l['__builtins__'] |
Jeremy Hylton | e1bb5f9 | 2001-01-19 03:26:33 +0000 | [diff] [blame] | 481 | if (g, l) != ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g (%s), l (%s)' %(g,l) |
Guido van Rossum | b3b09c9 | 1993-10-22 14:24:22 +0000 | [diff] [blame] | 482 | |
| 483 | |
Jeremy Hylton | f828e2d | 2001-02-19 15:54:52 +0000 | [diff] [blame] | 484 | print "assert_stmt" # assert_stmt: 'assert' test [',' test] |
| 485 | assert 1 |
| 486 | assert 1, 1 |
| 487 | assert lambda x:x |
| 488 | assert 1, lambda x:x+1 |
| 489 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 490 | ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef |
| 491 | # Tested below |
| 492 | |
| 493 | print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] |
| 494 | if 1: pass |
| 495 | if 1: pass |
| 496 | else: pass |
| 497 | if 0: pass |
| 498 | elif 0: pass |
| 499 | if 0: pass |
| 500 | elif 0: pass |
| 501 | elif 0: pass |
| 502 | elif 0: pass |
| 503 | else: pass |
| 504 | |
| 505 | print 'while_stmt' # 'while' test ':' suite ['else' ':' suite] |
| 506 | while 0: pass |
| 507 | while 0: pass |
| 508 | else: pass |
| 509 | |
| 510 | print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 511 | for i in 1, 2, 3: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 512 | for i, j, k in (): pass |
| 513 | else: pass |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 514 | class Squares: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 515 | def __init__(self, max): |
| 516 | self.max = max |
| 517 | self.sofar = [] |
| 518 | def __len__(self): return len(self.sofar) |
| 519 | def __getitem__(self, i): |
| 520 | if not 0 <= i < self.max: raise IndexError |
| 521 | n = len(self.sofar) |
| 522 | while n <= i: |
| 523 | self.sofar.append(n*n) |
| 524 | n = n+1 |
| 525 | return self.sofar[i] |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 526 | n = 0 |
| 527 | for x in Squares(10): n = n+x |
| 528 | if n != 285: raise TestFailed, 'for over growing sequence' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 529 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 530 | print 'try_stmt' |
| 531 | ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
| 532 | ### | 'try' ':' suite 'finally' ':' suite |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 533 | ### except_clause: 'except' [expr [',' expr]] |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 534 | try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 535 | 1/0 |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 536 | except ZeroDivisionError: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 537 | pass |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 538 | else: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 539 | pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 540 | try: 1/0 |
| 541 | except EOFError: pass |
| 542 | except TypeError, msg: pass |
| 543 | except RuntimeError, msg: pass |
| 544 | except: pass |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 545 | else: pass |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 546 | try: 1/0 |
| 547 | except (EOFError, TypeError, ZeroDivisionError): pass |
| 548 | try: 1/0 |
| 549 | except (EOFError, TypeError, ZeroDivisionError), msg: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 550 | try: pass |
| 551 | finally: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 552 | |
| 553 | print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT |
| 554 | if 1: pass |
| 555 | if 1: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 556 | pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 557 | if 1: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 558 | # |
| 559 | # |
| 560 | # |
| 561 | pass |
| 562 | pass |
| 563 | # |
| 564 | pass |
| 565 | # |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 566 | |
| 567 | print 'test' |
| 568 | ### and_test ('or' and_test)* |
| 569 | ### and_test: not_test ('and' not_test)* |
| 570 | ### not_test: 'not' not_test | comparison |
| 571 | if not 1: pass |
| 572 | if 1 and 1: pass |
| 573 | if 1 or 1: pass |
| 574 | if not not not 1: pass |
| 575 | if not 1 and 1 and 1: pass |
| 576 | if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass |
| 577 | |
| 578 | print 'comparison' |
| 579 | ### comparison: expr (comp_op expr)* |
| 580 | ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' |
| 581 | if 1: pass |
| 582 | x = (1 == 1) |
| 583 | if 1 == 1: pass |
| 584 | if 1 != 1: pass |
| 585 | if 1 <> 1: pass |
| 586 | if 1 < 1: pass |
| 587 | if 1 > 1: pass |
| 588 | if 1 <= 1: pass |
| 589 | if 1 >= 1: pass |
| 590 | if 1 is 1: pass |
| 591 | if 1 is not 1: pass |
| 592 | if 1 in (): pass |
| 593 | if 1 not in (): pass |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 594 | if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 595 | |
| 596 | print 'binary mask ops' |
| 597 | x = 1 & 1 |
| 598 | x = 1 ^ 1 |
| 599 | x = 1 | 1 |
| 600 | |
| 601 | print 'shift ops' |
| 602 | x = 1 << 1 |
| 603 | x = 1 >> 1 |
| 604 | x = 1 << 1 >> 1 |
| 605 | |
| 606 | print 'additive ops' |
| 607 | x = 1 |
| 608 | x = 1 + 1 |
| 609 | x = 1 - 1 - 1 |
| 610 | x = 1 - 1 + 1 - 1 + 1 |
| 611 | |
| 612 | print 'multiplicative ops' |
| 613 | x = 1 * 1 |
| 614 | x = 1 / 1 |
| 615 | x = 1 % 1 |
| 616 | x = 1 / 1 * 1 % 1 |
| 617 | |
| 618 | print 'unary ops' |
| 619 | x = +1 |
| 620 | x = -1 |
| 621 | x = ~1 |
| 622 | x = ~1 ^ 1 & 1 | 1 & 1 ^ -1 |
| 623 | x = -1*1/1 + 1*1 - ---1*1 |
| 624 | |
| 625 | print 'selectors' |
| 626 | ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME |
| 627 | ### subscript: expr | [expr] ':' [expr] |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 628 | f1() |
| 629 | f2(1) |
| 630 | f2(1,) |
| 631 | f3(1, 2) |
| 632 | f3(1, 2,) |
| 633 | f4(1, (2, (3, 4))) |
| 634 | v0() |
| 635 | v0(1) |
| 636 | v0(1,) |
| 637 | v0(1,2) |
| 638 | v0(1,2,3,4,5,6,7,8,9,0) |
| 639 | v1(1) |
| 640 | v1(1,) |
| 641 | v1(1,2) |
| 642 | v1(1,2,3) |
| 643 | v1(1,2,3,4,5,6,7,8,9,0) |
| 644 | v2(1,2) |
| 645 | v2(1,2,3) |
| 646 | v2(1,2,3,4) |
| 647 | v2(1,2,3,4,5,6,7,8,9,0) |
| 648 | v3(1,(2,3)) |
| 649 | v3(1,(2,3),4) |
| 650 | v3(1,(2,3),4,5,6,7,8,9,0) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 651 | print |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 652 | import sys, time |
| 653 | c = sys.path[0] |
| 654 | x = time.time() |
| 655 | x = sys.modules['time'].time() |
| 656 | a = '01234' |
| 657 | c = a[0] |
| 658 | c = a[-1] |
| 659 | s = a[0:5] |
| 660 | s = a[:5] |
| 661 | s = a[0:] |
| 662 | s = a[:] |
| 663 | s = a[-5:] |
| 664 | s = a[:-1] |
| 665 | s = a[-4:-3] |
| 666 | |
| 667 | print 'atoms' |
| 668 | ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING |
| 669 | ### dictmaker: test ':' test (',' test ':' test)* [','] |
| 670 | |
| 671 | x = (1) |
| 672 | x = (1 or 2 or 3) |
| 673 | x = (1 or 2 or 3, 2, 3) |
| 674 | |
| 675 | x = [] |
| 676 | x = [1] |
| 677 | x = [1 or 2 or 3] |
| 678 | x = [1 or 2 or 3, 2, 3] |
| 679 | x = [] |
| 680 | |
| 681 | x = {} |
| 682 | x = {'one': 1} |
| 683 | x = {'one': 1,} |
| 684 | x = {'one' or 'two': 1 or 2} |
| 685 | x = {'one': 1, 'two': 2} |
| 686 | x = {'one': 1, 'two': 2,} |
| 687 | x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6} |
| 688 | |
| 689 | x = `x` |
| 690 | x = `1 or 2 or 3` |
| 691 | x = x |
| 692 | x = 'x' |
| 693 | x = 123 |
| 694 | |
| 695 | ### exprlist: expr (',' expr)* [','] |
| 696 | ### testlist: test (',' test)* [','] |
| 697 | # These have been exercised enough above |
| 698 | |
Brett Cannon | 4ebc7e3 | 2005-04-09 01:27:37 +0000 | [diff] [blame] | 699 | print 'classdef' # 'class' NAME ['(' [testlist] ')'] ':' suite |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 700 | class B: pass |
Brett Cannon | 4ebc7e3 | 2005-04-09 01:27:37 +0000 | [diff] [blame] | 701 | class B2(): pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 702 | class C1(B): pass |
| 703 | class C2(B): pass |
| 704 | class D(C1, C2, B): pass |
| 705 | class C: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 706 | def meth1(self): pass |
| 707 | def meth2(self, arg): pass |
| 708 | def meth3(self, a1, a2): pass |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 709 | |
| 710 | # list comprehension tests |
| 711 | nums = [1, 2, 3, 4, 5] |
| 712 | strs = ["Apple", "Banana", "Coconut"] |
| 713 | spcs = [" Apple", " Banana ", "Coco nut "] |
| 714 | |
| 715 | print [s.strip() for s in spcs] |
| 716 | print [3 * x for x in nums] |
| 717 | print [x for x in nums if x > 2] |
| 718 | print [(i, s) for i in nums for s in strs] |
| 719 | print [(i, s) for i in nums for s in [f for f in strs if "n" in f]] |
Tim Peters | f545baa | 2003-06-15 23:26:30 +0000 | [diff] [blame] | 720 | print [(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)] |
Jeremy Hylton | 578ceee | 2001-01-23 01:51:40 +0000 | [diff] [blame] | 721 | |
| 722 | def test_in_func(l): |
| 723 | return [None < x < 3 for x in l if x > 2] |
| 724 | |
| 725 | print test_in_func(nums) |
| 726 | |
Jeremy Hylton | e241e29 | 2001-03-19 20:42:11 +0000 | [diff] [blame] | 727 | def test_nested_front(): |
| 728 | print [[y for y in [x, x + 1]] for x in [1,3,5]] |
| 729 | |
| 730 | test_nested_front() |
| 731 | |
Jeremy Hylton | 92e9f29 | 2001-01-25 17:03:37 +0000 | [diff] [blame] | 732 | check_syntax("[i, s for i in nums for s in strs]") |
| 733 | check_syntax("[x if y]") |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 734 | |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 735 | suppliers = [ |
| 736 | (1, "Boeing"), |
| 737 | (2, "Ford"), |
| 738 | (3, "Macdonalds") |
| 739 | ] |
| 740 | |
| 741 | parts = [ |
| 742 | (10, "Airliner"), |
| 743 | (20, "Engine"), |
| 744 | (30, "Cheeseburger") |
| 745 | ] |
| 746 | |
| 747 | suppart = [ |
| 748 | (1, 10), (1, 20), (2, 20), (3, 30) |
| 749 | ] |
| 750 | |
| 751 | print [ |
| 752 | (sname, pname) |
| 753 | for (sno, sname) in suppliers |
| 754 | for (pno, pname) in parts |
| 755 | for (sp_sno, sp_pno) in suppart |
| 756 | if sno == sp_sno and pno == sp_pno |
| 757 | ] |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 758 | |
| 759 | # generator expression tests |
| 760 | g = ([x for x in range(10)] for x in range(1)) |
| 761 | verify(g.next() == [x for x in range(10)]) |
| 762 | try: |
| 763 | g.next() |
| 764 | raise TestFailed, 'should produce StopIteration exception' |
| 765 | except StopIteration: |
| 766 | pass |
| 767 | |
| 768 | a = 1 |
| 769 | try: |
| 770 | g = (a for d in a) |
| 771 | g.next() |
| 772 | raise TestFailed, 'should produce TypeError' |
| 773 | except TypeError: |
| 774 | pass |
| 775 | |
| 776 | verify(list((x, y) for x in 'abcd' for y in 'abcd') == [(x, y) for x in 'abcd' for y in 'abcd']) |
| 777 | verify(list((x, y) for x in 'ab' for y in 'xy') == [(x, y) for x in 'ab' for y in 'xy']) |
| 778 | |
| 779 | a = [x for x in range(10)] |
| 780 | b = (x for x in (y for y in a)) |
| 781 | verify(sum(b) == sum([x for x in range(10)])) |
| 782 | |
| 783 | verify(sum(x**2 for x in range(10)) == sum([x**2 for x in range(10)])) |
| 784 | verify(sum(x*x for x in range(10) if x%2) == sum([x*x for x in range(10) if x%2])) |
| 785 | verify(sum(x for x in (y for y in range(10))) == sum([x for x in range(10)])) |
| 786 | verify(sum(x for x in (y for y in (z for z in range(10)))) == sum([x for x in range(10)])) |
| 787 | verify(sum(x for x in [y for y in (z for z in range(10))]) == sum([x for x in range(10)])) |
| 788 | verify(sum(x for x in (y for y in (z for z in range(10) if True)) if True) == sum([x for x in range(10)])) |
| 789 | verify(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True) == 0) |
| 790 | check_syntax("foo(x for x in range(10), 100)") |
| 791 | check_syntax("foo(100, x for x in range(10))") |
| 792 | |
| 793 | # test for outmost iterable precomputation |
| 794 | x = 10; g = (i for i in range(x)); x = 5 |
| 795 | verify(len(list(g)) == 10) |
| 796 | |
| 797 | # This should hold, since we're only precomputing outmost iterable. |
| 798 | x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x)) |
| 799 | x = 5; t = True; |
| 800 | verify([(i,j) for i in range(10) for j in range(5)] == list(g)) |