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