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 | |
| 4 | from test_support import * |
| 5 | |
| 6 | print '1. Parser' |
| 7 | |
| 8 | print '1.1 Tokens' |
| 9 | |
| 10 | print '1.1.1 Backslashes' |
| 11 | |
| 12 | # Backslash means line continuation: |
| 13 | x = 1 \ |
| 14 | + 1 |
| 15 | if x <> 2: raise TestFailed, 'backslash for line continuation' |
| 16 | |
| 17 | # Backslash does not means continuation in comments :\ |
| 18 | x = 0 |
| 19 | if x <> 0: raise TestFailed, 'backslash ending comment' |
| 20 | |
| 21 | print '1.1.2 Numeric literals' |
| 22 | |
| 23 | print '1.1.2.1 Plain integers' |
| 24 | if 0xff <> 255: raise TestFailed, 'hex int' |
| 25 | if 0377 <> 255: raise TestFailed, 'octal int' |
Guido van Rossum | dd8cb44 | 1993-12-29 15:33:08 +0000 | [diff] [blame] | 26 | if 2147483647 != 017777777777: raise TestFailed, 'large positive int' |
| 27 | try: |
| 28 | from sys import maxint |
| 29 | except ImportError: |
| 30 | maxint = 2147483647 |
| 31 | if maxint == 2147483647: |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 32 | if -2147483647-1 != 020000000000: raise TestFailed, 'max negative int' |
| 33 | # XXX -2147483648 |
| 34 | if 037777777777 != -1: raise TestFailed, 'oct -1' |
| 35 | if 0xffffffff != -1: raise TestFailed, 'hex -1' |
| 36 | for s in '2147483648', '040000000000', '0x100000000': |
| 37 | try: |
| 38 | x = eval(s) |
| 39 | except OverflowError: |
| 40 | continue |
Guido van Rossum | 51b1c1c | 1995-03-04 22:30:54 +0000 | [diff] [blame] | 41 | ## raise TestFailed, \ |
| 42 | print \ |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 43 | 'No OverflowError on huge integer literal ' + `s` |
Guido van Rossum | dd8cb44 | 1993-12-29 15:33:08 +0000 | [diff] [blame] | 44 | elif eval('maxint == 9223372036854775807'): |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 45 | if eval('-9223372036854775807-1 != 01000000000000000000000'): |
Guido van Rossum | dd8cb44 | 1993-12-29 15:33:08 +0000 | [diff] [blame] | 46 | raise TestFailed, 'max negative int' |
| 47 | if eval('01777777777777777777777') != -1: raise TestFailed, 'oct -1' |
| 48 | if eval('0xffffffffffffffff') != -1: raise TestFailed, 'hex -1' |
| 49 | for s in '9223372036854775808', '02000000000000000000000', \ |
| 50 | '0x10000000000000000': |
| 51 | try: |
| 52 | x = eval(s) |
| 53 | except OverflowError: |
| 54 | continue |
| 55 | raise TestFailed, \ |
| 56 | 'No OverflowError on huge integer literal ' + `s` |
| 57 | else: |
| 58 | print 'Weird maxint value', maxint |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 59 | |
| 60 | print '1.1.2.2 Long integers' |
| 61 | x = 0L |
| 62 | x = 0l |
| 63 | x = 0xffffffffffffffffL |
| 64 | x = 0xffffffffffffffffl |
| 65 | x = 077777777777777777L |
| 66 | x = 077777777777777777l |
| 67 | x = 123456789012345678901234567890L |
| 68 | x = 123456789012345678901234567890l |
| 69 | |
| 70 | print '1.1.2.3 Floating point' |
| 71 | x = 3.14 |
| 72 | x = 314. |
| 73 | x = 0.314 |
| 74 | # XXX x = 000.314 |
| 75 | x = .314 |
| 76 | x = 3e14 |
| 77 | x = 3E14 |
| 78 | x = 3e-14 |
| 79 | x = 3e+14 |
| 80 | x = 3.e14 |
| 81 | x = .3e14 |
| 82 | x = 3.1e4 |
| 83 | |
Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 84 | print '1.1.3 String literals' |
| 85 | |
Guido van Rossum | 505043f | 1997-04-16 00:27:45 +0000 | [diff] [blame] | 86 | ##def assert(s): |
| 87 | ## if not s: raise TestFailed, 'see traceback' |
Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 88 | |
| 89 | x = ''; y = ""; assert(len(x) == 0 and x == y) |
| 90 | x = '\''; y = "'"; assert(len(x) == 1 and x == y and ord(x) == 39) |
| 91 | x = '"'; y = "\""; assert(len(x) == 1 and x == y and ord(x) == 34) |
| 92 | x = "doesn't \"shrink\" does it" |
| 93 | y = 'doesn\'t "shrink" does it' |
| 94 | assert(len(x) == 24 and x == y) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 95 | x = "does \"shrink\" doesn't it" |
| 96 | y = 'does "shrink" doesn\'t it' |
| 97 | assert(len(x) == 24 and x == y) |
| 98 | x = """ |
| 99 | The "quick" |
| 100 | brown fox |
| 101 | jumps over |
| 102 | the 'lazy' dog. |
| 103 | """ |
| 104 | y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n' |
| 105 | assert(x == y) |
| 106 | y = ''' |
| 107 | The "quick" |
| 108 | brown fox |
| 109 | jumps over |
| 110 | the 'lazy' dog. |
| 111 | '''; assert(x == y) |
| 112 | y = "\n\ |
| 113 | The \"quick\"\n\ |
| 114 | brown fox\n\ |
| 115 | jumps over\n\ |
| 116 | the 'lazy' dog.\n\ |
| 117 | "; assert(x == y) |
| 118 | y = '\n\ |
| 119 | The \"quick\"\n\ |
| 120 | brown fox\n\ |
| 121 | jumps over\n\ |
| 122 | the \'lazy\' dog.\n\ |
| 123 | '; assert(x == y) |
Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 124 | |
| 125 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 126 | print '1.2 Grammar' |
| 127 | |
| 128 | print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE |
| 129 | # XXX can't test in a script -- this rule is only used when interactive |
| 130 | |
| 131 | print 'file_input' # (NEWLINE | stmt)* ENDMARKER |
| 132 | # Being tested as this very moment this very module |
| 133 | |
| 134 | print 'expr_input' # testlist NEWLINE |
| 135 | # XXX Hard to test -- used only in calls to input() |
| 136 | |
| 137 | print 'eval_input' # testlist ENDMARKER |
| 138 | x = eval('1, 0 or 1') |
| 139 | |
| 140 | print 'funcdef' |
| 141 | ### 'def' NAME parameters ':' suite |
| 142 | ### parameters: '(' [varargslist] ')' |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 143 | ### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME] |
| 144 | ### | ('**'|'*' '*') NAME) |
| 145 | ### | fpdef ['=' test] (',' fpdef ['=' test])* [','] |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 146 | ### fpdef: NAME | '(' fplist ')' |
| 147 | ### fplist: fpdef (',' fpdef)* [','] |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 148 | ### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test) |
| 149 | ### argument: [test '='] test # Really [keyword '='] test |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 150 | def f1(): pass |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 151 | f1() |
| 152 | f1(*()) |
| 153 | f1(*(), **{}) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 154 | def f2(one_argument): pass |
| 155 | def f3(two, arguments): pass |
| 156 | def f4(two, (compound, (argument, list))): pass |
| 157 | def a1(one_arg,): pass |
| 158 | def a2(two, args,): pass |
| 159 | def v0(*rest): pass |
| 160 | def v1(a, *rest): pass |
| 161 | def v2(a, b, *rest): pass |
| 162 | def v3(a, (b, c), *rest): pass |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 163 | def d01(a=1): pass |
| 164 | d01() |
| 165 | d01(1) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 166 | d01(*(1,)) |
| 167 | d01(**{'a':2}) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 168 | def d11(a, b=1): pass |
| 169 | d11(1) |
| 170 | d11(1, 2) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 171 | d11(1, **{'b':2}) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 172 | def d21(a, b, c=1): pass |
| 173 | d21(1, 2) |
| 174 | d21(1, 2, 3) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 175 | d21(*(1, 2, 3)) |
| 176 | d21(1, *(2, 3)) |
| 177 | d21(1, 2, *(3,)) |
| 178 | d21(1, 2, **{'c':3}) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 179 | def d02(a=1, b=2): pass |
| 180 | d02() |
| 181 | d02(1) |
| 182 | d02(1, 2) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 183 | d02(*(1, 2)) |
| 184 | d02(1, *(2,)) |
| 185 | d02(1, **{'b':2}) |
| 186 | d02(**{'a': 1, 'b': 2}) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 187 | def d12(a, b=1, c=2): pass |
| 188 | d12(1) |
| 189 | d12(1, 2) |
| 190 | d12(1, 2, 3) |
| 191 | def d22(a, b, c=1, d=2): pass |
| 192 | d22(1, 2) |
| 193 | d22(1, 2, 3) |
| 194 | d22(1, 2, 3, 4) |
| 195 | def d01v(a=1, *rest): pass |
| 196 | d01v() |
| 197 | d01v(1) |
| 198 | d01v(1, 2) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 199 | d01v(*(1, 2, 3, 4)) |
| 200 | d01v(*(1,)) |
| 201 | d01v(**{'a':2}) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 202 | def d11v(a, b=1, *rest): pass |
| 203 | d11v(1) |
| 204 | d11v(1, 2) |
| 205 | d11v(1, 2, 3) |
| 206 | def d21v(a, b, c=1, *rest): pass |
| 207 | d21v(1, 2) |
| 208 | d21v(1, 2, 3) |
| 209 | d21v(1, 2, 3, 4) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 210 | d21v(*(1, 2, 3, 4)) |
| 211 | d21v(1, 2, **{'c': 3}) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 212 | def d02v(a=1, b=2, *rest): pass |
| 213 | d02v() |
| 214 | d02v(1) |
| 215 | d02v(1, 2) |
| 216 | d02v(1, 2, 3) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 217 | d02v(1, *(2, 3, 4)) |
| 218 | d02v(**{'a': 1, 'b': 2}) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 219 | def d12v(a, b=1, c=2, *rest): pass |
| 220 | d12v(1) |
| 221 | d12v(1, 2) |
| 222 | d12v(1, 2, 3) |
| 223 | d12v(1, 2, 3, 4) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 224 | d12v(*(1, 2, 3, 4)) |
| 225 | d12v(1, 2, *(3, 4, 5)) |
| 226 | d12v(1, *(2,), **{'c': 3}) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 227 | def d22v(a, b, c=1, d=2, *rest): pass |
| 228 | d22v(1, 2) |
| 229 | d22v(1, 2, 3) |
| 230 | d22v(1, 2, 3, 4) |
| 231 | d22v(1, 2, 3, 4, 5) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 232 | d22v(*(1, 2, 3, 4)) |
| 233 | d22v(1, 2, *(3, 4, 5)) |
| 234 | d22v(1, *(2, 3), **{'d': 4}) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 235 | |
| 236 | ### stmt: simple_stmt | compound_stmt |
| 237 | # Tested below |
| 238 | |
| 239 | ### simple_stmt: small_stmt (';' small_stmt)* [';'] |
| 240 | print 'simple_stmt' |
| 241 | x = 1; pass; del x |
| 242 | |
Guido van Rossum | b3b09c9 | 1993-10-22 14:24:22 +0000 | [diff] [blame] | 243 | ### 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] | 244 | # Tested below |
| 245 | |
| 246 | print 'expr_stmt' # (exprlist '=')* exprlist |
| 247 | 1 |
| 248 | 1, 2, 3 |
| 249 | x = 1 |
| 250 | x = 1, 2, 3 |
| 251 | x = y = z = 1, 2, 3 |
| 252 | x, y, z = 1, 2, 3 |
| 253 | abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4) |
| 254 | # NB these variables are deleted below |
| 255 | |
| 256 | print 'print_stmt' # 'print' (test ',')* [test] |
| 257 | print 1, 2, 3 |
| 258 | print 1, 2, 3, |
| 259 | print |
| 260 | print 0 or 1, 0 or 1, |
| 261 | print 0 or 1 |
| 262 | |
Barry Warsaw | efc92ee | 2000-08-21 15:46:50 +0000 | [diff] [blame] | 263 | print 'extended print_stmt' # 'print' '>>' test ',' |
| 264 | import sys |
| 265 | print >> sys.stdout, 1, 2, 3 |
| 266 | print >> sys.stdout, 1, 2, 3, |
| 267 | print >> sys.stdout |
| 268 | print >> sys.stdout, 0 or 1, 0 or 1, |
| 269 | print >> sys.stdout, 0 or 1 |
| 270 | |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 271 | # test print >> None |
| 272 | class Gulp: |
| 273 | def write(self, msg): pass |
| 274 | |
| 275 | def driver(): |
| 276 | oldstdout = sys.stdout |
| 277 | sys.stdout = Gulp() |
| 278 | try: |
| 279 | tellme(Gulp()) |
| 280 | tellme() |
| 281 | finally: |
| 282 | sys.stdout = oldstdout |
| 283 | |
| 284 | # we should see this once |
| 285 | def tellme(file=sys.stdout): |
| 286 | print >> file, 'hello world' |
| 287 | |
| 288 | driver() |
| 289 | |
| 290 | # we should not see this at all |
| 291 | def tellme(file=None): |
| 292 | print >> file, 'goodbye universe' |
| 293 | |
| 294 | driver() |
| 295 | |
Barry Warsaw | efc92ee | 2000-08-21 15:46:50 +0000 | [diff] [blame] | 296 | # syntax errors |
| 297 | def check_syntax(statement): |
| 298 | try: |
| 299 | compile(statement, '<string>', 'exec') |
| 300 | except SyntaxError: |
| 301 | pass |
| 302 | else: |
| 303 | print 'Missing SyntaxError: "%s"' % statement |
| 304 | check_syntax('print ,') |
| 305 | check_syntax('print >> x,') |
| 306 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 307 | print 'del_stmt' # 'del' exprlist |
| 308 | del abc |
| 309 | del x, y, (z, xyz) |
| 310 | |
| 311 | print 'pass_stmt' # 'pass' |
| 312 | pass |
| 313 | |
| 314 | print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt |
| 315 | # Tested below |
| 316 | |
| 317 | print 'break_stmt' # 'break' |
| 318 | while 1: break |
| 319 | |
| 320 | print 'continue_stmt' # 'continue' |
| 321 | i = 1 |
| 322 | while i: i = 0; continue |
| 323 | |
| 324 | print 'return_stmt' # 'return' [testlist] |
| 325 | def g1(): return |
| 326 | def g2(): return 1 |
| 327 | g1() |
| 328 | x = g2() |
| 329 | |
| 330 | print 'raise_stmt' # 'raise' test [',' test] |
| 331 | try: raise RuntimeError, 'just testing' |
| 332 | except RuntimeError: pass |
| 333 | try: raise KeyboardInterrupt |
| 334 | except KeyboardInterrupt: pass |
| 335 | |
| 336 | print 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 337 | import sys |
Guido van Rossum | 51b1c1c | 1995-03-04 22:30:54 +0000 | [diff] [blame] | 338 | import time, sys |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 339 | from time import time |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 340 | from sys import * |
Guido van Rossum | 51b1c1c | 1995-03-04 22:30:54 +0000 | [diff] [blame] | 341 | from sys import path, argv |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 342 | |
| 343 | print 'global_stmt' # 'global' NAME (',' NAME)* |
| 344 | def f(): |
| 345 | global a |
| 346 | global a, b |
| 347 | global one, two, three, four, five, six, seven, eight, nine, ten |
| 348 | |
Guido van Rossum | b3b09c9 | 1993-10-22 14:24:22 +0000 | [diff] [blame] | 349 | print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]] |
| 350 | def f(): |
| 351 | z = None |
| 352 | del z |
| 353 | exec 'z=1+1\n' |
| 354 | if z <> 2: raise TestFailed, 'exec \'z=1+1\'\\n' |
| 355 | del z |
| 356 | exec 'z=1+1' |
| 357 | if z <> 2: raise TestFailed, 'exec \'z=1+1\'' |
| 358 | f() |
| 359 | g = {} |
| 360 | exec 'z = 1' in g |
Guido van Rossum | 1f97612 | 1995-01-10 10:34:21 +0000 | [diff] [blame] | 361 | if g.has_key('__builtins__'): del g['__builtins__'] |
Guido van Rossum | b3b09c9 | 1993-10-22 14:24:22 +0000 | [diff] [blame] | 362 | if g <> {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g' |
| 363 | g = {} |
| 364 | l = {} |
| 365 | exec 'global a; a = 1; b = 2' in g, l |
Guido van Rossum | 1f97612 | 1995-01-10 10:34:21 +0000 | [diff] [blame] | 366 | if g.has_key('__builtins__'): del g['__builtins__'] |
| 367 | if l.has_key('__builtins__'): del l['__builtins__'] |
Guido van Rossum | b3b09c9 | 1993-10-22 14:24:22 +0000 | [diff] [blame] | 368 | if (g, l) <> ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g, l' |
| 369 | |
| 370 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 371 | ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef |
| 372 | # Tested below |
| 373 | |
| 374 | print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] |
| 375 | if 1: pass |
| 376 | if 1: pass |
| 377 | else: pass |
| 378 | if 0: pass |
| 379 | elif 0: pass |
| 380 | if 0: pass |
| 381 | elif 0: pass |
| 382 | elif 0: pass |
| 383 | elif 0: pass |
| 384 | else: pass |
| 385 | |
| 386 | print 'while_stmt' # 'while' test ':' suite ['else' ':' suite] |
| 387 | while 0: pass |
| 388 | while 0: pass |
| 389 | else: pass |
| 390 | |
| 391 | print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 392 | for i in 1, 2, 3: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 393 | for i, j, k in (): pass |
| 394 | else: pass |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 395 | class Squares: |
| 396 | def __init__(self, max): |
| 397 | self.max = max |
| 398 | self.sofar = [] |
| 399 | def __len__(self): return len(self.sofar) |
| 400 | def __getitem__(self, i): |
| 401 | if not 0 <= i < self.max: raise IndexError |
| 402 | n = len(self.sofar) |
| 403 | while n <= i: |
| 404 | self.sofar.append(n*n) |
| 405 | n = n+1 |
| 406 | return self.sofar[i] |
| 407 | n = 0 |
| 408 | for x in Squares(10): n = n+x |
| 409 | if n != 285: raise TestFailed, 'for over growing sequence' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 410 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 411 | print 'try_stmt' |
| 412 | ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
| 413 | ### | 'try' ':' suite 'finally' ':' suite |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 414 | ### except_clause: 'except' [expr [',' expr]] |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 415 | try: |
| 416 | 1/0 |
| 417 | except ZeroDivisionError: |
| 418 | pass |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 419 | else: |
| 420 | pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 421 | try: 1/0 |
| 422 | except EOFError: pass |
| 423 | except TypeError, msg: pass |
| 424 | except RuntimeError, msg: pass |
| 425 | except: pass |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 426 | else: pass |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 427 | try: 1/0 |
| 428 | except (EOFError, TypeError, ZeroDivisionError): pass |
| 429 | try: 1/0 |
| 430 | except (EOFError, TypeError, ZeroDivisionError), msg: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 431 | try: pass |
| 432 | finally: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 433 | |
| 434 | print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT |
| 435 | if 1: pass |
| 436 | if 1: |
| 437 | pass |
| 438 | if 1: |
| 439 | # |
| 440 | # |
| 441 | # |
| 442 | pass |
| 443 | pass |
| 444 | # |
| 445 | pass |
| 446 | # |
| 447 | |
| 448 | print 'test' |
| 449 | ### and_test ('or' and_test)* |
| 450 | ### and_test: not_test ('and' not_test)* |
| 451 | ### not_test: 'not' not_test | comparison |
| 452 | if not 1: pass |
| 453 | if 1 and 1: pass |
| 454 | if 1 or 1: pass |
| 455 | if not not not 1: pass |
| 456 | if not 1 and 1 and 1: pass |
| 457 | if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass |
| 458 | |
| 459 | print 'comparison' |
| 460 | ### comparison: expr (comp_op expr)* |
| 461 | ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' |
| 462 | if 1: pass |
| 463 | x = (1 == 1) |
| 464 | if 1 == 1: pass |
| 465 | if 1 != 1: pass |
| 466 | if 1 <> 1: pass |
| 467 | if 1 < 1: pass |
| 468 | if 1 > 1: pass |
| 469 | if 1 <= 1: pass |
| 470 | if 1 >= 1: pass |
| 471 | if 1 is 1: pass |
| 472 | if 1 is not 1: pass |
| 473 | if 1 in (): pass |
| 474 | if 1 not in (): pass |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 475 | 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] | 476 | |
| 477 | print 'binary mask ops' |
| 478 | x = 1 & 1 |
| 479 | x = 1 ^ 1 |
| 480 | x = 1 | 1 |
| 481 | |
| 482 | print 'shift ops' |
| 483 | x = 1 << 1 |
| 484 | x = 1 >> 1 |
| 485 | x = 1 << 1 >> 1 |
| 486 | |
| 487 | print 'additive ops' |
| 488 | x = 1 |
| 489 | x = 1 + 1 |
| 490 | x = 1 - 1 - 1 |
| 491 | x = 1 - 1 + 1 - 1 + 1 |
| 492 | |
| 493 | print 'multiplicative ops' |
| 494 | x = 1 * 1 |
| 495 | x = 1 / 1 |
| 496 | x = 1 % 1 |
| 497 | x = 1 / 1 * 1 % 1 |
| 498 | |
| 499 | print 'unary ops' |
| 500 | x = +1 |
| 501 | x = -1 |
| 502 | x = ~1 |
| 503 | x = ~1 ^ 1 & 1 | 1 & 1 ^ -1 |
| 504 | x = -1*1/1 + 1*1 - ---1*1 |
| 505 | |
| 506 | print 'selectors' |
| 507 | ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME |
| 508 | ### subscript: expr | [expr] ':' [expr] |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 509 | f1() |
| 510 | f2(1) |
| 511 | f2(1,) |
| 512 | f3(1, 2) |
| 513 | f3(1, 2,) |
| 514 | f4(1, (2, (3, 4))) |
| 515 | v0() |
| 516 | v0(1) |
| 517 | v0(1,) |
| 518 | v0(1,2) |
| 519 | v0(1,2,3,4,5,6,7,8,9,0) |
| 520 | v1(1) |
| 521 | v1(1,) |
| 522 | v1(1,2) |
| 523 | v1(1,2,3) |
| 524 | v1(1,2,3,4,5,6,7,8,9,0) |
| 525 | v2(1,2) |
| 526 | v2(1,2,3) |
| 527 | v2(1,2,3,4) |
| 528 | v2(1,2,3,4,5,6,7,8,9,0) |
| 529 | v3(1,(2,3)) |
| 530 | v3(1,(2,3),4) |
| 531 | v3(1,(2,3),4,5,6,7,8,9,0) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 532 | print |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 533 | import sys, time |
| 534 | c = sys.path[0] |
| 535 | x = time.time() |
| 536 | x = sys.modules['time'].time() |
| 537 | a = '01234' |
| 538 | c = a[0] |
| 539 | c = a[-1] |
| 540 | s = a[0:5] |
| 541 | s = a[:5] |
| 542 | s = a[0:] |
| 543 | s = a[:] |
| 544 | s = a[-5:] |
| 545 | s = a[:-1] |
| 546 | s = a[-4:-3] |
| 547 | |
| 548 | print 'atoms' |
| 549 | ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING |
| 550 | ### dictmaker: test ':' test (',' test ':' test)* [','] |
| 551 | |
| 552 | x = (1) |
| 553 | x = (1 or 2 or 3) |
| 554 | x = (1 or 2 or 3, 2, 3) |
| 555 | |
| 556 | x = [] |
| 557 | x = [1] |
| 558 | x = [1 or 2 or 3] |
| 559 | x = [1 or 2 or 3, 2, 3] |
| 560 | x = [] |
| 561 | |
| 562 | x = {} |
| 563 | x = {'one': 1} |
| 564 | x = {'one': 1,} |
| 565 | x = {'one' or 'two': 1 or 2} |
| 566 | x = {'one': 1, 'two': 2} |
| 567 | x = {'one': 1, 'two': 2,} |
| 568 | x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6} |
| 569 | |
| 570 | x = `x` |
| 571 | x = `1 or 2 or 3` |
| 572 | x = x |
| 573 | x = 'x' |
| 574 | x = 123 |
| 575 | |
| 576 | ### exprlist: expr (',' expr)* [','] |
| 577 | ### testlist: test (',' test)* [','] |
| 578 | # These have been exercised enough above |
| 579 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 580 | print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 581 | class B: pass |
| 582 | class C1(B): pass |
| 583 | class C2(B): pass |
| 584 | class D(C1, C2, B): pass |
| 585 | class C: |
| 586 | def meth1(self): pass |
| 587 | def meth2(self, arg): pass |
| 588 | def meth3(self, a1, a2): pass |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 589 | |
| 590 | # list comprehension tests |
| 591 | nums = [1, 2, 3, 4, 5] |
| 592 | strs = ["Apple", "Banana", "Coconut"] |
| 593 | spcs = [" Apple", " Banana ", "Coco nut "] |
| 594 | |
| 595 | print [s.strip() for s in spcs] |
| 596 | print [3 * x for x in nums] |
| 597 | print [x for x in nums if x > 2] |
| 598 | print [(i, s) for i in nums for s in strs] |
| 599 | print [(i, s) for i in nums for s in [f for f in strs if "n" in f]] |
| 600 | try: |
| 601 | eval("[i, s for i in nums for s in strs]") |
| 602 | print "FAIL: should have raised a SyntaxError!" |
| 603 | except SyntaxError: |
| 604 | print "good: got a SyntaxError as expected" |
| 605 | |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 606 | try: |
| 607 | eval("[x if y]") |
| 608 | print "FAIL: should have raised a SyntaxError!" |
| 609 | except SyntaxError: |
| 610 | print "good: got a SyntaxError as expected" |
| 611 | |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 612 | suppliers = [ |
| 613 | (1, "Boeing"), |
| 614 | (2, "Ford"), |
| 615 | (3, "Macdonalds") |
| 616 | ] |
| 617 | |
| 618 | parts = [ |
| 619 | (10, "Airliner"), |
| 620 | (20, "Engine"), |
| 621 | (30, "Cheeseburger") |
| 622 | ] |
| 623 | |
| 624 | suppart = [ |
| 625 | (1, 10), (1, 20), (2, 20), (3, 30) |
| 626 | ] |
| 627 | |
| 628 | print [ |
| 629 | (sname, pname) |
| 630 | for (sno, sname) in suppliers |
| 631 | for (pno, pname) in parts |
| 632 | for (sp_sno, sp_pno) in suppart |
| 633 | if sno == sp_sno and pno == sp_pno |
| 634 | ] |