Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 1 | from UserList import UserList |
| 2 | |
| 3 | def f(*a, **k): |
| 4 | print a, k |
| 5 | |
| 6 | def g(x, *y, **z): |
| 7 | print x, y, z |
| 8 | |
| 9 | def h(j=1, a=2, h=3): |
| 10 | print j, a, h |
| 11 | |
| 12 | f() |
| 13 | f(1) |
| 14 | f(1, 2) |
| 15 | f(1, 2, 3) |
| 16 | |
| 17 | f(1, 2, 3, *(4, 5)) |
| 18 | f(1, 2, 3, *[4, 5]) |
Jeremy Hylton | 003663d | 2000-03-28 23:53:22 +0000 | [diff] [blame] | 19 | f(1, 2, 3, *UserList([4, 5])) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 20 | f(1, 2, 3, **{'a':4, 'b':5}) |
| 21 | f(1, 2, 3, *(4, 5), **{'a':6, 'b':7}) |
| 22 | f(1, 2, 3, x=4, y=5, *(6, 7), **{'a':8, 'b':9}) |
| 23 | |
| 24 | try: |
| 25 | g() |
| 26 | except TypeError, err: |
| 27 | print "TypeError:", err |
| 28 | else: |
| 29 | print "should raise TypeError: not enough arguments; expected 1, got 0" |
| 30 | |
| 31 | try: |
| 32 | g(*()) |
| 33 | except TypeError, err: |
| 34 | print "TypeError:", err |
| 35 | else: |
| 36 | print "should raise TypeError: not enough arguments; expected 1, got 0" |
| 37 | |
| 38 | try: |
| 39 | g(*(), **{}) |
| 40 | except TypeError, err: |
| 41 | print "TypeError:", err |
| 42 | else: |
| 43 | print "should raise TypeError: not enough arguments; expected 1, got 0" |
| 44 | |
| 45 | g(1) |
| 46 | g(1, 2) |
| 47 | g(1, 2, 3) |
| 48 | g(1, 2, 3, *(4, 5)) |
Jeremy Hylton | 074c3e6 | 2000-03-30 23:55:31 +0000 | [diff] [blame] | 49 | class Nothing: pass |
| 50 | try: |
| 51 | g(*Nothing()) |
| 52 | except AttributeError, attr: |
Guido van Rossum | 4b49101 | 2000-04-10 13:37:14 +0000 | [diff] [blame] | 53 | pass |
Jeremy Hylton | 074c3e6 | 2000-03-30 23:55:31 +0000 | [diff] [blame] | 54 | else: |
| 55 | print "should raise AttributeError: __len__" |
| 56 | |
| 57 | class Nothing: |
| 58 | def __len__(self): |
| 59 | return 5 |
| 60 | try: |
| 61 | g(*Nothing()) |
| 62 | except AttributeError, attr: |
Guido van Rossum | 4b49101 | 2000-04-10 13:37:14 +0000 | [diff] [blame] | 63 | pass |
Jeremy Hylton | 074c3e6 | 2000-03-30 23:55:31 +0000 | [diff] [blame] | 64 | else: |
| 65 | print "should raise AttributeError: __getitem__" |
| 66 | |
| 67 | class Nothing: |
| 68 | def __len__(self): |
| 69 | return 5 |
| 70 | def __getitem__(self, i): |
| 71 | if i < 3: |
| 72 | return i |
| 73 | else: |
| 74 | raise IndexError, i |
| 75 | g(*Nothing()) |
| 76 | |
| 77 | # make sure the function call doesn't stomp on the dictionary? |
| 78 | d = {'a': 1, 'b': 2, 'c': 3} |
| 79 | d2 = d.copy() |
| 80 | assert d == d2 |
| 81 | g(1, d=4, **d) |
| 82 | print d |
| 83 | print d2 |
| 84 | assert d == d2, "function call modified dictionary" |
| 85 | |
| 86 | # what about willful misconduct? |
| 87 | def saboteur(**kw): |
Vladimir Marangozov | 5ff2ac2 | 2000-07-15 00:42:09 +0000 | [diff] [blame^] | 88 | kw['x'] = locals() # yields a cyclic kw |
| 89 | return kw |
Jeremy Hylton | 074c3e6 | 2000-03-30 23:55:31 +0000 | [diff] [blame] | 90 | d = {} |
Vladimir Marangozov | 5ff2ac2 | 2000-07-15 00:42:09 +0000 | [diff] [blame^] | 91 | kw = saboteur(a=1, **d) |
Jeremy Hylton | 074c3e6 | 2000-03-30 23:55:31 +0000 | [diff] [blame] | 92 | assert d == {} |
Vladimir Marangozov | 5ff2ac2 | 2000-07-15 00:42:09 +0000 | [diff] [blame^] | 93 | # break the cycle |
| 94 | del kw['x'] |
Jeremy Hylton | 074c3e6 | 2000-03-30 23:55:31 +0000 | [diff] [blame] | 95 | |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 96 | try: |
| 97 | g(1, 2, 3, **{'x':4, 'y':5}) |
| 98 | except TypeError, err: |
| 99 | print err |
| 100 | else: |
| 101 | print "should raise TypeError: keyword parameter redefined" |
| 102 | |
| 103 | try: |
| 104 | g(1, 2, 3, a=4, b=5, *(6, 7), **{'a':8, 'b':9}) |
| 105 | except TypeError, err: |
| 106 | print err |
| 107 | else: |
| 108 | print "should raise TypeError: keyword parameter redefined" |
| 109 | |
| 110 | try: |
| 111 | f(**{1:2}) |
| 112 | except TypeError, err: |
| 113 | print err |
| 114 | else: |
| 115 | print "should raise TypeError: keywords must be strings" |
| 116 | |
| 117 | try: |
| 118 | h(**{'e': 2}) |
| 119 | except TypeError, err: |
| 120 | print err |
| 121 | else: |
| 122 | print "should raise TypeError: unexpected keyword argument: e" |
| 123 | |
| 124 | try: |
| 125 | h(*h) |
| 126 | except TypeError, err: |
| 127 | print err |
| 128 | else: |
| 129 | print "should raise TypeError: * argument must be a tuple" |
| 130 | |
| 131 | try: |
| 132 | h(**h) |
| 133 | except TypeError, err: |
| 134 | print err |
| 135 | else: |
| 136 | print "should raise TypeError: ** argument must be a dictionary" |
| 137 | |
| 138 | def f2(*a, **b): |
| 139 | return a, b |
| 140 | |
| 141 | d = {} |
| 142 | for i in range(512): |
| 143 | key = 'k%d' % i |
| 144 | d[key] = i |
| 145 | a, b = f2(1, *(2, 3), **d) |
| 146 | print len(a), len(b), b == d |