4
from __future__ import print_function
8
>>> try: errors_call_no_args(f)
9
... except TypeError: pass
10
... else: print("FAILED!")
12
>>> def f(*, a, b, c, d, e): pass
13
>>> try: errors_call_no_args(f)
14
... except TypeError: pass
15
... else: print("FAILED!")
17
>>> def f(*, kw, b): pass
18
>>> try: errors_call_3args_2kwargs(f)
19
... except TypeError: pass
20
... else: print("FAILED!")
22
>>> def f(a, b=2, *, kw): pass
23
>>> try: errors_call_3args_1kwarg(f)
24
... except TypeError: pass
25
... else: print("FAILED!")
28
>>> try: errors_call_1arg_1kwarg(f)
29
... except TypeError: pass
30
... else: print("FAILED!")
33
# test for method/function calls. adapted from CPython's "test_extcall.py".
36
return '{%s}' % ', '.join(['%r: %r' % item for item in sorted(d.items())])
38
# We're going the use these types for extra testing
41
from collections import UserList, UserDict
43
from UserList import UserList
44
from UserDict import UserDict
47
# We're defining four helper functions
56
print(x, y, sortdict(z))
62
# Argument list examples
64
def call_f_positional():
66
>>> call_f_positional()
75
(1, 2, 3, 4, 5, 6, 7) {}
76
(1, 2, 3, 4, 5, 6, 7) {}
77
(1, 2, 3, 4, 5, 6, 7) {}
87
f(1, 2, 3, *UserList([4, 5]))
88
f(1, 2, 3, *[4, 5], *[6, 7])
89
f(1, *[2, 3], 4, *[5, 6], 7)
90
f(*UserList([1, 2]), *UserList([3, 4]), 5, *UserList([6, 7]))
91
f(1, *[] or () and {}, *() and [], *{} or [] and (), *{} and [] or (), 2)
94
# Here we add keyword arguments
99
(1, 2, 3) {'a': 4, 'b': 5}
100
(1, 2, 3, 4, 5) {'a': 6, 'b': 7}
101
(1, 2, 3, 6, 7) {'a': 8, 'b': 9, 'x': 4, 'y': 5}
102
(1, 2, 3, 4, 5) {'a': 6, 'b': 7, 'c': 8}
103
(1, 2, 3, 4, 5) {'a': 8, 'b': 9, 'x': 6, 'y': 7}
104
(1, 2, 3) {'a': 4, 'b': 5}
105
(1, 2, 3, 4, 5) {'a': 6, 'b': 7}
106
(1, 2, 3, 6, 7) {'a': 8, 'b': 9, 'x': 4, 'y': 5}
107
(1, 2, 3, 4, 5) {'a': 8, 'b': 9, 'x': 6, 'y': 7}
111
f(1, 2, 3, **{'a':4, 'b':5})
112
f(1, 2, 3, *[4, 5], **{'a':6, 'b':7})
113
f(1, 2, 3, x=4, y=5, *(6, 7), **{'a':8, 'b': 9})
114
f(1, 2, 3, *[4, 5], **{'c': 8}, **{'a':6, 'b':7})
115
f(1, 2, 3, *(4, 5), x=6, y=7, **{'a':8, 'b': 9})
117
f(1, 2, 3, **UserDict(a=4, b=5))
118
f(1, 2, 3, *(4, 5), **UserDict(a=6, b=7))
119
f(1, 2, 3, x=4, y=5, *(6, 7), **UserDict(a=8, b=9))
120
f(1, 2, 3, *(4, 5), x=6, y=7, **UserDict(a=8, b=9))
122
f(1, *[] or () and {}, *() and [], *{} or [] and (), *{} and [] or (), 2,
123
**{} and {} or {}, **{} or {} and {}, **{} and {}, a=3)
126
# Examples with invalid arguments (TypeErrors). We're also testing the function
127
# names in the exception messages.
129
# Verify clearing of SF bug #733667
133
>>> errors_f1() # doctest: +ELLIPSIS
134
Traceback (most recent call last):
136
TypeError: ...got multiple values for keyword argument 'a'
138
f(1, 2, **{'a': -1, 'b': 5}, **{'a': 4, 'c': 6})
143
>>> errors_f2() # doctest: +ELLIPSIS
144
Traceback (most recent call last):
146
TypeError: ...multiple values for keyword argument 'a'
148
f(1, 2, **{'a': -1, 'b': 5}, a=4, c=6)
154
... except TypeError: pass
155
... else: print("FAILED!")
163
... except TypeError: pass
164
... else: print("FAILED!")
172
Traceback (most recent call last):
174
TypeError: g() takes at least 1 positional argument (0 given)
176
# TypeError: g() missing 1 required positional argument: 'x'
184
Traceback (most recent call last):
186
TypeError: g() takes at least 1 positional argument (0 given)
188
# TypeError: g() missing 1 required positional argument: 'x'
196
Traceback (most recent call last):
198
TypeError: g() takes at least 1 positional argument (0 given)
200
# TypeError: g() missing 1 required positional argument: 'x'
205
def call_g_positional():
207
>>> call_g_positional()
220
def call_nonseq_positional1():
222
>>> call_nonseq_positional1() # doctest: +ELLIPSIS
223
Traceback (most recent call last):
225
TypeError: ...Nothing...
227
# TypeError: g() argument after * must be a sequence, not Nothing
229
class Nothing(object): pass
233
def call_nonseq_positional2():
235
>>> call_nonseq_positional2() # doctest: +ELLIPSIS
236
Traceback (most recent call last):
238
TypeError: ...Nothing...
240
# TypeError: g() argument after * must be a sequence, not Nothing
242
class Nothing(object):
243
def __len__(self): return 5
247
def call_seqlike_positional1():
249
>>> call_seqlike_positional1()
252
class Nothing(object):
253
def __len__(self): return 5
254
def __getitem__(self, i):
256
else: raise IndexError(i)
261
def call_seqlike_positional2():
263
>>> call_seqlike_positional2()
267
def __init__(self): self.c = 0
268
def __iter__(self): return self
280
# Make sure that the function doesn't stomp the dictionary
282
def call_kwargs_unmodified1():
284
>>> call_kwargs_unmodified1()
285
1 () {'a': 1, 'b': 2, 'c': 3, 'd': 4}
288
d = {'a': 1, 'b': 2, 'c': 3}
294
# What about willful misconduct?
296
def call_kwargs_unmodified2():
298
>>> call_kwargs_unmodified2()
306
kw = saboteur(a=1, **d)
310
def errors_args_kwargs_overlap():
312
>>> errors_args_kwargs_overlap() # doctest: +ELLIPSIS
313
Traceback (most recent call last):
315
TypeError: ...got multiple values for... argument 'x'
317
g(1, 2, 3, **{'x': 4, 'y': 5})
320
def errors_non_string_kwarg():
322
>>> errors_non_string_kwarg() # doctest: +ELLIPSIS
323
Traceback (most recent call last):
324
TypeError: ...keywords must be strings...
329
def errors_unexpected_kwarg():
331
>>> errors_unexpected_kwarg()
332
Traceback (most recent call last):
334
TypeError: h() got an unexpected keyword argument 'e'
339
def errors_call_nonseq():
341
>>> try: errors_call_nonseq()
342
... except TypeError: pass
343
... else: print("FAILED!")
348
def errors_call_builtin_nonseq():
350
>>> try: errors_call_builtin_nonseq()
351
... except TypeError: pass
352
... else: print("FAILED!")
357
def errors_call_none_nonseq():
359
>>> try: errors_call_none_nonseq()
360
... except TypeError: pass
361
... else: print("FAILED!")
366
def errors_call_nonmapping_kwargs():
368
>>> try: errors_call_nonmapping_kwargs()
369
... except TypeError: pass
370
... else: print("FAILED!")
375
def errors_call_builtin_nonmapping_kwargs():
377
>>> try: errors_call_builtin_nonmapping_kwargs()
378
... except TypeError: pass
379
... else: print("FAILED!")
384
def errors_call_none_nonmapping_kwargs():
386
>>> try: errors_call_none_nonmapping_kwargs()
387
... except TypeError: pass
388
... else: print("FAILED!")
393
''' # compile time error in Cython
394
def errors_call_builtin_duplicate_kwarg():
396
>>> errors_call_builtin_duplicate_kwarg() # doctest: +ELLIPSIS
397
Traceback (most recent call last):
399
TypeError: ...got multiple values for keyword argument 'b'
405
# Another helper function
411
def call_many_kwargs():
420
a, b = f2(1, *(2,3), **d)
421
return len(a), len(b), b == d
426
>>> class Foo(object):
427
... def method(self, arg1, arg2):
437
Foo.method(*(x, 1, 2))
438
Foo.method(x, *(1, 2))
439
Foo.method(*(1, 2, 3))
440
Foo.method(1, *[2, 3])
443
# A PyCFunction that takes only positional parameters should allow an
444
# empty keyword dictionary to pass without a complaint, but raise a
445
# TypeError if the dictionary is not empty
447
def call_builtin_empty_dict():
449
>>> call_builtin_empty_dict()
452
silence = id(1, **{})
455
def call_builtin_nonempty_dict():
457
>>> call_builtin_nonempty_dict() # doctest: +ELLIPSIS
458
Traceback (most recent call last):
460
TypeError: id() ... keyword argument...
462
return id(1, **{'foo': 1})
465
''' Cython: currently just passes empty kwargs into f() while CPython keeps the content
467
# A corner case of keyword dictionary items being deleted during
468
# the function call setup. See <https://bugs.python.org/issue2016>.
470
def call_kwargs_modified_while_building():
472
>>> call_kwargs_modified_while_building()
476
def __eq__(self, other):
481
return str.__eq__(self, other)
483
return str.__hash__(self)
485
x = {Name("a"):1, Name("b"):2}
494
def errors_call_one_arg(f):
497
>>> try: errors_call_one_arg(f)
498
... except TypeError: pass
499
... else: print("FAILED!")
503
def errors_call_2args(f):
506
>>> try: errors_call_2args(f)
507
... except TypeError: pass
508
... else: print("FAILED!")
512
def errors_call_3args(f):
514
>>> def f(a, b=1): pass
515
>>> try: errors_call_3args(f)
516
... except TypeError: pass
517
... else: print("FAILED!")
522
def errors_call_1arg_1kwarg(f):
527
def errors_call_3args_2kwargs(f):
529
f(1, 2, 3, b=3, kw=3)
532
def errors_call_3args_1kwarg(f):
537
# Too few and missing arguments:
539
def errors_call_no_args(f):
542
>>> try: errors_call_no_args(f)
543
... except TypeError: pass
544
... else: print("FAILED!")
546
>>> def f(a, b): pass
547
>>> try: errors_call_no_args(f)
548
... except TypeError: pass
549
... else: print("FAILED!")
551
>>> def f(a, b, c): pass
552
>>> try: errors_call_no_args(f)
553
... except TypeError: pass
554
... else: print("FAILED!")
556
>>> def f(a, b, c, d, e): pass
557
>>> try: errors_call_no_args(f)
558
... except TypeError: pass
559
... else: print("FAILED!")
564
def errors_call_one_missing_kwarg(f):
566
>>> def f(a, b=4, c=5, d=5): pass
567
>>> try: errors_call_one_missing_kwarg(f)
568
... except TypeError: pass
569
... else: print("FAILED!")