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 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 11 | from test.support import run_unittest, check_syntax_error |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 12 | import unittest |
Jeremy Hylton | 7d3dff2 | 2001-10-10 01:45:02 +0000 | [diff] [blame] | 13 | import sys |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 14 | # testing import * |
| 15 | from sys import * |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 16 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 17 | class TokenTests(unittest.TestCase): |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 18 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 19 | def testBackslash(self): |
| 20 | # Backslash means line continuation: |
| 21 | x = 1 \ |
| 22 | + 1 |
| 23 | self.assertEquals(x, 2, 'backslash for line continuation') |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 24 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 25 | # Backslash does not means continuation in comments :\ |
| 26 | x = 0 |
| 27 | self.assertEquals(x, 0, 'backslash ending comment') |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 28 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 29 | def testPlainIntegers(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 30 | self.assertEquals(type(000), type(0)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 31 | self.assertEquals(0xff, 255) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 32 | self.assertEquals(0o377, 255) |
| 33 | self.assertEquals(2147483647, 0o17777777777) |
| 34 | self.assertEquals(0b1001, 9) |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 35 | # "0x" is not a valid literal |
| 36 | self.assertRaises(SyntaxError, eval, "0x") |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 37 | from sys import maxsize |
| 38 | if maxsize == 2147483647: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 39 | self.assertEquals(-2147483647-1, -0o20000000000) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 40 | # XXX -2147483648 |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 41 | self.assert_(0o37777777777 > 0) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 42 | self.assert_(0xffffffff > 0) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 43 | self.assert_(0b1111111111111111111111111111111 > 0) |
| 44 | for s in ('2147483648', '0o40000000000', '0x100000000', |
| 45 | '0b10000000000000000000000000000000'): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 46 | try: |
| 47 | x = eval(s) |
| 48 | except OverflowError: |
| 49 | self.fail("OverflowError on huge integer literal %r" % s) |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 50 | elif maxsize == 9223372036854775807: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 51 | self.assertEquals(-9223372036854775807-1, -0o1000000000000000000000) |
| 52 | self.assert_(0o1777777777777777777777 > 0) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 53 | self.assert_(0xffffffffffffffff > 0) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 54 | self.assert_(0b11111111111111111111111111111111111111111111111111111111111111 > 0) |
| 55 | for s in '9223372036854775808', '0o2000000000000000000000', \ |
| 56 | '0x10000000000000000', \ |
| 57 | '0b100000000000000000000000000000000000000000000000000000000000000': |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 58 | try: |
| 59 | x = eval(s) |
| 60 | except OverflowError: |
| 61 | self.fail("OverflowError on huge integer literal %r" % s) |
| 62 | else: |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 63 | self.fail('Weird maxsize value %r' % maxsize) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 64 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 65 | def testLongIntegers(self): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 66 | x = 0 |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 67 | x = 0xffffffffffffffff |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 68 | x = 0Xffffffffffffffff |
| 69 | x = 0o77777777777777777 |
| 70 | x = 0O77777777777777777 |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 71 | x = 123456789012345678901234567890 |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 72 | x = 0b100000000000000000000000000000000000000000000000000000000000000000000 |
| 73 | x = 0B111111111111111111111111111111111111111111111111111111111111111111111 |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 74 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 75 | def testFloats(self): |
| 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 |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 88 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 89 | def testStringLiterals(self): |
| 90 | x = ''; y = ""; self.assert_(len(x) == 0 and x == y) |
| 91 | x = '\''; y = "'"; self.assert_(len(x) == 1 and x == y and ord(x) == 39) |
| 92 | x = '"'; y = "\""; self.assert_(len(x) == 1 and x == y and ord(x) == 34) |
| 93 | x = "doesn't \"shrink\" does it" |
| 94 | y = 'doesn\'t "shrink" does it' |
| 95 | self.assert_(len(x) == 24 and x == y) |
| 96 | x = "does \"shrink\" doesn't it" |
| 97 | y = 'does "shrink" doesn\'t it' |
| 98 | self.assert_(len(x) == 24 and x == y) |
| 99 | x = """ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 100 | The "quick" |
| 101 | brown fox |
| 102 | jumps over |
| 103 | the 'lazy' dog. |
| 104 | """ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 105 | y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n' |
| 106 | self.assertEquals(x, y) |
| 107 | y = ''' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 108 | The "quick" |
| 109 | brown fox |
| 110 | jumps over |
| 111 | the 'lazy' dog. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 112 | ''' |
| 113 | self.assertEquals(x, y) |
| 114 | y = "\n\ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 115 | The \"quick\"\n\ |
| 116 | brown fox\n\ |
| 117 | jumps over\n\ |
| 118 | the 'lazy' dog.\n\ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 119 | " |
| 120 | self.assertEquals(x, y) |
| 121 | y = '\n\ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 122 | The \"quick\"\n\ |
| 123 | brown fox\n\ |
| 124 | jumps over\n\ |
| 125 | the \'lazy\' dog.\n\ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 126 | ' |
| 127 | self.assertEquals(x, y) |
| 128 | |
| 129 | def testEllipsis(self): |
| 130 | x = ... |
| 131 | self.assert_(x is Ellipsis) |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 132 | self.assertRaises(SyntaxError, eval, ".. .") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 133 | |
| 134 | class GrammarTests(unittest.TestCase): |
| 135 | |
| 136 | # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE |
| 137 | # XXX can't test in a script -- this rule is only used when interactive |
| 138 | |
| 139 | # file_input: (NEWLINE | stmt)* ENDMARKER |
| 140 | # Being tested as this very moment this very module |
| 141 | |
| 142 | # expr_input: testlist NEWLINE |
| 143 | # XXX Hard to test -- used only in calls to input() |
| 144 | |
| 145 | def testEvalInput(self): |
| 146 | # testlist ENDMARKER |
| 147 | x = eval('1, 0 or 1') |
| 148 | |
| 149 | def testFuncdef(self): |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 150 | ### [decorators] 'def' NAME parameters ['->' test] ':' suite |
| 151 | ### decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE |
| 152 | ### decorators: decorator+ |
| 153 | ### parameters: '(' [typedargslist] ')' |
| 154 | ### typedargslist: ((tfpdef ['=' test] ',')* |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 155 | ### ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 156 | ### | tfpdef ['=' test] (',' tfpdef ['=' test])* [',']) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 157 | ### tfpdef: NAME [':' test] |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 158 | ### varargslist: ((vfpdef ['=' test] ',')* |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 159 | ### ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 160 | ### | vfpdef ['=' test] (',' vfpdef ['=' test])* [',']) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 161 | ### vfpdef: NAME |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 162 | def f1(): pass |
| 163 | f1() |
| 164 | f1(*()) |
| 165 | f1(*(), **{}) |
| 166 | def f2(one_argument): pass |
| 167 | def f3(two, arguments): pass |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 168 | self.assertEquals(f2.__code__.co_varnames, ('one_argument',)) |
| 169 | self.assertEquals(f3.__code__.co_varnames, ('two', 'arguments')) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 170 | def a1(one_arg,): pass |
| 171 | def a2(two, args,): pass |
| 172 | def v0(*rest): pass |
| 173 | def v1(a, *rest): pass |
| 174 | def v2(a, b, *rest): pass |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 175 | |
| 176 | f1() |
| 177 | f2(1) |
| 178 | f2(1,) |
| 179 | f3(1, 2) |
| 180 | f3(1, 2,) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 181 | v0() |
| 182 | v0(1) |
| 183 | v0(1,) |
| 184 | v0(1,2) |
| 185 | v0(1,2,3,4,5,6,7,8,9,0) |
| 186 | v1(1) |
| 187 | v1(1,) |
| 188 | v1(1,2) |
| 189 | v1(1,2,3) |
| 190 | v1(1,2,3,4,5,6,7,8,9,0) |
| 191 | v2(1,2) |
| 192 | v2(1,2,3) |
| 193 | v2(1,2,3,4) |
| 194 | v2(1,2,3,4,5,6,7,8,9,0) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 195 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 196 | def d01(a=1): pass |
| 197 | d01() |
| 198 | d01(1) |
| 199 | d01(*(1,)) |
| 200 | d01(**{'a':2}) |
| 201 | def d11(a, b=1): pass |
| 202 | d11(1) |
| 203 | d11(1, 2) |
| 204 | d11(1, **{'b':2}) |
| 205 | def d21(a, b, c=1): pass |
| 206 | d21(1, 2) |
| 207 | d21(1, 2, 3) |
| 208 | d21(*(1, 2, 3)) |
| 209 | d21(1, *(2, 3)) |
| 210 | d21(1, 2, *(3,)) |
| 211 | d21(1, 2, **{'c':3}) |
| 212 | def d02(a=1, b=2): pass |
| 213 | d02() |
| 214 | d02(1) |
| 215 | d02(1, 2) |
| 216 | d02(*(1, 2)) |
| 217 | d02(1, *(2,)) |
| 218 | d02(1, **{'b':2}) |
| 219 | d02(**{'a': 1, 'b': 2}) |
| 220 | def d12(a, b=1, c=2): pass |
| 221 | d12(1) |
| 222 | d12(1, 2) |
| 223 | d12(1, 2, 3) |
| 224 | def d22(a, b, c=1, d=2): pass |
| 225 | d22(1, 2) |
| 226 | d22(1, 2, 3) |
| 227 | d22(1, 2, 3, 4) |
| 228 | def d01v(a=1, *rest): pass |
| 229 | d01v() |
| 230 | d01v(1) |
| 231 | d01v(1, 2) |
| 232 | d01v(*(1, 2, 3, 4)) |
| 233 | d01v(*(1,)) |
| 234 | d01v(**{'a':2}) |
| 235 | def d11v(a, b=1, *rest): pass |
| 236 | d11v(1) |
| 237 | d11v(1, 2) |
| 238 | d11v(1, 2, 3) |
| 239 | def d21v(a, b, c=1, *rest): pass |
| 240 | d21v(1, 2) |
| 241 | d21v(1, 2, 3) |
| 242 | d21v(1, 2, 3, 4) |
| 243 | d21v(*(1, 2, 3, 4)) |
| 244 | d21v(1, 2, **{'c': 3}) |
| 245 | def d02v(a=1, b=2, *rest): pass |
| 246 | d02v() |
| 247 | d02v(1) |
| 248 | d02v(1, 2) |
| 249 | d02v(1, 2, 3) |
| 250 | d02v(1, *(2, 3, 4)) |
| 251 | d02v(**{'a': 1, 'b': 2}) |
| 252 | def d12v(a, b=1, c=2, *rest): pass |
| 253 | d12v(1) |
| 254 | d12v(1, 2) |
| 255 | d12v(1, 2, 3) |
| 256 | d12v(1, 2, 3, 4) |
| 257 | d12v(*(1, 2, 3, 4)) |
| 258 | d12v(1, 2, *(3, 4, 5)) |
| 259 | d12v(1, *(2,), **{'c': 3}) |
| 260 | def d22v(a, b, c=1, d=2, *rest): pass |
| 261 | d22v(1, 2) |
| 262 | d22v(1, 2, 3) |
| 263 | d22v(1, 2, 3, 4) |
| 264 | d22v(1, 2, 3, 4, 5) |
| 265 | d22v(*(1, 2, 3, 4)) |
| 266 | d22v(1, 2, *(3, 4, 5)) |
| 267 | d22v(1, *(2, 3), **{'d': 4}) |
Georg Brandl | d8b690f | 2008-05-16 17:28:50 +0000 | [diff] [blame] | 268 | |
| 269 | # keyword argument type tests |
| 270 | try: |
| 271 | str('x', **{b'foo':1 }) |
| 272 | except TypeError: |
| 273 | pass |
| 274 | else: |
| 275 | self.fail('Bytes should not work as keyword argument names') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 276 | # keyword only argument tests |
| 277 | def pos0key1(*, key): return key |
| 278 | pos0key1(key=100) |
| 279 | def pos2key2(p1, p2, *, k1, k2=100): return p1,p2,k1,k2 |
| 280 | pos2key2(1, 2, k1=100) |
| 281 | pos2key2(1, 2, k1=100, k2=200) |
| 282 | pos2key2(1, 2, k2=100, k1=200) |
| 283 | def pos2key2dict(p1, p2, *, k1=100, k2, **kwarg): return p1,p2,k1,k2,kwarg |
| 284 | pos2key2dict(1,2,k2=100,tokwarg1=100,tokwarg2=200) |
| 285 | pos2key2dict(1,2,tokwarg1=100,tokwarg2=200, k2=100) |
| 286 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 287 | # argument annotation tests |
| 288 | def f(x) -> list: pass |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 289 | self.assertEquals(f.__annotations__, {'return': list}) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 290 | def f(x:int): pass |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 291 | self.assertEquals(f.__annotations__, {'x': int}) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 292 | def f(*x:str): pass |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 293 | self.assertEquals(f.__annotations__, {'x': str}) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 294 | def f(**x:float): pass |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 295 | self.assertEquals(f.__annotations__, {'x': float}) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 296 | def f(x, y:1+2): pass |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 297 | self.assertEquals(f.__annotations__, {'y': 3}) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 298 | def f(a, b:1, c:2, d): pass |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 299 | self.assertEquals(f.__annotations__, {'b': 1, 'c': 2}) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 300 | def f(a, b:1, c:2, d, e:3=4, f=5, *g:6): pass |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 301 | self.assertEquals(f.__annotations__, |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 302 | {'b': 1, 'c': 2, 'e': 3, 'g': 6}) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 303 | def f(a, b:1, c:2, d, e:3=4, f=5, *g:6, h:7, i=8, j:9=10, |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 304 | **k:11) -> 12: pass |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 305 | self.assertEquals(f.__annotations__, |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 306 | {'b': 1, 'c': 2, 'e': 3, 'g': 6, 'h': 7, 'j': 9, |
| 307 | 'k': 11, 'return': 12}) |
Nick Coghlan | 71011e2 | 2007-04-23 11:05:01 +0000 | [diff] [blame] | 308 | # Check for SF Bug #1697248 - mixing decorators and a return annotation |
| 309 | def null(x): return x |
| 310 | @null |
| 311 | def f(x) -> list: pass |
| 312 | self.assertEquals(f.__annotations__, {'return': list}) |
| 313 | |
Guido van Rossum | 0240b92 | 2007-02-26 21:23:50 +0000 | [diff] [blame] | 314 | # test MAKE_CLOSURE with a variety of oparg's |
| 315 | closure = 1 |
| 316 | def f(): return closure |
| 317 | def f(x=1): return closure |
| 318 | def f(*, k=1): return closure |
| 319 | def f() -> int: return closure |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 320 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 321 | # Check ast errors in *args and *kwargs |
| 322 | check_syntax_error(self, "f(*g(1=2))") |
| 323 | check_syntax_error(self, "f(**g(1=2))") |
| 324 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 325 | def testLambdef(self): |
| 326 | ### lambdef: 'lambda' [varargslist] ':' test |
| 327 | l1 = lambda : 0 |
| 328 | self.assertEquals(l1(), 0) |
| 329 | l2 = lambda : a[d] # XXX just testing the expression |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 330 | l3 = lambda : [2 < x for x in [-1, 3, 0]] |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 331 | self.assertEquals(l3(), [0, 1, 0]) |
| 332 | l4 = lambda x = lambda y = lambda z=1 : z : y() : x() |
| 333 | self.assertEquals(l4(), 1) |
| 334 | l5 = lambda x, y, z=2: x + y + z |
| 335 | self.assertEquals(l5(1, 2), 5) |
| 336 | self.assertEquals(l5(1, 2, 3), 6) |
| 337 | check_syntax_error(self, "lambda x: x = 2") |
| 338 | l6 = lambda x, y, *, k=20: x+y+k |
| 339 | self.assertEquals(l6(1,2), 1+2+20) |
| 340 | self.assertEquals(l6(1,2,k=10), 1+2+10) |
Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 341 | |
| 342 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 343 | ### stmt: simple_stmt | compound_stmt |
| 344 | # Tested below |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 345 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 346 | def testSimpleStmt(self): |
| 347 | ### simple_stmt: small_stmt (';' small_stmt)* [';'] |
| 348 | x = 1; pass; del x |
| 349 | def foo(): |
| 350 | # verify statments that end with semi-colons |
| 351 | x = 1; pass; del x; |
| 352 | foo() |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 353 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 354 | ### small_stmt: expr_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 355 | # Tested below |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 356 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 357 | def testExprStmt(self): |
| 358 | # (exprlist '=')* exprlist |
| 359 | 1 |
| 360 | 1, 2, 3 |
| 361 | x = 1 |
| 362 | x = 1, 2, 3 |
| 363 | x = y = z = 1, 2, 3 |
| 364 | x, y, z = 1, 2, 3 |
| 365 | abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 366 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 367 | check_syntax_error(self, "x + 1 = 1") |
| 368 | check_syntax_error(self, "a + 1 = b + 2") |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 369 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 370 | def testDelStmt(self): |
| 371 | # 'del' exprlist |
| 372 | abc = [1,2,3] |
| 373 | x, y, z = abc |
| 374 | xyz = x, y, z |
Barry Warsaw | 7e3e1c1 | 2000-10-11 21:26:03 +0000 | [diff] [blame] | 375 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 376 | del abc |
| 377 | del x, y, (z, xyz) |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 378 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 379 | def testPassStmt(self): |
| 380 | # 'pass' |
| 381 | pass |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 382 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 383 | # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 384 | # Tested below |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 385 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 386 | def testBreakStmt(self): |
| 387 | # 'break' |
| 388 | while 1: break |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 389 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 390 | def testContinueStmt(self): |
| 391 | # 'continue' |
| 392 | i = 1 |
| 393 | while i: i = 0; continue |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 394 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 395 | msg = "" |
| 396 | while not msg: |
| 397 | msg = "ok" |
| 398 | try: |
| 399 | continue |
| 400 | msg = "continue failed to continue inside try" |
| 401 | except: |
| 402 | msg = "continue inside try called except block" |
| 403 | if msg != "ok": |
| 404 | self.fail(msg) |
Barry Warsaw | efc92ee | 2000-08-21 15:46:50 +0000 | [diff] [blame] | 405 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 406 | msg = "" |
| 407 | while not msg: |
| 408 | msg = "finally block not called" |
| 409 | try: |
| 410 | continue |
| 411 | finally: |
| 412 | msg = "ok" |
| 413 | if msg != "ok": |
| 414 | self.fail(msg) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 415 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 416 | def test_break_continue_loop(self): |
| 417 | # This test warrants an explanation. It is a test specifically for SF bugs |
| 418 | # #463359 and #462937. The bug is that a 'break' statement executed or |
| 419 | # exception raised inside a try/except inside a loop, *after* a continue |
| 420 | # statement has been executed in that loop, will cause the wrong number of |
| 421 | # arguments to be popped off the stack and the instruction pointer reset to |
| 422 | # a very small number (usually 0.) Because of this, the following test |
| 423 | # *must* written as a function, and the tracking vars *must* be function |
| 424 | # arguments with default values. Otherwise, the test will loop and loop. |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 425 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 426 | def test_inner(extra_burning_oil = 1, count=0): |
| 427 | big_hippo = 2 |
| 428 | while big_hippo: |
| 429 | count += 1 |
| 430 | try: |
| 431 | if extra_burning_oil and big_hippo == 1: |
| 432 | extra_burning_oil -= 1 |
| 433 | break |
| 434 | big_hippo -= 1 |
| 435 | continue |
| 436 | except: |
| 437 | raise |
| 438 | if count > 2 or big_hippo != 1: |
| 439 | self.fail("continue then break in try/except in loop broken!") |
| 440 | test_inner() |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 441 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 442 | def testReturn(self): |
| 443 | # 'return' [testlist] |
| 444 | def g1(): return |
| 445 | def g2(): return 1 |
| 446 | g1() |
| 447 | x = g2() |
| 448 | check_syntax_error(self, "class foo:return 1") |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 449 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 450 | def testYield(self): |
| 451 | check_syntax_error(self, "class foo:yield 1") |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 452 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 453 | def testRaise(self): |
| 454 | # 'raise' test [',' test] |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 455 | try: raise RuntimeError('just testing') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 456 | except RuntimeError: pass |
| 457 | try: raise KeyboardInterrupt |
| 458 | except KeyboardInterrupt: pass |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 459 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 460 | def testImport(self): |
| 461 | # 'import' dotted_as_names |
| 462 | import sys |
| 463 | import time, sys |
| 464 | # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names) |
| 465 | from time import time |
| 466 | from time import (time) |
| 467 | # not testable inside a function, but already done at top of the module |
| 468 | # from sys import * |
| 469 | from sys import path, argv |
| 470 | from sys import (path, argv) |
| 471 | from sys import (path, argv,) |
Tim Peters | 10fb386 | 2001-02-09 20:17:14 +0000 | [diff] [blame] | 472 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 473 | def testGlobal(self): |
| 474 | # 'global' NAME (',' NAME)* |
| 475 | global a |
| 476 | global a, b |
| 477 | global one, two, three, four, five, six, seven, eight, nine, ten |
Thomas Wouters | 80d373c | 2001-09-26 12:43:39 +0000 | [diff] [blame] | 478 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 479 | def testAssert(self): |
| 480 | # assert_stmt: 'assert' test [',' test] |
| 481 | assert 1 |
| 482 | assert 1, 1 |
| 483 | assert lambda x:x |
| 484 | assert 1, lambda x:x+1 |
Thomas Wouters | 80d373c | 2001-09-26 12:43:39 +0000 | [diff] [blame] | 485 | try: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 486 | assert 0, "msg" |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 487 | except AssertionError as e: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 488 | self.assertEquals(e.args[0], "msg") |
| 489 | else: |
| 490 | if __debug__: |
| 491 | self.fail("AssertionError not raised by assert 0") |
Thomas Wouters | 80d373c | 2001-09-26 12:43:39 +0000 | [diff] [blame] | 492 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 493 | ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef |
| 494 | # Tested below |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 495 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 496 | def testIf(self): |
| 497 | # '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 |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 508 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 509 | def testWhile(self): |
| 510 | # 'while' test ':' suite ['else' ':' suite] |
| 511 | while 0: pass |
| 512 | while 0: pass |
| 513 | else: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 514 | |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 515 | # Issue1920: "while 0" is optimized away, |
| 516 | # ensure that the "else" clause is still present. |
| 517 | x = 0 |
| 518 | while 0: |
| 519 | x = 1 |
| 520 | else: |
| 521 | x = 2 |
| 522 | self.assertEquals(x, 2) |
| 523 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 524 | def testFor(self): |
| 525 | # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] |
| 526 | for i in 1, 2, 3: pass |
| 527 | for i, j, k in (): pass |
| 528 | else: pass |
| 529 | class Squares: |
| 530 | def __init__(self, max): |
| 531 | self.max = max |
| 532 | self.sofar = [] |
| 533 | def __len__(self): return len(self.sofar) |
| 534 | def __getitem__(self, i): |
| 535 | if not 0 <= i < self.max: raise IndexError |
| 536 | n = len(self.sofar) |
| 537 | while n <= i: |
| 538 | self.sofar.append(n*n) |
| 539 | n = n+1 |
| 540 | return self.sofar[i] |
| 541 | n = 0 |
| 542 | for x in Squares(10): n = n+x |
| 543 | if n != 285: |
| 544 | self.fail('for over growing sequence') |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 545 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 546 | result = [] |
| 547 | for x, in [(1,), (2,), (3,)]: |
| 548 | result.append(x) |
| 549 | self.assertEqual(result, [1, 2, 3]) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 550 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 551 | def testTry(self): |
| 552 | ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
| 553 | ### | 'try' ':' suite 'finally' ':' suite |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 554 | ### except_clause: 'except' [expr ['as' expr]] |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 555 | try: |
| 556 | 1/0 |
| 557 | except ZeroDivisionError: |
| 558 | pass |
| 559 | else: |
| 560 | pass |
| 561 | try: 1/0 |
| 562 | except EOFError: pass |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 563 | except TypeError as msg: pass |
| 564 | except RuntimeError as msg: pass |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 565 | except: pass |
| 566 | else: pass |
| 567 | try: 1/0 |
| 568 | except (EOFError, TypeError, ZeroDivisionError): pass |
| 569 | try: 1/0 |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 570 | except (EOFError, TypeError, ZeroDivisionError) as msg: pass |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 571 | try: pass |
| 572 | finally: pass |
Jeremy Hylton | f828e2d | 2001-02-19 15:54:52 +0000 | [diff] [blame] | 573 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 574 | def testSuite(self): |
| 575 | # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT |
| 576 | if 1: pass |
| 577 | if 1: |
| 578 | pass |
| 579 | if 1: |
| 580 | # |
| 581 | # |
| 582 | # |
| 583 | pass |
| 584 | pass |
| 585 | # |
| 586 | pass |
| 587 | # |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 588 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 589 | def testTest(self): |
| 590 | ### and_test ('or' and_test)* |
| 591 | ### and_test: not_test ('and' not_test)* |
| 592 | ### not_test: 'not' not_test | comparison |
| 593 | if not 1: pass |
| 594 | if 1 and 1: pass |
| 595 | if 1 or 1: pass |
| 596 | if not not not 1: pass |
| 597 | if not 1 and 1 and 1: pass |
| 598 | if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 599 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 600 | def testComparison(self): |
| 601 | ### comparison: expr (comp_op expr)* |
| 602 | ### comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not' |
| 603 | if 1: pass |
| 604 | x = (1 == 1) |
| 605 | if 1 == 1: pass |
| 606 | if 1 != 1: pass |
| 607 | if 1 < 1: pass |
| 608 | if 1 > 1: pass |
| 609 | if 1 <= 1: pass |
| 610 | if 1 >= 1: pass |
| 611 | if 1 is 1: pass |
| 612 | if 1 is not 1: pass |
| 613 | if 1 in (): pass |
| 614 | if 1 not in (): pass |
| 615 | if 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] | 616 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 617 | def testBinaryMaskOps(self): |
| 618 | x = 1 & 1 |
| 619 | x = 1 ^ 1 |
| 620 | x = 1 | 1 |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 621 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 622 | def testShiftOps(self): |
| 623 | x = 1 << 1 |
| 624 | x = 1 >> 1 |
| 625 | x = 1 << 1 >> 1 |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 626 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 627 | def testAdditiveOps(self): |
| 628 | x = 1 |
| 629 | x = 1 + 1 |
| 630 | x = 1 - 1 - 1 |
| 631 | x = 1 - 1 + 1 - 1 + 1 |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 632 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 633 | def testMultiplicativeOps(self): |
| 634 | x = 1 * 1 |
| 635 | x = 1 / 1 |
| 636 | x = 1 % 1 |
| 637 | x = 1 / 1 * 1 % 1 |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 638 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 639 | def testUnaryOps(self): |
| 640 | x = +1 |
| 641 | x = -1 |
| 642 | x = ~1 |
| 643 | x = ~1 ^ 1 & 1 | 1 & 1 ^ -1 |
| 644 | x = -1*1/1 + 1*1 - ---1*1 |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 645 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 646 | def testSelectors(self): |
| 647 | ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME |
| 648 | ### subscript: expr | [expr] ':' [expr] |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 649 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 650 | import sys, time |
| 651 | c = sys.path[0] |
| 652 | x = time.time() |
| 653 | x = sys.modules['time'].time() |
| 654 | a = '01234' |
| 655 | c = a[0] |
| 656 | c = a[-1] |
| 657 | s = a[0:5] |
| 658 | s = a[:5] |
| 659 | s = a[0:] |
| 660 | s = a[:] |
| 661 | s = a[-5:] |
| 662 | s = a[:-1] |
| 663 | s = a[-4:-3] |
| 664 | # A rough test of SF bug 1333982. http://python.org/sf/1333982 |
| 665 | # The testing here is fairly incomplete. |
| 666 | # Test cases should include: commas with 1 and 2 colons |
| 667 | d = {} |
| 668 | d[1] = 1 |
| 669 | d[1,] = 2 |
| 670 | d[1,2] = 3 |
| 671 | d[1,2,3] = 4 |
| 672 | L = list(d) |
| 673 | L.sort(key=lambda x: x if isinstance(x, tuple) else ()) |
| 674 | self.assertEquals(str(L), '[1, (1,), (1, 2), (1, 2, 3)]') |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 675 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 676 | def testAtoms(self): |
| 677 | ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING |
| 678 | ### dictsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [',']) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 679 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 680 | x = (1) |
| 681 | x = (1 or 2 or 3) |
| 682 | x = (1 or 2 or 3, 2, 3) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 683 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 684 | x = [] |
| 685 | x = [1] |
| 686 | x = [1 or 2 or 3] |
| 687 | x = [1 or 2 or 3, 2, 3] |
| 688 | x = [] |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 689 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 690 | x = {} |
| 691 | x = {'one': 1} |
| 692 | x = {'one': 1,} |
| 693 | x = {'one' or 'two': 1 or 2} |
| 694 | x = {'one': 1, 'two': 2} |
| 695 | x = {'one': 1, 'two': 2,} |
| 696 | x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6} |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 697 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 698 | x = {'one'} |
| 699 | x = {'one', 1,} |
| 700 | x = {'one', 'two', 'three'} |
| 701 | x = {2, 3, 4,} |
| 702 | |
| 703 | x = x |
| 704 | x = 'x' |
| 705 | x = 123 |
| 706 | |
| 707 | ### exprlist: expr (',' expr)* [','] |
| 708 | ### testlist: test (',' test)* [','] |
| 709 | # These have been exercised enough above |
| 710 | |
| 711 | def testClassdef(self): |
| 712 | # 'class' NAME ['(' [testlist] ')'] ':' suite |
| 713 | class B: pass |
| 714 | class B2(): pass |
| 715 | class C1(B): pass |
| 716 | class C2(B): pass |
| 717 | class D(C1, C2, B): pass |
| 718 | class C: |
| 719 | def meth1(self): pass |
| 720 | def meth2(self, arg): pass |
| 721 | def meth3(self, a1, a2): pass |
| 722 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 723 | # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE |
| 724 | # decorators: decorator+ |
| 725 | # decorated: decorators (classdef | funcdef) |
| 726 | def class_decorator(x): return x |
| 727 | @class_decorator |
| 728 | class G: pass |
| 729 | |
| 730 | def testDictcomps(self): |
| 731 | # dictorsetmaker: ( (test ':' test (comp_for | |
| 732 | # (',' test ':' test)* [','])) | |
| 733 | # (test (comp_for | (',' test)* [','])) ) |
| 734 | nums = [1, 2, 3] |
| 735 | self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4}) |
| 736 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 737 | def testListcomps(self): |
| 738 | # list comprehension tests |
| 739 | nums = [1, 2, 3, 4, 5] |
| 740 | strs = ["Apple", "Banana", "Coconut"] |
| 741 | spcs = [" Apple", " Banana ", "Coco nut "] |
| 742 | |
| 743 | self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco nut']) |
| 744 | self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15]) |
| 745 | self.assertEqual([x for x in nums if x > 2], [3, 4, 5]) |
| 746 | self.assertEqual([(i, s) for i in nums for s in strs], |
| 747 | [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'), |
| 748 | (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'), |
| 749 | (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'), |
| 750 | (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'), |
| 751 | (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')]) |
| 752 | self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]], |
| 753 | [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'), |
| 754 | (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'), |
| 755 | (5, 'Banana'), (5, 'Coconut')]) |
| 756 | self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)], |
| 757 | [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]]) |
| 758 | |
| 759 | def test_in_func(l): |
| 760 | return [0 < x < 3 for x in l if x > 2] |
| 761 | |
| 762 | self.assertEqual(test_in_func(nums), [False, False, False]) |
| 763 | |
| 764 | def test_nested_front(): |
| 765 | self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]], |
| 766 | [[1, 2], [3, 4], [5, 6]]) |
| 767 | |
| 768 | test_nested_front() |
| 769 | |
| 770 | check_syntax_error(self, "[i, s for i in nums for s in strs]") |
| 771 | check_syntax_error(self, "[x if y]") |
| 772 | |
| 773 | suppliers = [ |
| 774 | (1, "Boeing"), |
| 775 | (2, "Ford"), |
| 776 | (3, "Macdonalds") |
| 777 | ] |
| 778 | |
| 779 | parts = [ |
| 780 | (10, "Airliner"), |
| 781 | (20, "Engine"), |
| 782 | (30, "Cheeseburger") |
| 783 | ] |
| 784 | |
| 785 | suppart = [ |
| 786 | (1, 10), (1, 20), (2, 20), (3, 30) |
| 787 | ] |
| 788 | |
| 789 | x = [ |
| 790 | (sname, pname) |
| 791 | for (sno, sname) in suppliers |
| 792 | for (pno, pname) in parts |
| 793 | for (sp_sno, sp_pno) in suppart |
| 794 | if sno == sp_sno and pno == sp_pno |
| 795 | ] |
| 796 | |
| 797 | self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'), |
| 798 | ('Macdonalds', 'Cheeseburger')]) |
| 799 | |
| 800 | def testGenexps(self): |
| 801 | # generator expression tests |
| 802 | g = ([x for x in range(10)] for x in range(1)) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 803 | self.assertEqual(next(g), [x for x in range(10)]) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 804 | try: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 805 | next(g) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 806 | self.fail('should produce StopIteration exception') |
| 807 | except StopIteration: |
| 808 | pass |
| 809 | |
| 810 | a = 1 |
| 811 | try: |
| 812 | g = (a for d in a) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 813 | next(g) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 814 | self.fail('should produce TypeError') |
| 815 | except TypeError: |
| 816 | pass |
| 817 | |
| 818 | self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd']) |
| 819 | self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy']) |
| 820 | |
| 821 | a = [x for x in range(10)] |
| 822 | b = (x for x in (y for y in a)) |
| 823 | self.assertEqual(sum(b), sum([x for x in range(10)])) |
| 824 | |
| 825 | self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)])) |
| 826 | self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2])) |
| 827 | self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)])) |
| 828 | self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)])) |
| 829 | self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)])) |
| 830 | self.assertEqual(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)])) |
| 831 | self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0) |
| 832 | check_syntax_error(self, "foo(x for x in range(10), 100)") |
| 833 | check_syntax_error(self, "foo(100, x for x in range(10))") |
| 834 | |
| 835 | def testComprehensionSpecials(self): |
| 836 | # test for outmost iterable precomputation |
| 837 | x = 10; g = (i for i in range(x)); x = 5 |
| 838 | self.assertEqual(len(list(g)), 10) |
| 839 | |
| 840 | # This should hold, since we're only precomputing outmost iterable. |
| 841 | x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x)) |
| 842 | x = 5; t = True; |
| 843 | self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g)) |
| 844 | |
| 845 | # Grammar allows multiple adjacent 'if's in listcomps and genexps, |
| 846 | # even though it's silly. Make sure it works (ifelse broke this.) |
| 847 | self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7]) |
| 848 | self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7]) |
| 849 | |
| 850 | # verify unpacking single element tuples in listcomp/genexp. |
| 851 | self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6]) |
| 852 | self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9]) |
| 853 | |
| 854 | def testIfElseExpr(self): |
| 855 | # Test ifelse expressions in various cases |
| 856 | def _checkeval(msg, ret): |
| 857 | "helper to check that evaluation of expressions is done correctly" |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 858 | print(x) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 859 | return ret |
| 860 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 861 | # the next line is not allowed anymore |
| 862 | #self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True]) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 863 | self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True]) |
| 864 | self.assertEqual([ x(False) for x in (lambda x: False if x else True, lambda x: True if x else False) if x(False) ], [True]) |
| 865 | self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5) |
| 866 | self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5) |
| 867 | self.assertEqual((5 and 6 if 0 else 1), 1) |
| 868 | self.assertEqual(((5 and 6) if 0 else 1), 1) |
| 869 | self.assertEqual((5 and (6 if 1 else 1)), 6) |
| 870 | self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3) |
| 871 | self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1) |
| 872 | self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5) |
| 873 | self.assertEqual((not 5 if 1 else 1), False) |
| 874 | self.assertEqual((not 5 if 0 else 1), 1) |
| 875 | self.assertEqual((6 + 1 if 1 else 2), 7) |
| 876 | self.assertEqual((6 - 1 if 1 else 2), 5) |
| 877 | self.assertEqual((6 * 2 if 1 else 4), 12) |
| 878 | self.assertEqual((6 / 2 if 1 else 3), 3) |
| 879 | self.assertEqual((6 < 4 if 0 else 2), 2) |
Jeremy Hylton | 7b03bad | 2006-02-28 17:46:23 +0000 | [diff] [blame] | 880 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 881 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 882 | def test_main(): |
| 883 | run_unittest(TokenTests, GrammarTests) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 884 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 885 | if __name__ == '__main__': |
| 886 | test_main() |