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