blob: 6860751e578620cecc6664bd9cf103f33fe826fe [file] [log] [blame]
Benjamin Petersone18ef192009-01-20 14:21:16 +00001# -*- coding: utf-8 -*-
Benjamin Petersone39b2ec2010-03-21 17:34:54 +00002
Brett Cannon0bb79502008-03-18 01:58:56 +00003"""Doctest for method/function calls.
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +00004
Brett Cannon0bb79502008-03-18 01:58:56 +00005We're going the use these types for extra testing
Raymond Hettinger40174c32003-05-31 07:04:16 +00006
Brett Cannon0bb79502008-03-18 01:58:56 +00007 >>> from UserList import UserList
8 >>> from UserDict import UserDict
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +00009
Brett Cannon0bb79502008-03-18 01:58:56 +000010We're defining four helper functions
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +000011
Brett Cannon0bb79502008-03-18 01:58:56 +000012 >>> def e(a,b):
13 ... print a, b
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +000014
Brett Cannon0bb79502008-03-18 01:58:56 +000015 >>> def f(*a, **k):
16 ... print a, test_support.sortdict(k)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +000017
Brett Cannon0bb79502008-03-18 01:58:56 +000018 >>> def g(x, *y, **z):
19 ... print x, y, test_support.sortdict(z)
20
21 >>> def h(j=1, a=2, h=3):
22 ... print j, a, h
23
24Argument list examples
25
26 >>> f()
27 () {}
28 >>> f(1)
29 (1,) {}
30 >>> f(1, 2)
31 (1, 2) {}
32 >>> f(1, 2, 3)
33 (1, 2, 3) {}
34 >>> f(1, 2, 3, *(4, 5))
35 (1, 2, 3, 4, 5) {}
36 >>> f(1, 2, 3, *[4, 5])
37 (1, 2, 3, 4, 5) {}
38 >>> f(1, 2, 3, *UserList([4, 5]))
39 (1, 2, 3, 4, 5) {}
40
41Here we add keyword arguments
42
43 >>> f(1, 2, 3, **{'a':4, 'b':5})
44 (1, 2, 3) {'a': 4, 'b': 5}
45 >>> f(1, 2, 3, *[4, 5], **{'a':6, 'b':7})
46 (1, 2, 3, 4, 5) {'a': 6, 'b': 7}
47 >>> f(1, 2, 3, x=4, y=5, *(6, 7), **{'a':8, 'b': 9})
48 (1, 2, 3, 6, 7) {'a': 8, 'b': 9, 'x': 4, 'y': 5}
49
50 >>> f(1, 2, 3, **UserDict(a=4, b=5))
51 (1, 2, 3) {'a': 4, 'b': 5}
52 >>> f(1, 2, 3, *(4, 5), **UserDict(a=6, b=7))
53 (1, 2, 3, 4, 5) {'a': 6, 'b': 7}
54 >>> f(1, 2, 3, x=4, y=5, *(6, 7), **UserDict(a=8, b=9))
55 (1, 2, 3, 6, 7) {'a': 8, 'b': 9, 'x': 4, 'y': 5}
56
57Examples with invalid arguments (TypeErrors). We're also testing the function
58names in the exception messages.
59
60Verify clearing of SF bug #733667
61
62 >>> e(c=4)
63 Traceback (most recent call last):
64 ...
65 TypeError: e() got an unexpected keyword argument 'c'
66
67 >>> g()
68 Traceback (most recent call last):
69 ...
70 TypeError: g() takes at least 1 argument (0 given)
71
72 >>> g(*())
73 Traceback (most recent call last):
74 ...
75 TypeError: g() takes at least 1 argument (0 given)
76
77 >>> g(*(), **{})
78 Traceback (most recent call last):
79 ...
80 TypeError: g() takes at least 1 argument (0 given)
81
82 >>> g(1)
83 1 () {}
84 >>> g(1, 2)
85 1 (2,) {}
86 >>> g(1, 2, 3)
87 1 (2, 3) {}
88 >>> g(1, 2, 3, *(4, 5))
89 1 (2, 3, 4, 5) {}
90
91 >>> class Nothing: pass
92 ...
93 >>> g(*Nothing())
94 Traceback (most recent call last):
95 ...
Martin Panter0bb165e2016-01-31 06:30:56 +000096 TypeError: g() argument after * must be an iterable, not instance
Brett Cannon0bb79502008-03-18 01:58:56 +000097
98 >>> class Nothing:
99 ... def __len__(self): return 5
100 ...
101
102 >>> g(*Nothing())
103 Traceback (most recent call last):
104 ...
Martin Panter0bb165e2016-01-31 06:30:56 +0000105 TypeError: g() argument after * must be an iterable, not instance
Brett Cannon0bb79502008-03-18 01:58:56 +0000106
107 >>> class Nothing():
108 ... def __len__(self): return 5
109 ... def __getitem__(self, i):
110 ... if i<3: return i
111 ... else: raise IndexError(i)
112 ...
113
114 >>> g(*Nothing())
115 0 (1, 2) {}
116
117 >>> class Nothing:
118 ... def __init__(self): self.c = 0
119 ... def __iter__(self): return self
120 ... def next(self):
121 ... if self.c == 4:
122 ... raise StopIteration
123 ... c = self.c
124 ... self.c += 1
125 ... return c
126 ...
127
128 >>> g(*Nothing())
129 0 (1, 2, 3) {}
130
Martin Panter0bb165e2016-01-31 06:30:56 +0000131Check for issue #4806: Does a TypeError in a generator get propagated with the
132right error message?
133
134 >>> def broken(): raise TypeError("myerror")
135 ...
136
137 >>> g(*(broken() for i in range(1)))
138 Traceback (most recent call last):
139 ...
140 TypeError: myerror
141
Brett Cannon0bb79502008-03-18 01:58:56 +0000142Make sure that the function doesn't stomp the dictionary
143
144 >>> d = {'a': 1, 'b': 2, 'c': 3}
145 >>> d2 = d.copy()
146 >>> g(1, d=4, **d)
147 1 () {'a': 1, 'b': 2, 'c': 3, 'd': 4}
148 >>> d == d2
149 True
150
151What about willful misconduct?
152
153 >>> def saboteur(**kw):
154 ... kw['x'] = 'm'
155 ... return kw
156
157 >>> d = {}
158 >>> kw = saboteur(a=1, **d)
159 >>> d
160 {}
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000161
Georg Brandl2134e752007-05-21 20:34:16 +0000162
Brett Cannon0bb79502008-03-18 01:58:56 +0000163 >>> g(1, 2, 3, **{'x': 4, 'y': 5})
164 Traceback (most recent call last):
165 ...
166 TypeError: g() got multiple values for keyword argument 'x'
167
168 >>> f(**{1:2})
169 Traceback (most recent call last):
170 ...
171 TypeError: f() keywords must be strings
172
173 >>> h(**{'e': 2})
174 Traceback (most recent call last):
175 ...
176 TypeError: h() got an unexpected keyword argument 'e'
177
178 >>> h(*h)
179 Traceback (most recent call last):
180 ...
Martin Panter0bb165e2016-01-31 06:30:56 +0000181 TypeError: h() argument after * must be an iterable, not function
Brett Cannon0bb79502008-03-18 01:58:56 +0000182
Serhiy Storchaka78b634d2016-10-02 10:36:33 +0300183 >>> h(1, *h)
184 Traceback (most recent call last):
185 ...
186 TypeError: h() argument after * must be an iterable, not function
187
Brett Cannon0bb79502008-03-18 01:58:56 +0000188 >>> dir(*h)
189 Traceback (most recent call last):
190 ...
Martin Panter0bb165e2016-01-31 06:30:56 +0000191 TypeError: dir() argument after * must be an iterable, not function
Brett Cannon0bb79502008-03-18 01:58:56 +0000192
193 >>> None(*h)
194 Traceback (most recent call last):
195 ...
Martin Panter0bb165e2016-01-31 06:30:56 +0000196 TypeError: NoneType object argument after * must be an iterable, \
Brett Cannon0bb79502008-03-18 01:58:56 +0000197not function
198
199 >>> h(**h)
200 Traceback (most recent call last):
201 ...
202 TypeError: h() argument after ** must be a mapping, not function
203
204 >>> dir(**h)
205 Traceback (most recent call last):
206 ...
207 TypeError: dir() argument after ** must be a mapping, not function
208
209 >>> None(**h)
210 Traceback (most recent call last):
211 ...
212 TypeError: NoneType object argument after ** must be a mapping, \
213not function
214
215 >>> dir(b=1, **{'b': 1})
216 Traceback (most recent call last):
217 ...
218 TypeError: dir() got multiple values for keyword argument 'b'
219
220Another helper function
221
222 >>> def f2(*a, **b):
223 ... return a, b
Georg Brandl2134e752007-05-21 20:34:16 +0000224
225
Brett Cannon0bb79502008-03-18 01:58:56 +0000226 >>> d = {}
227 >>> for i in xrange(512):
228 ... key = 'k%d' % i
229 ... d[key] = i
230 >>> a, b = f2(1, *(2,3), **d)
231 >>> len(a), len(b), b == d
232 (3, 512, True)
Raymond Hettinger40174c32003-05-31 07:04:16 +0000233
Brett Cannon0bb79502008-03-18 01:58:56 +0000234 >>> class Foo:
235 ... def method(self, arg1, arg2):
236 ... return arg1+arg2
Fred Drake004d5e62000-10-23 17:22:08 +0000237
Brett Cannon0bb79502008-03-18 01:58:56 +0000238 >>> x = Foo()
239 >>> Foo.method(*(x, 1, 2))
240 3
241 >>> Foo.method(x, *(1, 2))
242 3
243 >>> Foo.method(*(1, 2, 3))
244 Traceback (most recent call last):
245 ...
246 TypeError: unbound method method() must be called with Foo instance as \
247first argument (got int instance instead)
Fred Drake004d5e62000-10-23 17:22:08 +0000248
Brett Cannon0bb79502008-03-18 01:58:56 +0000249 >>> Foo.method(1, *[2, 3])
250 Traceback (most recent call last):
251 ...
252 TypeError: unbound method method() must be called with Foo instance as \
253first argument (got int instance instead)
Fred Drake004d5e62000-10-23 17:22:08 +0000254
Ezio Melottic2077b02011-03-16 12:34:31 +0200255A PyCFunction that takes only positional parameters should allow an
Brett Cannon0bb79502008-03-18 01:58:56 +0000256empty keyword dictionary to pass without a complaint, but raise a
257TypeError if te dictionary is not empty
Jeremy Hylton074c3e62000-03-30 23:55:31 +0000258
Brett Cannon0bb79502008-03-18 01:58:56 +0000259 >>> try:
260 ... silence = id(1, *{})
261 ... True
262 ... except:
263 ... False
264 True
Fred Drake004d5e62000-10-23 17:22:08 +0000265
Brett Cannon0bb79502008-03-18 01:58:56 +0000266 >>> id(1, **{'foo': 1})
267 Traceback (most recent call last):
268 ...
269 TypeError: id() takes no keyword arguments
Jeremy Hylton074c3e62000-03-30 23:55:31 +0000270
Amaury Forgeot d'Arc595f7a52009-06-25 22:29:29 +0000271A corner case of keyword dictionary items being deleted during
272the function call setup. See <http://bugs.python.org/issue2016>.
273
274 >>> class Name(str):
275 ... def __eq__(self, other):
276 ... try:
277 ... del x[self]
278 ... except KeyError:
279 ... pass
280 ... return str.__eq__(self, other)
281 ... def __hash__(self):
282 ... return str.__hash__(self)
283
284 >>> x = {Name("a"):1, Name("b"):2}
285 >>> def f(a, b):
286 ... print a,b
287 >>> f(**x)
288 1 2
Benjamin Peterson96545892010-03-21 20:21:00 +0000289
Serhiy Storchaka9a118f12016-04-17 09:37:36 +0300290An obscure message:
Benjamin Peterson96545892010-03-21 20:21:00 +0000291
292 >>> def f(a, b):
293 ... pass
294 >>> f(b=1)
295 Traceback (most recent call last):
296 ...
297 TypeError: f() takes exactly 2 arguments (1 given)
Benjamin Petersonbb9d7262010-03-21 20:30:30 +0000298
299The number of arguments passed in includes keywords:
300
301 >>> def f(a):
302 ... pass
303 >>> f(6, a=4, *(1, 2, 3))
304 Traceback (most recent call last):
305 ...
306 TypeError: f() takes exactly 1 argument (5 given)
Brett Cannon0bb79502008-03-18 01:58:56 +0000307"""
Samuele Pedroni8036c832004-02-21 21:03:30 +0000308
Benjamin Petersone18ef192009-01-20 14:21:16 +0000309import unittest
Benjamin Peterson52b32b62010-03-21 19:54:56 +0000310import sys
Brett Cannon0bb79502008-03-18 01:58:56 +0000311from test import test_support
Samuele Pedroni8036c832004-02-21 21:03:30 +0000312
Benjamin Petersone18ef192009-01-20 14:21:16 +0000313
Benjamin Petersone39b2ec2010-03-21 17:34:54 +0000314class ExtCallTest(unittest.TestCase):
Benjamin Petersone18ef192009-01-20 14:21:16 +0000315
316 def test_unicode_keywords(self):
317 def f(a):
318 return a
319 self.assertEqual(f(**{u'a': 4}), 4)
320 self.assertRaises(TypeError, f, **{u'stören': 4})
321 self.assertRaises(TypeError, f, **{u'someLongString':2})
322 try:
323 f(a=4, **{u'a': 4})
324 except TypeError:
325 pass
326 else:
327 self.fail("duplicate arguments didn't raise")
328
329
Brett Cannon0bb79502008-03-18 01:58:56 +0000330def test_main():
Benjamin Petersone39b2ec2010-03-21 17:34:54 +0000331 test_support.run_doctest(sys.modules[__name__], True)
332 test_support.run_unittest(ExtCallTest)
Jeremy Hylton074c3e62000-03-30 23:55:31 +0000333
Brett Cannon0bb79502008-03-18 01:58:56 +0000334if __name__ == '__main__':
335 test_main()