1 """Doctest for method/function calls.
3 We're going the use these types for extra testing
5 >>> from collections import UserList
6 >>> from collections import UserDict
8 We're defining four helper functions
14 ... print(a, support.sortdict(k))
16 >>> def g(x, *y, **z):
17 ... print(x, y, support.sortdict(z))
19 >>> def h(j=1, a=2, h=3):
22 Argument list examples
32 >>> f(1, 2, 3, *(4, 5))
34 >>> f(1, 2, 3, *[4, 5])
36 >>> f(1, 2, 3, *UserList([4, 5]))
39 Here we add keyword arguments
41 >>> f(1, 2, 3, **{'a':4, 'b':5})
42 (1, 2, 3) {'a': 4, 'b': 5}
43 >>> f(1, 2, 3, *[4, 5], **{'a':6, 'b':7})
44 (1, 2, 3, 4, 5) {'a': 6, 'b': 7}
45 >>> f(1, 2, 3, x=4, y=5, *(6, 7), **{'a':8, 'b': 9})
46 (1, 2, 3, 6, 7) {'a': 8, 'b': 9, 'x': 4, 'y': 5}
48 >>> f(1, 2, 3, **UserDict(a=4, b=5))
49 (1, 2, 3) {'a': 4, 'b': 5}
50 >>> f(1, 2, 3, *(4, 5), **UserDict(a=6, b=7))
51 (1, 2, 3, 4, 5) {'a': 6, 'b': 7}
52 >>> f(1, 2, 3, x=4, y=5, *(6, 7), **UserDict(a=8, b=9))
53 (1, 2, 3, 6, 7) {'a': 8, 'b': 9, 'x': 4, 'y': 5}
55 Examples with invalid arguments (TypeErrors). We're also testing the function
56 names in the exception messages.
58 Verify clearing of SF bug #733667
61 Traceback (most recent call last):
63 TypeError: e() got an unexpected keyword argument 'c'
66 Traceback (most recent call last):
68 TypeError: g() takes at least 1 positional argument (0 given)
71 Traceback (most recent call last):
73 TypeError: g() takes at least 1 positional argument (0 given)
76 Traceback (most recent call last):
78 TypeError: g() takes at least 1 positional argument (0 given)
86 >>> g(1, 2, 3, *(4, 5))
89 >>> class Nothing: pass
92 Traceback (most recent call last):
94 TypeError: g() argument after * must be a sequence, not Nothing
97 ... def __len__(self): return 5
101 Traceback (most recent call last):
103 TypeError: g() argument after * must be a sequence, not Nothing
106 ... def __len__(self): return 5
107 ... def __getitem__(self, i):
109 ... else: raise IndexError(i)
116 ... def __init__(self): self.c = 0
117 ... def __iter__(self): return self
118 ... def __next__(self):
120 ... raise StopIteration
129 Make sure that the function doesn't stomp the dictionary
131 >>> d = {'a': 1, 'b': 2, 'c': 3}
134 1 () {'a': 1, 'b': 2, 'c': 3, 'd': 4}
138 What about willful misconduct?
140 >>> def saboteur(**kw):
145 >>> kw = saboteur(a=1, **d)
150 >>> g(1, 2, 3, **{'x': 4, 'y': 5})
151 Traceback (most recent call last):
153 TypeError: g() got multiple values for keyword argument 'x'
156 Traceback (most recent call last):
158 TypeError: f() keywords must be strings
161 Traceback (most recent call last):
163 TypeError: h() got an unexpected keyword argument 'e'
166 Traceback (most recent call last):
168 TypeError: h() argument after * must be a sequence, not function
171 Traceback (most recent call last):
173 TypeError: dir() argument after * must be a sequence, not function
176 Traceback (most recent call last):
178 TypeError: NoneType object argument after * must be a sequence, \
182 Traceback (most recent call last):
184 TypeError: h() argument after ** must be a mapping, not function
187 Traceback (most recent call last):
189 TypeError: dir() argument after ** must be a mapping, not function
192 Traceback (most recent call last):
194 TypeError: NoneType object argument after ** must be a mapping, \
197 >>> dir(b=1, **{'b': 1})
198 Traceback (most recent call last):
200 TypeError: dir() got multiple values for keyword argument 'b'
202 Another helper function
209 >>> for i in range(512):
212 >>> a, b = f2(1, *(2,3), **d)
213 >>> len(a), len(b), b == d
217 ... def method(self, arg1, arg2):
221 >>> Foo.method(*(x, 1, 2))
223 >>> Foo.method(x, *(1, 2))
225 >>> Foo.method(*(1, 2, 3))
227 >>> Foo.method(1, *[2, 3])
230 A PyCFunction that takes only positional parameters shoud allow an
231 empty keyword dictionary to pass without a complaint, but raise a
232 TypeError if te dictionary is not empty
235 ... silence = id(1, *{})
241 >>> id(1, **{'foo': 1})
242 Traceback (most recent call last):
244 TypeError: id() takes no keyword arguments
246 A corner case of keyword dictionary items being deleted during
247 the function call setup. See <http://bugs.python.org/issue2016>.
250 ... def __eq__(self, other):
255 ... return str.__eq__(self, other)
256 ... def __hash__(self):
257 ... return str.__hash__(self)
259 >>> x = {Name("a"):1, Name("b"):2}
266 from test
import support
269 from test
import test_extcall
# self import
270 support
.run_doctest(test_extcall
, True)
272 if __name__
== '__main__':