Benjamin Peterson | a567a77 | 2010-03-21 21:00:50 +0000 | [diff] [blame] | 1 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 2 | """Doctest for method/function calls. |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 3 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 4 | We're going the use these types for extra testing |
Raymond Hettinger | 40174c3 | 2003-05-31 07:04:16 +0000 | [diff] [blame] | 5 | |
Neal Norwitz | d79bacc | 2008-03-18 20:58:39 +0000 | [diff] [blame] | 6 | >>> from collections import UserList |
| 7 | >>> from collections import UserDict |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 8 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 9 | We're defining four helper functions |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 10 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 11 | >>> def e(a,b): |
Neal Norwitz | d79bacc | 2008-03-18 20:58:39 +0000 | [diff] [blame] | 12 | ... print(a, b) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 13 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 14 | >>> def f(*a, **k): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 15 | ... print(a, support.sortdict(k)) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 16 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 17 | >>> def g(x, *y, **z): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 18 | ... print(x, y, support.sortdict(z)) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 19 | |
| 20 | >>> def h(j=1, a=2, h=3): |
Neal Norwitz | d79bacc | 2008-03-18 20:58:39 +0000 | [diff] [blame] | 21 | ... print(j, a, h) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 22 | |
| 23 | Argument list examples |
| 24 | |
| 25 | >>> f() |
| 26 | () {} |
| 27 | >>> f(1) |
| 28 | (1,) {} |
| 29 | >>> f(1, 2) |
| 30 | (1, 2) {} |
| 31 | >>> f(1, 2, 3) |
| 32 | (1, 2, 3) {} |
| 33 | >>> f(1, 2, 3, *(4, 5)) |
| 34 | (1, 2, 3, 4, 5) {} |
| 35 | >>> f(1, 2, 3, *[4, 5]) |
| 36 | (1, 2, 3, 4, 5) {} |
| 37 | >>> f(1, 2, 3, *UserList([4, 5])) |
| 38 | (1, 2, 3, 4, 5) {} |
| 39 | |
| 40 | Here we add keyword arguments |
| 41 | |
| 42 | >>> f(1, 2, 3, **{'a':4, 'b':5}) |
| 43 | (1, 2, 3) {'a': 4, 'b': 5} |
| 44 | >>> f(1, 2, 3, *[4, 5], **{'a':6, 'b':7}) |
| 45 | (1, 2, 3, 4, 5) {'a': 6, 'b': 7} |
| 46 | >>> f(1, 2, 3, x=4, y=5, *(6, 7), **{'a':8, 'b': 9}) |
| 47 | (1, 2, 3, 6, 7) {'a': 8, 'b': 9, 'x': 4, 'y': 5} |
| 48 | |
| 49 | >>> f(1, 2, 3, **UserDict(a=4, b=5)) |
| 50 | (1, 2, 3) {'a': 4, 'b': 5} |
| 51 | >>> f(1, 2, 3, *(4, 5), **UserDict(a=6, b=7)) |
| 52 | (1, 2, 3, 4, 5) {'a': 6, 'b': 7} |
| 53 | >>> f(1, 2, 3, x=4, y=5, *(6, 7), **UserDict(a=8, b=9)) |
| 54 | (1, 2, 3, 6, 7) {'a': 8, 'b': 9, 'x': 4, 'y': 5} |
| 55 | |
| 56 | Examples with invalid arguments (TypeErrors). We're also testing the function |
| 57 | names in the exception messages. |
| 58 | |
| 59 | Verify clearing of SF bug #733667 |
| 60 | |
| 61 | >>> e(c=4) |
| 62 | Traceback (most recent call last): |
| 63 | ... |
| 64 | TypeError: e() got an unexpected keyword argument 'c' |
| 65 | |
| 66 | >>> g() |
| 67 | Traceback (most recent call last): |
| 68 | ... |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 69 | TypeError: g() takes at least 1 positional argument but 0 were given |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 70 | |
| 71 | >>> g(*()) |
| 72 | Traceback (most recent call last): |
| 73 | ... |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 74 | TypeError: g() takes at least 1 positional argument but 0 were given |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 75 | |
| 76 | >>> g(*(), **{}) |
| 77 | Traceback (most recent call last): |
| 78 | ... |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 79 | TypeError: g() takes at least 1 positional argument but 0 were given |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 80 | |
| 81 | >>> g(1) |
| 82 | 1 () {} |
| 83 | >>> g(1, 2) |
| 84 | 1 (2,) {} |
| 85 | >>> g(1, 2, 3) |
| 86 | 1 (2, 3) {} |
| 87 | >>> g(1, 2, 3, *(4, 5)) |
| 88 | 1 (2, 3, 4, 5) {} |
| 89 | |
| 90 | >>> class Nothing: pass |
| 91 | ... |
| 92 | >>> g(*Nothing()) |
| 93 | Traceback (most recent call last): |
| 94 | ... |
Neal Norwitz | d79bacc | 2008-03-18 20:58:39 +0000 | [diff] [blame] | 95 | TypeError: g() argument after * must be a sequence, not Nothing |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 96 | |
| 97 | >>> class Nothing: |
| 98 | ... def __len__(self): return 5 |
| 99 | ... |
| 100 | |
| 101 | >>> g(*Nothing()) |
| 102 | Traceback (most recent call last): |
| 103 | ... |
Neal Norwitz | d79bacc | 2008-03-18 20:58:39 +0000 | [diff] [blame] | 104 | TypeError: g() argument after * must be a sequence, not Nothing |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 105 | |
| 106 | >>> class Nothing(): |
| 107 | ... def __len__(self): return 5 |
| 108 | ... def __getitem__(self, i): |
| 109 | ... if i<3: return i |
| 110 | ... else: raise IndexError(i) |
| 111 | ... |
| 112 | |
| 113 | >>> g(*Nothing()) |
| 114 | 0 (1, 2) {} |
| 115 | |
| 116 | >>> class Nothing: |
| 117 | ... def __init__(self): self.c = 0 |
| 118 | ... def __iter__(self): return self |
Neal Norwitz | d79bacc | 2008-03-18 20:58:39 +0000 | [diff] [blame] | 119 | ... def __next__(self): |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 120 | ... if self.c == 4: |
| 121 | ... raise StopIteration |
| 122 | ... c = self.c |
| 123 | ... self.c += 1 |
| 124 | ... return c |
| 125 | ... |
| 126 | |
| 127 | >>> g(*Nothing()) |
| 128 | 0 (1, 2, 3) {} |
| 129 | |
| 130 | Make sure that the function doesn't stomp the dictionary |
| 131 | |
| 132 | >>> d = {'a': 1, 'b': 2, 'c': 3} |
| 133 | >>> d2 = d.copy() |
| 134 | >>> g(1, d=4, **d) |
| 135 | 1 () {'a': 1, 'b': 2, 'c': 3, 'd': 4} |
| 136 | >>> d == d2 |
| 137 | True |
| 138 | |
| 139 | What about willful misconduct? |
| 140 | |
| 141 | >>> def saboteur(**kw): |
| 142 | ... kw['x'] = 'm' |
| 143 | ... return kw |
| 144 | |
| 145 | >>> d = {} |
| 146 | >>> kw = saboteur(a=1, **d) |
| 147 | >>> d |
| 148 | {} |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 149 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 150 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 151 | >>> g(1, 2, 3, **{'x': 4, 'y': 5}) |
| 152 | Traceback (most recent call last): |
| 153 | ... |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 154 | TypeError: g() got multiple values for argument 'x' |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 155 | |
| 156 | >>> f(**{1:2}) |
| 157 | Traceback (most recent call last): |
| 158 | ... |
| 159 | TypeError: f() keywords must be strings |
| 160 | |
| 161 | >>> h(**{'e': 2}) |
| 162 | Traceback (most recent call last): |
| 163 | ... |
| 164 | TypeError: h() got an unexpected keyword argument 'e' |
| 165 | |
| 166 | >>> h(*h) |
| 167 | Traceback (most recent call last): |
| 168 | ... |
| 169 | TypeError: h() argument after * must be a sequence, not function |
| 170 | |
| 171 | >>> dir(*h) |
| 172 | Traceback (most recent call last): |
| 173 | ... |
| 174 | TypeError: dir() argument after * must be a sequence, not function |
| 175 | |
| 176 | >>> None(*h) |
| 177 | Traceback (most recent call last): |
| 178 | ... |
| 179 | TypeError: NoneType object argument after * must be a sequence, \ |
| 180 | not function |
| 181 | |
| 182 | >>> h(**h) |
| 183 | Traceback (most recent call last): |
| 184 | ... |
| 185 | TypeError: h() argument after ** must be a mapping, not function |
| 186 | |
| 187 | >>> dir(**h) |
| 188 | Traceback (most recent call last): |
| 189 | ... |
| 190 | TypeError: dir() argument after ** must be a mapping, not function |
| 191 | |
| 192 | >>> None(**h) |
| 193 | Traceback (most recent call last): |
| 194 | ... |
| 195 | TypeError: NoneType object argument after ** must be a mapping, \ |
| 196 | not function |
| 197 | |
| 198 | >>> dir(b=1, **{'b': 1}) |
| 199 | Traceback (most recent call last): |
| 200 | ... |
| 201 | TypeError: dir() got multiple values for keyword argument 'b' |
| 202 | |
| 203 | Another helper function |
| 204 | |
| 205 | >>> def f2(*a, **b): |
| 206 | ... return a, b |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 207 | |
| 208 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 209 | >>> d = {} |
Neal Norwitz | d79bacc | 2008-03-18 20:58:39 +0000 | [diff] [blame] | 210 | >>> for i in range(512): |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 211 | ... key = 'k%d' % i |
| 212 | ... d[key] = i |
| 213 | >>> a, b = f2(1, *(2,3), **d) |
| 214 | >>> len(a), len(b), b == d |
| 215 | (3, 512, True) |
Raymond Hettinger | 40174c3 | 2003-05-31 07:04:16 +0000 | [diff] [blame] | 216 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 217 | >>> class Foo: |
| 218 | ... def method(self, arg1, arg2): |
| 219 | ... return arg1+arg2 |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 220 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 221 | >>> x = Foo() |
| 222 | >>> Foo.method(*(x, 1, 2)) |
| 223 | 3 |
| 224 | >>> Foo.method(x, *(1, 2)) |
| 225 | 3 |
| 226 | >>> Foo.method(*(1, 2, 3)) |
Neal Norwitz | d79bacc | 2008-03-18 20:58:39 +0000 | [diff] [blame] | 227 | 5 |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 228 | >>> Foo.method(1, *[2, 3]) |
Neal Norwitz | d79bacc | 2008-03-18 20:58:39 +0000 | [diff] [blame] | 229 | 5 |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 230 | |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 231 | A PyCFunction that takes only positional parameters should allow an |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 232 | empty keyword dictionary to pass without a complaint, but raise a |
| 233 | TypeError if te dictionary is not empty |
Jeremy Hylton | 074c3e6 | 2000-03-30 23:55:31 +0000 | [diff] [blame] | 234 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 235 | >>> try: |
| 236 | ... silence = id(1, *{}) |
| 237 | ... True |
| 238 | ... except: |
| 239 | ... False |
| 240 | True |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 241 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 242 | >>> id(1, **{'foo': 1}) |
| 243 | Traceback (most recent call last): |
| 244 | ... |
| 245 | TypeError: id() takes no keyword arguments |
Jeremy Hylton | 074c3e6 | 2000-03-30 23:55:31 +0000 | [diff] [blame] | 246 | |
Benjamin Peterson | 0289b15 | 2009-06-28 17:22:03 +0000 | [diff] [blame] | 247 | A corner case of keyword dictionary items being deleted during |
| 248 | the function call setup. See <http://bugs.python.org/issue2016>. |
| 249 | |
| 250 | >>> class Name(str): |
| 251 | ... def __eq__(self, other): |
| 252 | ... try: |
| 253 | ... del x[self] |
| 254 | ... except KeyError: |
| 255 | ... pass |
| 256 | ... return str.__eq__(self, other) |
| 257 | ... def __hash__(self): |
| 258 | ... return str.__hash__(self) |
| 259 | |
| 260 | >>> x = {Name("a"):1, Name("b"):2} |
| 261 | >>> def f(a, b): |
| 262 | ... print(a,b) |
| 263 | >>> f(**x) |
| 264 | 1 2 |
Benjamin Peterson | a567a77 | 2010-03-21 21:00:50 +0000 | [diff] [blame] | 265 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 266 | Some additional tests about positional argument errors: |
Benjamin Peterson | a567a77 | 2010-03-21 21:00:50 +0000 | [diff] [blame] | 267 | |
| 268 | >>> def f(a, b): |
| 269 | ... pass |
| 270 | >>> f(b=1) |
| 271 | Traceback (most recent call last): |
| 272 | ... |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 273 | TypeError: f() takes 2 positional arguments but 1 was given |
Benjamin Peterson | a567a77 | 2010-03-21 21:00:50 +0000 | [diff] [blame] | 274 | |
| 275 | >>> def f(a): |
| 276 | ... pass |
| 277 | >>> f(6, a=4, *(1, 2, 3)) |
| 278 | Traceback (most recent call last): |
| 279 | ... |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 280 | TypeError: f() got multiple values for argument 'a' |
Benjamin Peterson | afcee8b | 2010-03-21 21:16:24 +0000 | [diff] [blame] | 281 | >>> def f(a, *, kw): |
| 282 | ... pass |
| 283 | >>> f(6, 4, kw=4) |
| 284 | Traceback (most recent call last): |
| 285 | ... |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 286 | TypeError: f() takes 1 positional argument but 2 positional arguments (and 1 keyword-only argument) were given |
| 287 | |
| 288 | >>> def f(a): |
| 289 | ... pass |
| 290 | >>> f() |
| 291 | Traceback (most recent call last): |
| 292 | ... |
| 293 | TypeError: f() takes 1 positional argument but 0 were given |
| 294 | |
| 295 | >>> def f(a, b): |
| 296 | ... pass |
| 297 | >>> f(1) |
| 298 | Traceback (most recent call last): |
| 299 | ... |
| 300 | TypeError: f() takes 2 positional arguments but 1 was given |
| 301 | |
| 302 | >>> def f(a, *b): |
| 303 | ... pass |
| 304 | >>> f() |
| 305 | Traceback (most recent call last): |
| 306 | ... |
| 307 | TypeError: f() takes at least 1 positional argument but 0 were given |
| 308 | |
| 309 | >>> def f(a, *, kw=4): |
| 310 | ... pass |
| 311 | >>> f(kw=4) |
| 312 | Traceback (most recent call last): |
| 313 | ... |
| 314 | TypeError: f() takes 1 positional argument but 0 positional arguments (and 1 keyword-only argument) were given |
| 315 | |
| 316 | >>> def f(a, b=2): |
| 317 | ... pass |
| 318 | >>> f() |
| 319 | Traceback (most recent call last): |
| 320 | ... |
| 321 | TypeError: f() takes from 1 to 2 positional arguments but 0 were given |
| 322 | |
| 323 | >>> def f(a, *b): |
| 324 | ... pass |
| 325 | >>> f() |
| 326 | Traceback (most recent call last): |
| 327 | ... |
| 328 | TypeError: f() takes at least 1 positional argument but 0 were given |
| 329 | |
| 330 | >>> def f(*, kw): |
| 331 | ... pass |
| 332 | >>> f(3, kw=4) |
| 333 | Traceback (most recent call last): |
| 334 | ... |
| 335 | TypeError: f() takes 0 positional arguments but 1 positional argument (and 1 keyword-only argument) were given |
| 336 | |
| 337 | >>> def f(a, c=3, *b, kw): |
| 338 | ... pass |
| 339 | >>> f() |
| 340 | Traceback (most recent call last): |
| 341 | ... |
| 342 | TypeError: f() takes at least 1 positional argument but 0 were given |
| 343 | >>> f(kw=3) |
| 344 | Traceback (most recent call last): |
| 345 | ... |
| 346 | TypeError: f() takes at least 1 positional argument but 0 positional arguments (and 1 keyword-only argument) were given |
| 347 | >>> f(kw=3, c=4) |
| 348 | Traceback (most recent call last): |
| 349 | ... |
| 350 | TypeError: f() takes at least 1 positional argument but 1 positional argument (and 1 keyword-only argument) were given |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 351 | """ |
Samuele Pedroni | 8036c83 | 2004-02-21 21:03:30 +0000 | [diff] [blame] | 352 | |
Benjamin Peterson | a567a77 | 2010-03-21 21:00:50 +0000 | [diff] [blame] | 353 | import sys |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 354 | from test import support |
Samuele Pedroni | 8036c83 | 2004-02-21 21:03:30 +0000 | [diff] [blame] | 355 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 356 | def test_main(): |
Benjamin Peterson | a567a77 | 2010-03-21 21:00:50 +0000 | [diff] [blame] | 357 | support.run_doctest(sys.modules[__name__], True) |
Jeremy Hylton | 074c3e6 | 2000-03-30 23:55:31 +0000 | [diff] [blame] | 358 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 359 | if __name__ == '__main__': |
| 360 | test_main() |