Tim Peters | 2f228e7 | 2001-05-13 00:19:31 +0000 | [diff] [blame^] | 1 | from test_support import verify, verbose, TestFailed, sortdict |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 2 | from UserList import UserList |
| 3 | |
| 4 | def f(*a, **k): |
Tim Peters | 08dabf0 | 2001-01-21 18:52:02 +0000 | [diff] [blame] | 5 | print a, sortdict(k) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 6 | |
| 7 | def g(x, *y, **z): |
Tim Peters | 08dabf0 | 2001-01-21 18:52:02 +0000 | [diff] [blame] | 8 | print x, y, sortdict(z) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 9 | |
| 10 | def h(j=1, a=2, h=3): |
| 11 | print j, a, h |
| 12 | |
| 13 | f() |
| 14 | f(1) |
| 15 | f(1, 2) |
| 16 | f(1, 2, 3) |
| 17 | |
| 18 | f(1, 2, 3, *(4, 5)) |
| 19 | f(1, 2, 3, *[4, 5]) |
Jeremy Hylton | 003663d | 2000-03-28 23:53:22 +0000 | [diff] [blame] | 20 | f(1, 2, 3, *UserList([4, 5])) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 21 | f(1, 2, 3, **{'a':4, 'b':5}) |
| 22 | f(1, 2, 3, *(4, 5), **{'a':6, 'b':7}) |
| 23 | f(1, 2, 3, x=4, y=5, *(6, 7), **{'a':8, 'b':9}) |
| 24 | |
| 25 | try: |
| 26 | g() |
| 27 | except TypeError, err: |
| 28 | print "TypeError:", err |
| 29 | else: |
| 30 | print "should raise TypeError: not enough arguments; expected 1, got 0" |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 31 | |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 32 | try: |
| 33 | g(*()) |
| 34 | except TypeError, err: |
| 35 | print "TypeError:", err |
| 36 | else: |
| 37 | print "should raise TypeError: not enough arguments; expected 1, got 0" |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 38 | |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 39 | try: |
| 40 | g(*(), **{}) |
| 41 | except TypeError, err: |
| 42 | print "TypeError:", err |
| 43 | else: |
| 44 | print "should raise TypeError: not enough arguments; expected 1, got 0" |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 45 | |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 46 | g(1) |
| 47 | g(1, 2) |
| 48 | g(1, 2, 3) |
| 49 | g(1, 2, 3, *(4, 5)) |
Jeremy Hylton | 074c3e6 | 2000-03-30 23:55:31 +0000 | [diff] [blame] | 50 | class Nothing: pass |
| 51 | try: |
| 52 | g(*Nothing()) |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 53 | except TypeError, attr: |
Guido van Rossum | 4b49101 | 2000-04-10 13:37:14 +0000 | [diff] [blame] | 54 | pass |
Jeremy Hylton | 074c3e6 | 2000-03-30 23:55:31 +0000 | [diff] [blame] | 55 | else: |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 56 | print "should raise TypeError" |
Jeremy Hylton | 074c3e6 | 2000-03-30 23:55:31 +0000 | [diff] [blame] | 57 | |
| 58 | class Nothing: |
| 59 | def __len__(self): |
| 60 | return 5 |
| 61 | try: |
| 62 | g(*Nothing()) |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 63 | except TypeError, attr: |
Guido van Rossum | 4b49101 | 2000-04-10 13:37:14 +0000 | [diff] [blame] | 64 | pass |
Jeremy Hylton | 074c3e6 | 2000-03-30 23:55:31 +0000 | [diff] [blame] | 65 | else: |
Tim Peters | 6912d4d | 2001-05-05 03:56:37 +0000 | [diff] [blame] | 66 | print "should raise TypeError" |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 67 | |
Jeremy Hylton | 074c3e6 | 2000-03-30 23:55:31 +0000 | [diff] [blame] | 68 | class Nothing: |
| 69 | def __len__(self): |
| 70 | return 5 |
| 71 | def __getitem__(self, i): |
| 72 | if i < 3: |
| 73 | return i |
| 74 | else: |
| 75 | raise IndexError, i |
| 76 | g(*Nothing()) |
| 77 | |
| 78 | # make sure the function call doesn't stomp on the dictionary? |
| 79 | d = {'a': 1, 'b': 2, 'c': 3} |
| 80 | d2 = d.copy() |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 81 | verify(d == d2) |
Jeremy Hylton | 074c3e6 | 2000-03-30 23:55:31 +0000 | [diff] [blame] | 82 | g(1, d=4, **d) |
Tim Peters | 08dabf0 | 2001-01-21 18:52:02 +0000 | [diff] [blame] | 83 | print sortdict(d) |
| 84 | print sortdict(d2) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 85 | verify(d == d2, "function call modified dictionary") |
Jeremy Hylton | 074c3e6 | 2000-03-30 23:55:31 +0000 | [diff] [blame] | 86 | |
| 87 | # what about willful misconduct? |
| 88 | def saboteur(**kw): |
Vladimir Marangozov | 5ff2ac2 | 2000-07-15 00:42:09 +0000 | [diff] [blame] | 89 | kw['x'] = locals() # yields a cyclic kw |
| 90 | return kw |
Jeremy Hylton | 074c3e6 | 2000-03-30 23:55:31 +0000 | [diff] [blame] | 91 | d = {} |
Vladimir Marangozov | 5ff2ac2 | 2000-07-15 00:42:09 +0000 | [diff] [blame] | 92 | kw = saboteur(a=1, **d) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 93 | verify(d == {}) |
Vladimir Marangozov | 5ff2ac2 | 2000-07-15 00:42:09 +0000 | [diff] [blame] | 94 | # break the cycle |
| 95 | del kw['x'] |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 96 | |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 97 | try: |
| 98 | g(1, 2, 3, **{'x':4, 'y':5}) |
| 99 | except TypeError, err: |
| 100 | print err |
| 101 | else: |
| 102 | print "should raise TypeError: keyword parameter redefined" |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 103 | |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 104 | try: |
| 105 | g(1, 2, 3, a=4, b=5, *(6, 7), **{'a':8, 'b':9}) |
| 106 | except TypeError, err: |
| 107 | print err |
| 108 | else: |
| 109 | print "should raise TypeError: keyword parameter redefined" |
| 110 | |
| 111 | try: |
| 112 | f(**{1:2}) |
| 113 | except TypeError, err: |
| 114 | print err |
| 115 | else: |
| 116 | print "should raise TypeError: keywords must be strings" |
| 117 | |
| 118 | try: |
| 119 | h(**{'e': 2}) |
| 120 | except TypeError, err: |
| 121 | print err |
| 122 | else: |
| 123 | print "should raise TypeError: unexpected keyword argument: e" |
| 124 | |
| 125 | try: |
| 126 | h(*h) |
| 127 | except TypeError, err: |
| 128 | print err |
| 129 | else: |
| 130 | print "should raise TypeError: * argument must be a tuple" |
| 131 | |
| 132 | try: |
Jeremy Hylton | ceccc3c | 2001-04-11 13:53:35 +0000 | [diff] [blame] | 133 | dir(*h) |
| 134 | except TypeError, err: |
| 135 | print err |
| 136 | else: |
| 137 | print "should raise TypeError: * argument must be a tuple" |
| 138 | |
| 139 | try: |
| 140 | None(*h) |
| 141 | except TypeError, err: |
| 142 | print err |
| 143 | else: |
| 144 | print "should raise TypeError: * argument must be a tuple" |
| 145 | |
| 146 | try: |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 147 | h(**h) |
| 148 | except TypeError, err: |
| 149 | print err |
| 150 | else: |
| 151 | print "should raise TypeError: ** argument must be a dictionary" |
| 152 | |
Jeremy Hylton | ceccc3c | 2001-04-11 13:53:35 +0000 | [diff] [blame] | 153 | try: |
| 154 | dir(**h) |
| 155 | except TypeError, err: |
| 156 | print err |
| 157 | else: |
| 158 | print "should raise TypeError: ** argument must be a dictionary" |
| 159 | |
| 160 | try: |
| 161 | None(**h) |
| 162 | except TypeError, err: |
| 163 | print err |
| 164 | else: |
| 165 | print "should raise TypeError: ** argument must be a dictionary" |
| 166 | |
| 167 | try: |
| 168 | dir(b=1,**{'b':1}) |
| 169 | except TypeError, err: |
| 170 | print err |
| 171 | else: |
| 172 | print "should raise TypeError: dir() got multiple values for keyword argument 'b'" |
| 173 | |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 174 | def f2(*a, **b): |
| 175 | return a, b |
| 176 | |
| 177 | d = {} |
| 178 | for i in range(512): |
| 179 | key = 'k%d' % i |
| 180 | d[key] = i |
| 181 | a, b = f2(1, *(2, 3), **d) |
| 182 | print len(a), len(b), b == d |
Jeremy Hylton | 6b4ec51 | 2000-10-30 17:15:20 +0000 | [diff] [blame] | 183 | |
| 184 | class Foo: |
| 185 | def method(self, arg1, arg2): |
| 186 | return arg1 + arg2 |
| 187 | |
| 188 | x = Foo() |
| 189 | print Foo.method(*(x, 1, 2)) |
| 190 | print Foo.method(x, *(1, 2)) |
| 191 | try: |
| 192 | print Foo.method(*(1, 2, 3)) |
| 193 | except TypeError, err: |
| 194 | print err |
| 195 | try: |
| 196 | print Foo.method(1, *(2, 3)) |
| 197 | except TypeError, err: |
| 198 | print err |
| 199 | |
Fred Drake | 1a7aab7 | 2001-01-04 22:33:02 +0000 | [diff] [blame] | 200 | # A PyCFunction that takes only positional parameters should allow an |
| 201 | # empty keyword dictionary to pass without a complaint, but raise a |
| 202 | # TypeError if the dictionary is non-empty. |
| 203 | id(1, **{}) |
| 204 | try: |
| 205 | id(1, **{"foo": 1}) |
| 206 | except TypeError: |
| 207 | pass |
| 208 | else: |
| 209 | raise TestFailed, 'expected TypeError; no exception raised' |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 210 | |
| 211 | a, b, d, e, v, k = 'A', 'B', 'D', 'E', 'V', 'K' |
| 212 | funcs = [] |
| 213 | maxargs = {} |
| 214 | for args in ['', 'a', 'ab']: |
| 215 | for defargs in ['', 'd', 'de']: |
| 216 | for vararg in ['', 'v']: |
| 217 | for kwarg in ['', 'k']: |
| 218 | name = 'z' + args + defargs + vararg + kwarg |
| 219 | arglist = list(args) + map( |
| 220 | lambda x: '%s="%s"' % (x, x), defargs) |
| 221 | if vararg: arglist.append('*' + vararg) |
| 222 | if kwarg: arglist.append('**' + kwarg) |
Tim Peters | 2f228e7 | 2001-05-13 00:19:31 +0000 | [diff] [blame^] | 223 | decl = (('def %s(%s): print "ok %s", a, b, d, e, v, ' + |
| 224 | 'type(k) is type ("") and k or sortdict(k)') |
| 225 | % (name, ', '.join(arglist), name)) |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 226 | exec(decl) |
| 227 | func = eval(name) |
| 228 | funcs.append(func) |
| 229 | maxargs[func] = len(args + defargs) |
| 230 | |
| 231 | for name in ['za', 'zade', 'zabk', 'zabdv', 'zabdevk']: |
| 232 | func = eval(name) |
| 233 | for args in [(), (1, 2), (1, 2, 3, 4, 5)]: |
| 234 | for kwargs in ['', 'a', 'd', 'ad', 'abde']: |
| 235 | kwdict = {} |
| 236 | for k in kwargs: kwdict[k] = k + k |
Tim Peters | 08dabf0 | 2001-01-21 18:52:02 +0000 | [diff] [blame] | 237 | print func.func_name, args, sortdict(kwdict), '->', |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 238 | try: apply(func, args, kwdict) |
| 239 | except TypeError, err: print err |