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