Merged revisions 73623-73624 via svnmerge from
[python/dscho.git] / Lib / test / test_extcall.py
blobf1fff0a5ecaef2183b45c82c1b03ceacbe3b2e0b
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
10 >>> def e(a,b):
11 ... print(a, b)
13 >>> def f(*a, **k):
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):
20 ... print(j, a, h)
22 Argument list examples
24 >>> f()
25 () {}
26 >>> f(1)
27 (1,) {}
28 >>> f(1, 2)
29 (1, 2) {}
30 >>> f(1, 2, 3)
31 (1, 2, 3) {}
32 >>> f(1, 2, 3, *(4, 5))
33 (1, 2, 3, 4, 5) {}
34 >>> f(1, 2, 3, *[4, 5])
35 (1, 2, 3, 4, 5) {}
36 >>> f(1, 2, 3, *UserList([4, 5]))
37 (1, 2, 3, 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
60 >>> e(c=4)
61 Traceback (most recent call last):
62 ...
63 TypeError: e() got an unexpected keyword argument 'c'
65 >>> g()
66 Traceback (most recent call last):
67 ...
68 TypeError: g() takes at least 1 positional argument (0 given)
70 >>> g(*())
71 Traceback (most recent call last):
72 ...
73 TypeError: g() takes at least 1 positional argument (0 given)
75 >>> g(*(), **{})
76 Traceback (most recent call last):
77 ...
78 TypeError: g() takes at least 1 positional argument (0 given)
80 >>> g(1)
81 1 () {}
82 >>> g(1, 2)
83 1 (2,) {}
84 >>> g(1, 2, 3)
85 1 (2, 3) {}
86 >>> g(1, 2, 3, *(4, 5))
87 1 (2, 3, 4, 5) {}
89 >>> class Nothing: pass
90 ...
91 >>> g(*Nothing())
92 Traceback (most recent call last):
93 ...
94 TypeError: g() argument after * must be a sequence, not Nothing
96 >>> class Nothing:
97 ... def __len__(self): return 5
98 ...
100 >>> g(*Nothing())
101 Traceback (most recent call last):
103 TypeError: g() argument after * must be a sequence, not Nothing
105 >>> class Nothing():
106 ... def __len__(self): return 5
107 ... def __getitem__(self, i):
108 ... if i<3: return i
109 ... else: raise IndexError(i)
112 >>> g(*Nothing())
113 0 (1, 2) {}
115 >>> class Nothing:
116 ... def __init__(self): self.c = 0
117 ... def __iter__(self): return self
118 ... def __next__(self):
119 ... if self.c == 4:
120 ... raise StopIteration
121 ... c = self.c
122 ... self.c += 1
123 ... return c
126 >>> g(*Nothing())
127 0 (1, 2, 3) {}
129 Make sure that the function doesn't stomp the dictionary
131 >>> d = {'a': 1, 'b': 2, 'c': 3}
132 >>> d2 = d.copy()
133 >>> g(1, d=4, **d)
134 1 () {'a': 1, 'b': 2, 'c': 3, 'd': 4}
135 >>> d == d2
136 True
138 What about willful misconduct?
140 >>> def saboteur(**kw):
141 ... kw['x'] = 'm'
142 ... return kw
144 >>> d = {}
145 >>> kw = saboteur(a=1, **d)
146 >>> 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'
155 >>> f(**{1:2})
156 Traceback (most recent call last):
158 TypeError: f() keywords must be strings
160 >>> h(**{'e': 2})
161 Traceback (most recent call last):
163 TypeError: h() got an unexpected keyword argument 'e'
165 >>> h(*h)
166 Traceback (most recent call last):
168 TypeError: h() argument after * must be a sequence, not function
170 >>> dir(*h)
171 Traceback (most recent call last):
173 TypeError: dir() argument after * must be a sequence, not function
175 >>> None(*h)
176 Traceback (most recent call last):
178 TypeError: NoneType object argument after * must be a sequence, \
179 not function
181 >>> h(**h)
182 Traceback (most recent call last):
184 TypeError: h() argument after ** must be a mapping, not function
186 >>> dir(**h)
187 Traceback (most recent call last):
189 TypeError: dir() argument after ** must be a mapping, not function
191 >>> None(**h)
192 Traceback (most recent call last):
194 TypeError: NoneType object argument after ** must be a mapping, \
195 not function
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
204 >>> def f2(*a, **b):
205 ... return a, b
208 >>> d = {}
209 >>> for i in range(512):
210 ... key = 'k%d' % i
211 ... d[key] = i
212 >>> a, b = f2(1, *(2,3), **d)
213 >>> len(a), len(b), b == d
214 (3, 512, True)
216 >>> class Foo:
217 ... def method(self, arg1, arg2):
218 ... return arg1+arg2
220 >>> x = Foo()
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
234 >>> try:
235 ... silence = id(1, *{})
236 ... True
237 ... except:
238 ... False
239 True
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>.
249 >>> class Name(str):
250 ... def __eq__(self, other):
251 ... try:
252 ... del x[self]
253 ... except KeyError:
254 ... pass
255 ... return str.__eq__(self, other)
256 ... def __hash__(self):
257 ... return str.__hash__(self)
259 >>> x = {Name("a"):1, Name("b"):2}
260 >>> def f(a, b):
261 ... print(a,b)
262 >>> f(**x)
266 from test import support
268 def test_main():
269 from test import test_extcall # self import
270 support.run_doctest(test_extcall, True)
272 if __name__ == '__main__':
273 test_main()