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