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