| 1 | # Python test set -- part 1, grammar.
|
|---|
| 2 | # This just tests whether the parser accepts them all.
|
|---|
| 3 |
|
|---|
| 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 |
|
|---|
| 11 | from test.test_support import TestFailed, verify, vereq, check_syntax
|
|---|
| 12 | import sys
|
|---|
| 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
|
|---|
| 23 | if x != 2: raise TestFailed, 'backslash for line continuation'
|
|---|
| 24 |
|
|---|
| 25 | # Backslash does not means continuation in comments :\
|
|---|
| 26 | x = 0
|
|---|
| 27 | if x != 0: raise TestFailed, 'backslash ending comment'
|
|---|
| 28 |
|
|---|
| 29 | print '1.1.2 Numeric literals'
|
|---|
| 30 |
|
|---|
| 31 | print '1.1.2.1 Plain integers'
|
|---|
| 32 | if 0xff != 255: raise TestFailed, 'hex int'
|
|---|
| 33 | if 0377 != 255: raise TestFailed, 'octal int'
|
|---|
| 34 | if 2147483647 != 017777777777: raise TestFailed, 'large positive int'
|
|---|
| 35 | try:
|
|---|
| 36 | from sys import maxint
|
|---|
| 37 | except ImportError:
|
|---|
| 38 | maxint = 2147483647
|
|---|
| 39 | if maxint == 2147483647:
|
|---|
| 40 | # The following test will start to fail in Python 2.4;
|
|---|
| 41 | # change the 020000000000 to -020000000000
|
|---|
| 42 | if -2147483647-1 != -020000000000: raise TestFailed, 'max negative int'
|
|---|
| 43 | # XXX -2147483648
|
|---|
| 44 | if 037777777777 < 0: raise TestFailed, 'large oct'
|
|---|
| 45 | if 0xffffffff < 0: raise TestFailed, 'large hex'
|
|---|
| 46 | for s in '2147483648', '040000000000', '0x100000000':
|
|---|
| 47 | try:
|
|---|
| 48 | x = eval(s)
|
|---|
| 49 | except OverflowError:
|
|---|
| 50 | print "OverflowError on huge integer literal " + repr(s)
|
|---|
| 51 | elif eval('maxint == 9223372036854775807'):
|
|---|
| 52 | if eval('-9223372036854775807-1 != -01000000000000000000000'):
|
|---|
| 53 | raise TestFailed, 'max negative int'
|
|---|
| 54 | if eval('01777777777777777777777') < 0: raise TestFailed, 'large oct'
|
|---|
| 55 | if eval('0xffffffffffffffff') < 0: raise TestFailed, 'large hex'
|
|---|
| 56 | for s in '9223372036854775808', '02000000000000000000000', \
|
|---|
| 57 | '0x10000000000000000':
|
|---|
| 58 | try:
|
|---|
| 59 | x = eval(s)
|
|---|
| 60 | except OverflowError:
|
|---|
| 61 | print "OverflowError on huge integer literal " + repr(s)
|
|---|
| 62 | else:
|
|---|
| 63 | print 'Weird maxint value', maxint
|
|---|
| 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 |
|
|---|
| 89 | print '1.1.3 String literals'
|
|---|
| 90 |
|
|---|
| 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)
|
|---|
| 94 | x = "doesn't \"shrink\" does it"
|
|---|
| 95 | y = 'doesn\'t "shrink" does it'
|
|---|
| 96 | verify(len(x) == 24 and x == y)
|
|---|
| 97 | x = "does \"shrink\" doesn't it"
|
|---|
| 98 | y = 'does "shrink" doesn\'t it'
|
|---|
| 99 | verify(len(x) == 24 and x == y)
|
|---|
| 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'
|
|---|
| 107 | verify(x == y)
|
|---|
| 108 | y = '''
|
|---|
| 109 | The "quick"
|
|---|
| 110 | brown fox
|
|---|
| 111 | jumps over
|
|---|
| 112 | the 'lazy' dog.
|
|---|
| 113 | '''; verify(x == y)
|
|---|
| 114 | y = "\n\
|
|---|
| 115 | The \"quick\"\n\
|
|---|
| 116 | brown fox\n\
|
|---|
| 117 | jumps over\n\
|
|---|
| 118 | the 'lazy' dog.\n\
|
|---|
| 119 | "; verify(x == y)
|
|---|
| 120 | y = '\n\
|
|---|
| 121 | The \"quick\"\n\
|
|---|
| 122 | brown fox\n\
|
|---|
| 123 | jumps over\n\
|
|---|
| 124 | the \'lazy\' dog.\n\
|
|---|
| 125 | '; verify(x == y)
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 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] ')'
|
|---|
| 145 | ### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME]
|
|---|
| 146 | ### | ('**'|'*' '*') NAME)
|
|---|
| 147 | ### | fpdef ['=' test] (',' fpdef ['=' test])* [',']
|
|---|
| 148 | ### fpdef: NAME | '(' fplist ')'
|
|---|
| 149 | ### fplist: fpdef (',' fpdef)* [',']
|
|---|
| 150 | ### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test)
|
|---|
| 151 | ### argument: [test '='] test # Really [keyword '='] test
|
|---|
| 152 | def f1(): pass
|
|---|
| 153 | f1()
|
|---|
| 154 | f1(*())
|
|---|
| 155 | f1(*(), **{})
|
|---|
| 156 | def f2(one_argument): pass
|
|---|
| 157 | def f3(two, arguments): pass
|
|---|
| 158 | def f4(two, (compound, (argument, list))): pass
|
|---|
| 159 | def f5((compound, first), two): pass
|
|---|
| 160 | vereq(f2.func_code.co_varnames, ('one_argument',))
|
|---|
| 161 | vereq(f3.func_code.co_varnames, ('two', 'arguments'))
|
|---|
| 162 | if sys.platform.startswith('java'):
|
|---|
| 163 | vereq(f4.func_code.co_varnames,
|
|---|
| 164 | ('two', '(compound, (argument, list))', 'compound', 'argument',
|
|---|
| 165 | 'list',))
|
|---|
| 166 | vereq(f5.func_code.co_varnames,
|
|---|
| 167 | ('(compound, first)', 'two', 'compound', 'first'))
|
|---|
| 168 | else:
|
|---|
| 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'))
|
|---|
| 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
|
|---|
| 178 | def v3(a, (b, c), *rest): return a, b, c, rest
|
|---|
| 179 | # ceval unpacks the formal arguments into the first argcount names;
|
|---|
| 180 | # thus, the names nested inside tuples must appear after these names.
|
|---|
| 181 | if sys.platform.startswith('java'):
|
|---|
| 182 | verify(v3.func_code.co_varnames == ('a', '(b, c)', 'rest', 'b', 'c'))
|
|---|
| 183 | else:
|
|---|
| 184 | vereq(v3.func_code.co_varnames, ('a', '.1', 'rest', 'b', 'c'))
|
|---|
| 185 | verify(v3(1, (2, 3), 4) == (1, 2, 3, (4,)))
|
|---|
| 186 | def d01(a=1): pass
|
|---|
| 187 | d01()
|
|---|
| 188 | d01(1)
|
|---|
| 189 | d01(*(1,))
|
|---|
| 190 | d01(**{'a':2})
|
|---|
| 191 | def d11(a, b=1): pass
|
|---|
| 192 | d11(1)
|
|---|
| 193 | d11(1, 2)
|
|---|
| 194 | d11(1, **{'b':2})
|
|---|
| 195 | def d21(a, b, c=1): pass
|
|---|
| 196 | d21(1, 2)
|
|---|
| 197 | d21(1, 2, 3)
|
|---|
| 198 | d21(*(1, 2, 3))
|
|---|
| 199 | d21(1, *(2, 3))
|
|---|
| 200 | d21(1, 2, *(3,))
|
|---|
| 201 | d21(1, 2, **{'c':3})
|
|---|
| 202 | def d02(a=1, b=2): pass
|
|---|
| 203 | d02()
|
|---|
| 204 | d02(1)
|
|---|
| 205 | d02(1, 2)
|
|---|
| 206 | d02(*(1, 2))
|
|---|
| 207 | d02(1, *(2,))
|
|---|
| 208 | d02(1, **{'b':2})
|
|---|
| 209 | d02(**{'a': 1, 'b': 2})
|
|---|
| 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)
|
|---|
| 222 | d01v(*(1, 2, 3, 4))
|
|---|
| 223 | d01v(*(1,))
|
|---|
| 224 | d01v(**{'a':2})
|
|---|
| 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)
|
|---|
| 233 | d21v(*(1, 2, 3, 4))
|
|---|
| 234 | d21v(1, 2, **{'c': 3})
|
|---|
| 235 | def d02v(a=1, b=2, *rest): pass
|
|---|
| 236 | d02v()
|
|---|
| 237 | d02v(1)
|
|---|
| 238 | d02v(1, 2)
|
|---|
| 239 | d02v(1, 2, 3)
|
|---|
| 240 | d02v(1, *(2, 3, 4))
|
|---|
| 241 | d02v(**{'a': 1, 'b': 2})
|
|---|
| 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)
|
|---|
| 247 | d12v(*(1, 2, 3, 4))
|
|---|
| 248 | d12v(1, 2, *(3, 4, 5))
|
|---|
| 249 | d12v(1, *(2,), **{'c': 3})
|
|---|
| 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)
|
|---|
| 255 | d22v(*(1, 2, 3, 4))
|
|---|
| 256 | d22v(1, 2, *(3, 4, 5))
|
|---|
| 257 | d22v(1, *(2, 3), **{'d': 4})
|
|---|
| 258 | def d31v((x)): pass
|
|---|
| 259 | d31v(1)
|
|---|
| 260 | def d32v((x,)): pass
|
|---|
| 261 | d32v((1,))
|
|---|
| 262 |
|
|---|
| 263 | ### lambdef: 'lambda' [varargslist] ':' test
|
|---|
| 264 | print 'lambdef'
|
|---|
| 265 | l1 = lambda : 0
|
|---|
| 266 | verify(l1() == 0)
|
|---|
| 267 | l2 = lambda : a[d] # XXX just testing the expression
|
|---|
| 268 | l3 = lambda : [2 < x for x in [-1, 3, 0L]]
|
|---|
| 269 | verify(l3() == [0, 1, 0])
|
|---|
| 270 | l4 = lambda x = lambda y = lambda z=1 : z : y() : x()
|
|---|
| 271 | verify(l4() == 1)
|
|---|
| 272 | l5 = lambda x, y, z=2: x + y + z
|
|---|
| 273 | verify(l5(1, 2) == 5)
|
|---|
| 274 | verify(l5(1, 2, 3) == 6)
|
|---|
| 275 | check_syntax("lambda x: x = 2")
|
|---|
| 276 |
|
|---|
| 277 | ### stmt: simple_stmt | compound_stmt
|
|---|
| 278 | # Tested below
|
|---|
| 279 |
|
|---|
| 280 | ### simple_stmt: small_stmt (';' small_stmt)* [';']
|
|---|
| 281 | print 'simple_stmt'
|
|---|
| 282 | x = 1; pass; del x
|
|---|
| 283 | def foo():
|
|---|
| 284 | # verify statments that end with semi-colons
|
|---|
| 285 | x = 1; pass; del x;
|
|---|
| 286 | foo()
|
|---|
| 287 |
|
|---|
| 288 | ### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt
|
|---|
| 289 | # Tested below
|
|---|
| 290 |
|
|---|
| 291 | print 'expr_stmt' # (exprlist '=')* exprlist
|
|---|
| 292 | 1
|
|---|
| 293 | 1, 2, 3
|
|---|
| 294 | x = 1
|
|---|
| 295 | x = 1, 2, 3
|
|---|
| 296 | x = y = z = 1, 2, 3
|
|---|
| 297 | x, y, z = 1, 2, 3
|
|---|
| 298 | abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
|
|---|
| 299 | # NB these variables are deleted below
|
|---|
| 300 |
|
|---|
| 301 | check_syntax("x + 1 = 1")
|
|---|
| 302 | check_syntax("a + 1 = b + 2")
|
|---|
| 303 |
|
|---|
| 304 | print 'print_stmt' # 'print' (test ',')* [test]
|
|---|
| 305 | print 1, 2, 3
|
|---|
| 306 | print 1, 2, 3,
|
|---|
| 307 | print
|
|---|
| 308 | print 0 or 1, 0 or 1,
|
|---|
| 309 | print 0 or 1
|
|---|
| 310 |
|
|---|
| 311 | print 'extended print_stmt' # 'print' '>>' test ','
|
|---|
| 312 | import sys
|
|---|
| 313 | print >> sys.stdout, 1, 2, 3
|
|---|
| 314 | print >> sys.stdout, 1, 2, 3,
|
|---|
| 315 | print >> sys.stdout
|
|---|
| 316 | print >> sys.stdout, 0 or 1, 0 or 1,
|
|---|
| 317 | print >> sys.stdout, 0 or 1
|
|---|
| 318 |
|
|---|
| 319 | # test printing to an instance
|
|---|
| 320 | class Gulp:
|
|---|
| 321 | def write(self, msg): pass
|
|---|
| 322 |
|
|---|
| 323 | gulp = Gulp()
|
|---|
| 324 | print >> gulp, 1, 2, 3
|
|---|
| 325 | print >> gulp, 1, 2, 3,
|
|---|
| 326 | print >> gulp
|
|---|
| 327 | print >> gulp, 0 or 1, 0 or 1,
|
|---|
| 328 | print >> gulp, 0 or 1
|
|---|
| 329 |
|
|---|
| 330 | # test print >> None
|
|---|
| 331 | def driver():
|
|---|
| 332 | oldstdout = sys.stdout
|
|---|
| 333 | sys.stdout = Gulp()
|
|---|
| 334 | try:
|
|---|
| 335 | tellme(Gulp())
|
|---|
| 336 | tellme()
|
|---|
| 337 | finally:
|
|---|
| 338 | sys.stdout = oldstdout
|
|---|
| 339 |
|
|---|
| 340 | # we should see this once
|
|---|
| 341 | def tellme(file=sys.stdout):
|
|---|
| 342 | print >> file, 'hello world'
|
|---|
| 343 |
|
|---|
| 344 | driver()
|
|---|
| 345 |
|
|---|
| 346 | # we should not see this at all
|
|---|
| 347 | def tellme(file=None):
|
|---|
| 348 | print >> file, 'goodbye universe'
|
|---|
| 349 |
|
|---|
| 350 | driver()
|
|---|
| 351 |
|
|---|
| 352 | # syntax errors
|
|---|
| 353 | check_syntax('print ,')
|
|---|
| 354 | check_syntax('print >> x,')
|
|---|
| 355 |
|
|---|
| 356 | print 'del_stmt' # 'del' exprlist
|
|---|
| 357 | del abc
|
|---|
| 358 | del x, y, (z, xyz)
|
|---|
| 359 |
|
|---|
| 360 | print 'pass_stmt' # 'pass'
|
|---|
| 361 | pass
|
|---|
| 362 |
|
|---|
| 363 | print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt
|
|---|
| 364 | # Tested below
|
|---|
| 365 |
|
|---|
| 366 | print 'break_stmt' # 'break'
|
|---|
| 367 | while 1: break
|
|---|
| 368 |
|
|---|
| 369 | print 'continue_stmt' # 'continue'
|
|---|
| 370 | i = 1
|
|---|
| 371 | while i: i = 0; continue
|
|---|
| 372 |
|
|---|
| 373 | msg = ""
|
|---|
| 374 | while not msg:
|
|---|
| 375 | msg = "continue + try/except ok"
|
|---|
| 376 | try:
|
|---|
| 377 | continue
|
|---|
| 378 | msg = "continue failed to continue inside try"
|
|---|
| 379 | except:
|
|---|
| 380 | msg = "continue inside try called except block"
|
|---|
| 381 | print msg
|
|---|
| 382 |
|
|---|
| 383 | msg = ""
|
|---|
| 384 | while not msg:
|
|---|
| 385 | msg = "finally block not called"
|
|---|
| 386 | try:
|
|---|
| 387 | continue
|
|---|
| 388 | finally:
|
|---|
| 389 | msg = "continue + try/finally ok"
|
|---|
| 390 | print msg
|
|---|
| 391 |
|
|---|
| 392 |
|
|---|
| 393 | # This test warrants an explanation. It is a test specifically for SF bugs
|
|---|
| 394 | # #463359 and #462937. The bug is that a 'break' statement executed or
|
|---|
| 395 | # exception raised inside a try/except inside a loop, *after* a continue
|
|---|
| 396 | # statement has been executed in that loop, will cause the wrong number of
|
|---|
| 397 | # arguments to be popped off the stack and the instruction pointer reset to
|
|---|
| 398 | # a very small number (usually 0.) Because of this, the following test
|
|---|
| 399 | # *must* written as a function, and the tracking vars *must* be function
|
|---|
| 400 | # arguments with default values. Otherwise, the test will loop and loop.
|
|---|
| 401 |
|
|---|
| 402 | print "testing continue and break in try/except in loop"
|
|---|
| 403 | def test_break_continue_loop(extra_burning_oil = 1, count=0):
|
|---|
| 404 | big_hippo = 2
|
|---|
| 405 | while big_hippo:
|
|---|
| 406 | count += 1
|
|---|
| 407 | try:
|
|---|
| 408 | if extra_burning_oil and big_hippo == 1:
|
|---|
| 409 | extra_burning_oil -= 1
|
|---|
| 410 | break
|
|---|
| 411 | big_hippo -= 1
|
|---|
| 412 | continue
|
|---|
| 413 | except:
|
|---|
| 414 | raise
|
|---|
| 415 | if count > 2 or big_hippo <> 1:
|
|---|
| 416 | print "continue then break in try/except in loop broken!"
|
|---|
| 417 | test_break_continue_loop()
|
|---|
| 418 |
|
|---|
| 419 | print 'return_stmt' # 'return' [testlist]
|
|---|
| 420 | def g1(): return
|
|---|
| 421 | def g2(): return 1
|
|---|
| 422 | g1()
|
|---|
| 423 | x = g2()
|
|---|
| 424 | check_syntax("class foo:return 1")
|
|---|
| 425 |
|
|---|
| 426 | print 'yield_stmt'
|
|---|
| 427 | check_syntax("class foo:yield 1")
|
|---|
| 428 |
|
|---|
| 429 | print 'raise_stmt' # 'raise' test [',' test]
|
|---|
| 430 | try: raise RuntimeError, 'just testing'
|
|---|
| 431 | except RuntimeError: pass
|
|---|
| 432 | try: raise KeyboardInterrupt
|
|---|
| 433 | except KeyboardInterrupt: pass
|
|---|
| 434 |
|
|---|
| 435 | print 'import_name' # 'import' dotted_as_names
|
|---|
| 436 | import sys
|
|---|
| 437 | import time, sys
|
|---|
| 438 | print 'import_from' # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names)
|
|---|
| 439 | from time import time
|
|---|
| 440 | from time import (time)
|
|---|
| 441 | from sys import *
|
|---|
| 442 | from sys import path, argv
|
|---|
| 443 | from sys import (path, argv)
|
|---|
| 444 | from sys import (path, argv,)
|
|---|
| 445 |
|
|---|
| 446 | print 'global_stmt' # 'global' NAME (',' NAME)*
|
|---|
| 447 | def f():
|
|---|
| 448 | global a
|
|---|
| 449 | global a, b
|
|---|
| 450 | global one, two, three, four, five, six, seven, eight, nine, ten
|
|---|
| 451 |
|
|---|
| 452 | print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]]
|
|---|
| 453 | def f():
|
|---|
| 454 | z = None
|
|---|
| 455 | del z
|
|---|
| 456 | exec 'z=1+1\n'
|
|---|
| 457 | if z != 2: raise TestFailed, 'exec \'z=1+1\'\\n'
|
|---|
| 458 | del z
|
|---|
| 459 | exec 'z=1+1'
|
|---|
| 460 | if z != 2: raise TestFailed, 'exec \'z=1+1\''
|
|---|
| 461 | z = None
|
|---|
| 462 | del z
|
|---|
| 463 | import types
|
|---|
| 464 | if hasattr(types, "UnicodeType"):
|
|---|
| 465 | exec r"""if 1:
|
|---|
| 466 | exec u'z=1+1\n'
|
|---|
| 467 | if z != 2: raise TestFailed, 'exec u\'z=1+1\'\\n'
|
|---|
| 468 | del z
|
|---|
| 469 | exec u'z=1+1'
|
|---|
| 470 | if z != 2: raise TestFailed, 'exec u\'z=1+1\''
|
|---|
| 471 | """
|
|---|
| 472 | f()
|
|---|
| 473 | g = {}
|
|---|
| 474 | exec 'z = 1' in g
|
|---|
| 475 | if g.has_key('__builtins__'): del g['__builtins__']
|
|---|
| 476 | if g != {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g'
|
|---|
| 477 | g = {}
|
|---|
| 478 | l = {}
|
|---|
| 479 |
|
|---|
| 480 | import warnings
|
|---|
| 481 | warnings.filterwarnings("ignore", "global statement", module="<string>")
|
|---|
| 482 | exec 'global a; a = 1; b = 2' in g, l
|
|---|
| 483 | if g.has_key('__builtins__'): del g['__builtins__']
|
|---|
| 484 | if l.has_key('__builtins__'): del l['__builtins__']
|
|---|
| 485 | if (g, l) != ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g (%s), l (%s)' %(g,l)
|
|---|
| 486 |
|
|---|
| 487 |
|
|---|
| 488 | print "assert_stmt" # assert_stmt: 'assert' test [',' test]
|
|---|
| 489 | assert 1
|
|---|
| 490 | assert 1, 1
|
|---|
| 491 | assert lambda x:x
|
|---|
| 492 | assert 1, lambda x:x+1
|
|---|
| 493 |
|
|---|
| 494 | ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
|
|---|
| 495 | # Tested below
|
|---|
| 496 |
|
|---|
| 497 | print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
|
|---|
| 498 | if 1: pass
|
|---|
| 499 | if 1: pass
|
|---|
| 500 | else: pass
|
|---|
| 501 | if 0: pass
|
|---|
| 502 | elif 0: pass
|
|---|
| 503 | if 0: pass
|
|---|
| 504 | elif 0: pass
|
|---|
| 505 | elif 0: pass
|
|---|
| 506 | elif 0: pass
|
|---|
| 507 | else: pass
|
|---|
| 508 |
|
|---|
| 509 | print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
|
|---|
| 510 | while 0: pass
|
|---|
| 511 | while 0: pass
|
|---|
| 512 | else: pass
|
|---|
| 513 |
|
|---|
| 514 | print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
|
|---|
| 515 | for i in 1, 2, 3: pass
|
|---|
| 516 | for i, j, k in (): pass
|
|---|
| 517 | else: pass
|
|---|
| 518 | class Squares:
|
|---|
| 519 | def __init__(self, max):
|
|---|
| 520 | self.max = max
|
|---|
| 521 | self.sofar = []
|
|---|
| 522 | def __len__(self): return len(self.sofar)
|
|---|
| 523 | def __getitem__(self, i):
|
|---|
| 524 | if not 0 <= i < self.max: raise IndexError
|
|---|
| 525 | n = len(self.sofar)
|
|---|
| 526 | while n <= i:
|
|---|
| 527 | self.sofar.append(n*n)
|
|---|
| 528 | n = n+1
|
|---|
| 529 | return self.sofar[i]
|
|---|
| 530 | n = 0
|
|---|
| 531 | for x in Squares(10): n = n+x
|
|---|
| 532 | if n != 285: raise TestFailed, 'for over growing sequence'
|
|---|
| 533 |
|
|---|
| 534 | result = []
|
|---|
| 535 | for x, in [(1,), (2,), (3,)]:
|
|---|
| 536 | result.append(x)
|
|---|
| 537 | vereq(result, [1, 2, 3])
|
|---|
| 538 |
|
|---|
| 539 | print 'try_stmt'
|
|---|
| 540 | ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
|
|---|
| 541 | ### | 'try' ':' suite 'finally' ':' suite
|
|---|
| 542 | ### except_clause: 'except' [expr [',' expr]]
|
|---|
| 543 | try:
|
|---|
| 544 | 1/0
|
|---|
| 545 | except ZeroDivisionError:
|
|---|
| 546 | pass
|
|---|
| 547 | else:
|
|---|
| 548 | pass
|
|---|
| 549 | try: 1/0
|
|---|
| 550 | except EOFError: pass
|
|---|
| 551 | except TypeError, msg: pass
|
|---|
| 552 | except RuntimeError, msg: pass
|
|---|
| 553 | except: pass
|
|---|
| 554 | else: pass
|
|---|
| 555 | try: 1/0
|
|---|
| 556 | except (EOFError, TypeError, ZeroDivisionError): pass
|
|---|
| 557 | try: 1/0
|
|---|
| 558 | except (EOFError, TypeError, ZeroDivisionError), msg: pass
|
|---|
| 559 | try: pass
|
|---|
| 560 | finally: pass
|
|---|
| 561 |
|
|---|
| 562 | print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
|
|---|
| 563 | if 1: pass
|
|---|
| 564 | if 1:
|
|---|
| 565 | pass
|
|---|
| 566 | if 1:
|
|---|
| 567 | #
|
|---|
| 568 | #
|
|---|
| 569 | #
|
|---|
| 570 | pass
|
|---|
| 571 | pass
|
|---|
| 572 | #
|
|---|
| 573 | pass
|
|---|
| 574 | #
|
|---|
| 575 |
|
|---|
| 576 | print 'test'
|
|---|
| 577 | ### and_test ('or' and_test)*
|
|---|
| 578 | ### and_test: not_test ('and' not_test)*
|
|---|
| 579 | ### not_test: 'not' not_test | comparison
|
|---|
| 580 | if not 1: pass
|
|---|
| 581 | if 1 and 1: pass
|
|---|
| 582 | if 1 or 1: pass
|
|---|
| 583 | if not not not 1: pass
|
|---|
| 584 | if not 1 and 1 and 1: pass
|
|---|
| 585 | if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
|
|---|
| 586 |
|
|---|
| 587 | print 'comparison'
|
|---|
| 588 | ### comparison: expr (comp_op expr)*
|
|---|
| 589 | ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
|
|---|
| 590 | if 1: pass
|
|---|
| 591 | x = (1 == 1)
|
|---|
| 592 | if 1 == 1: pass
|
|---|
| 593 | if 1 != 1: pass
|
|---|
| 594 | if 1 <> 1: pass
|
|---|
| 595 | if 1 < 1: pass
|
|---|
| 596 | if 1 > 1: pass
|
|---|
| 597 | if 1 <= 1: pass
|
|---|
| 598 | if 1 >= 1: pass
|
|---|
| 599 | if 1 is 1: pass
|
|---|
| 600 | if 1 is not 1: pass
|
|---|
| 601 | if 1 in (): pass
|
|---|
| 602 | if 1 not in (): pass
|
|---|
| 603 | if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1: pass
|
|---|
| 604 |
|
|---|
| 605 | print 'binary mask ops'
|
|---|
| 606 | x = 1 & 1
|
|---|
| 607 | x = 1 ^ 1
|
|---|
| 608 | x = 1 | 1
|
|---|
| 609 |
|
|---|
| 610 | print 'shift ops'
|
|---|
| 611 | x = 1 << 1
|
|---|
| 612 | x = 1 >> 1
|
|---|
| 613 | x = 1 << 1 >> 1
|
|---|
| 614 |
|
|---|
| 615 | print 'additive ops'
|
|---|
| 616 | x = 1
|
|---|
| 617 | x = 1 + 1
|
|---|
| 618 | x = 1 - 1 - 1
|
|---|
| 619 | x = 1 - 1 + 1 - 1 + 1
|
|---|
| 620 |
|
|---|
| 621 | print 'multiplicative ops'
|
|---|
| 622 | x = 1 * 1
|
|---|
| 623 | x = 1 / 1
|
|---|
| 624 | x = 1 % 1
|
|---|
| 625 | x = 1 / 1 * 1 % 1
|
|---|
| 626 |
|
|---|
| 627 | print 'unary ops'
|
|---|
| 628 | x = +1
|
|---|
| 629 | x = -1
|
|---|
| 630 | x = ~1
|
|---|
| 631 | x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
|
|---|
| 632 | x = -1*1/1 + 1*1 - ---1*1
|
|---|
| 633 |
|
|---|
| 634 | print 'selectors'
|
|---|
| 635 | ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
|
|---|
| 636 | ### subscript: expr | [expr] ':' [expr]
|
|---|
| 637 | f1()
|
|---|
| 638 | f2(1)
|
|---|
| 639 | f2(1,)
|
|---|
| 640 | f3(1, 2)
|
|---|
| 641 | f3(1, 2,)
|
|---|
| 642 | f4(1, (2, (3, 4)))
|
|---|
| 643 | v0()
|
|---|
| 644 | v0(1)
|
|---|
| 645 | v0(1,)
|
|---|
| 646 | v0(1,2)
|
|---|
| 647 | v0(1,2,3,4,5,6,7,8,9,0)
|
|---|
| 648 | v1(1)
|
|---|
| 649 | v1(1,)
|
|---|
| 650 | v1(1,2)
|
|---|
| 651 | v1(1,2,3)
|
|---|
| 652 | v1(1,2,3,4,5,6,7,8,9,0)
|
|---|
| 653 | v2(1,2)
|
|---|
| 654 | v2(1,2,3)
|
|---|
| 655 | v2(1,2,3,4)
|
|---|
| 656 | v2(1,2,3,4,5,6,7,8,9,0)
|
|---|
| 657 | v3(1,(2,3))
|
|---|
| 658 | v3(1,(2,3),4)
|
|---|
| 659 | v3(1,(2,3),4,5,6,7,8,9,0)
|
|---|
| 660 | print
|
|---|
| 661 | import sys, time
|
|---|
| 662 | c = sys.path[0]
|
|---|
| 663 | x = time.time()
|
|---|
| 664 | x = sys.modules['time'].time()
|
|---|
| 665 | a = '01234'
|
|---|
| 666 | c = a[0]
|
|---|
| 667 | c = a[-1]
|
|---|
| 668 | s = a[0:5]
|
|---|
| 669 | s = a[:5]
|
|---|
| 670 | s = a[0:]
|
|---|
| 671 | s = a[:]
|
|---|
| 672 | s = a[-5:]
|
|---|
| 673 | s = a[:-1]
|
|---|
| 674 | s = a[-4:-3]
|
|---|
| 675 | # A rough test of SF bug 1333982. http://python.org/sf/1333982
|
|---|
| 676 | # The testing here is fairly incomplete.
|
|---|
| 677 | # Test cases should include: commas with 1 and 2 colons
|
|---|
| 678 | d = {}
|
|---|
| 679 | d[1] = 1
|
|---|
| 680 | d[1,] = 2
|
|---|
| 681 | d[1,2] = 3
|
|---|
| 682 | d[1,2,3] = 4
|
|---|
| 683 | L = list(d)
|
|---|
| 684 | L.sort()
|
|---|
| 685 | print L
|
|---|
| 686 |
|
|---|
| 687 |
|
|---|
| 688 | print 'atoms'
|
|---|
| 689 | ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
|
|---|
| 690 | ### dictmaker: test ':' test (',' test ':' test)* [',']
|
|---|
| 691 |
|
|---|
| 692 | x = (1)
|
|---|
| 693 | x = (1 or 2 or 3)
|
|---|
| 694 | x = (1 or 2 or 3, 2, 3)
|
|---|
| 695 |
|
|---|
| 696 | x = []
|
|---|
| 697 | x = [1]
|
|---|
| 698 | x = [1 or 2 or 3]
|
|---|
| 699 | x = [1 or 2 or 3, 2, 3]
|
|---|
| 700 | x = []
|
|---|
| 701 |
|
|---|
| 702 | x = {}
|
|---|
| 703 | x = {'one': 1}
|
|---|
| 704 | x = {'one': 1,}
|
|---|
| 705 | x = {'one' or 'two': 1 or 2}
|
|---|
| 706 | x = {'one': 1, 'two': 2}
|
|---|
| 707 | x = {'one': 1, 'two': 2,}
|
|---|
| 708 | x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
|
|---|
| 709 |
|
|---|
| 710 | x = `x`
|
|---|
| 711 | x = `1 or 2 or 3`
|
|---|
| 712 | x = x
|
|---|
| 713 | x = 'x'
|
|---|
| 714 | x = 123
|
|---|
| 715 |
|
|---|
| 716 | ### exprlist: expr (',' expr)* [',']
|
|---|
| 717 | ### testlist: test (',' test)* [',']
|
|---|
| 718 | # These have been exercised enough above
|
|---|
| 719 |
|
|---|
| 720 | print 'classdef' # 'class' NAME ['(' [testlist] ')'] ':' suite
|
|---|
| 721 | class B: pass
|
|---|
| 722 | class B2(): pass
|
|---|
| 723 | class C1(B): pass
|
|---|
| 724 | class C2(B): pass
|
|---|
| 725 | class D(C1, C2, B): pass
|
|---|
| 726 | class C:
|
|---|
| 727 | def meth1(self): pass
|
|---|
| 728 | def meth2(self, arg): pass
|
|---|
| 729 | def meth3(self, a1, a2): pass
|
|---|
| 730 |
|
|---|
| 731 | # list comprehension tests
|
|---|
| 732 | nums = [1, 2, 3, 4, 5]
|
|---|
| 733 | strs = ["Apple", "Banana", "Coconut"]
|
|---|
| 734 | spcs = [" Apple", " Banana ", "Coco nut "]
|
|---|
| 735 |
|
|---|
| 736 | print [s.strip() for s in spcs]
|
|---|
| 737 | print [3 * x for x in nums]
|
|---|
| 738 | print [x for x in nums if x > 2]
|
|---|
| 739 | print [(i, s) for i in nums for s in strs]
|
|---|
| 740 | print [(i, s) for i in nums for s in [f for f in strs if "n" in f]]
|
|---|
| 741 | print [(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)]
|
|---|
| 742 |
|
|---|
| 743 | def test_in_func(l):
|
|---|
| 744 | return [None < x < 3 for x in l if x > 2]
|
|---|
| 745 |
|
|---|
| 746 | print test_in_func(nums)
|
|---|
| 747 |
|
|---|
| 748 | def test_nested_front():
|
|---|
| 749 | print [[y for y in [x, x + 1]] for x in [1,3,5]]
|
|---|
| 750 |
|
|---|
| 751 | test_nested_front()
|
|---|
| 752 |
|
|---|
| 753 | check_syntax("[i, s for i in nums for s in strs]")
|
|---|
| 754 | check_syntax("[x if y]")
|
|---|
| 755 |
|
|---|
| 756 | suppliers = [
|
|---|
| 757 | (1, "Boeing"),
|
|---|
| 758 | (2, "Ford"),
|
|---|
| 759 | (3, "Macdonalds")
|
|---|
| 760 | ]
|
|---|
| 761 |
|
|---|
| 762 | parts = [
|
|---|
| 763 | (10, "Airliner"),
|
|---|
| 764 | (20, "Engine"),
|
|---|
| 765 | (30, "Cheeseburger")
|
|---|
| 766 | ]
|
|---|
| 767 |
|
|---|
| 768 | suppart = [
|
|---|
| 769 | (1, 10), (1, 20), (2, 20), (3, 30)
|
|---|
| 770 | ]
|
|---|
| 771 |
|
|---|
| 772 | print [
|
|---|
| 773 | (sname, pname)
|
|---|
| 774 | for (sno, sname) in suppliers
|
|---|
| 775 | for (pno, pname) in parts
|
|---|
| 776 | for (sp_sno, sp_pno) in suppart
|
|---|
| 777 | if sno == sp_sno and pno == sp_pno
|
|---|
| 778 | ]
|
|---|
| 779 |
|
|---|
| 780 | # generator expression tests
|
|---|
| 781 | g = ([x for x in range(10)] for x in range(1))
|
|---|
| 782 | verify(g.next() == [x for x in range(10)])
|
|---|
| 783 | try:
|
|---|
| 784 | g.next()
|
|---|
| 785 | raise TestFailed, 'should produce StopIteration exception'
|
|---|
| 786 | except StopIteration:
|
|---|
| 787 | pass
|
|---|
| 788 |
|
|---|
| 789 | a = 1
|
|---|
| 790 | try:
|
|---|
| 791 | g = (a for d in a)
|
|---|
| 792 | g.next()
|
|---|
| 793 | raise TestFailed, 'should produce TypeError'
|
|---|
| 794 | except TypeError:
|
|---|
| 795 | pass
|
|---|
| 796 |
|
|---|
| 797 | verify(list((x, y) for x in 'abcd' for y in 'abcd') == [(x, y) for x in 'abcd' for y in 'abcd'])
|
|---|
| 798 | verify(list((x, y) for x in 'ab' for y in 'xy') == [(x, y) for x in 'ab' for y in 'xy'])
|
|---|
| 799 |
|
|---|
| 800 | a = [x for x in range(10)]
|
|---|
| 801 | b = (x for x in (y for y in a))
|
|---|
| 802 | verify(sum(b) == sum([x for x in range(10)]))
|
|---|
| 803 |
|
|---|
| 804 | verify(sum(x**2 for x in range(10)) == sum([x**2 for x in range(10)]))
|
|---|
| 805 | verify(sum(x*x for x in range(10) if x%2) == sum([x*x for x in range(10) if x%2]))
|
|---|
| 806 | verify(sum(x for x in (y for y in range(10))) == sum([x for x in range(10)]))
|
|---|
| 807 | verify(sum(x for x in (y for y in (z for z in range(10)))) == sum([x for x in range(10)]))
|
|---|
| 808 | verify(sum(x for x in [y for y in (z for z in range(10))]) == sum([x for x in range(10)]))
|
|---|
| 809 | 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)]))
|
|---|
| 810 | verify(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True) == 0)
|
|---|
| 811 | check_syntax("foo(x for x in range(10), 100)")
|
|---|
| 812 | check_syntax("foo(100, x for x in range(10))")
|
|---|
| 813 |
|
|---|
| 814 | # test for outmost iterable precomputation
|
|---|
| 815 | x = 10; g = (i for i in range(x)); x = 5
|
|---|
| 816 | verify(len(list(g)) == 10)
|
|---|
| 817 |
|
|---|
| 818 | # This should hold, since we're only precomputing outmost iterable.
|
|---|
| 819 | x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x))
|
|---|
| 820 | x = 5; t = True;
|
|---|
| 821 | verify([(i,j) for i in range(10) for j in range(5)] == list(g))
|
|---|
| 822 |
|
|---|
| 823 | # Grammar allows multiple adjacent 'if's in listcomps and genexps,
|
|---|
| 824 | # even though it's silly. Make sure it works (ifelse broke this.)
|
|---|
| 825 | verify([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7])
|
|---|
| 826 | verify((x for x in range(10) if x % 2 if x % 3), [1, 5, 7])
|
|---|
| 827 |
|
|---|
| 828 | # Verify unpacking single element tuples in listcomp/genexp.
|
|---|
| 829 | vereq([x for x, in [(4,), (5,), (6,)]], [4, 5, 6])
|
|---|
| 830 | vereq(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9])
|
|---|
| 831 |
|
|---|
| 832 | # Test ifelse expressions in various cases
|
|---|
| 833 | def _checkeval(msg, ret):
|
|---|
| 834 | "helper to check that evaluation of expressions is done correctly"
|
|---|
| 835 | print x
|
|---|
| 836 | return ret
|
|---|
| 837 |
|
|---|
| 838 | verify([ x() for x in lambda: True, lambda: False if x() ] == [True])
|
|---|
| 839 | verify([ x() for x in (lambda: True, lambda: False) if x() ] == [True])
|
|---|
| 840 | verify([ x(False) for x in (lambda x: False if x else True, lambda x: True if x else False) if x(False) ] == [True])
|
|---|
| 841 | verify((5 if 1 else _checkeval("check 1", 0)) == 5)
|
|---|
| 842 | verify((_checkeval("check 2", 0) if 0 else 5) == 5)
|
|---|
| 843 | verify((5 and 6 if 0 else 1) == 1)
|
|---|
| 844 | verify(((5 and 6) if 0 else 1) == 1)
|
|---|
| 845 | verify((5 and (6 if 1 else 1)) == 6)
|
|---|
| 846 | verify((0 or _checkeval("check 3", 2) if 0 else 3) == 3)
|
|---|
| 847 | verify((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)) == 1)
|
|---|
| 848 | verify((0 or 5 if 1 else _checkeval("check 6", 3)) == 5)
|
|---|
| 849 | verify((not 5 if 1 else 1) == False)
|
|---|
| 850 | verify((not 5 if 0 else 1) == 1)
|
|---|
| 851 | verify((6 + 1 if 1 else 2) == 7)
|
|---|
| 852 | verify((6 - 1 if 1 else 2) == 5)
|
|---|
| 853 | verify((6 * 2 if 1 else 4) == 12)
|
|---|
| 854 | verify((6 / 2 if 1 else 3) == 3)
|
|---|
| 855 | verify((6 < 4 if 0 else 2) == 2)
|
|---|