blob: a10887e1630a2c63be46fa18bdd4bde8d545ad76 [file] [log] [blame]
Benjamin Petersone18ef192009-01-20 14:21:16 +00001# -*- coding: utf-8 -*-
Brett Cannon0bb79502008-03-18 01:58:56 +00002"""Doctest for method/function calls.
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +00003
Brett Cannon0bb79502008-03-18 01:58:56 +00004We're going the use these types for extra testing
Raymond Hettinger40174c32003-05-31 07:04:16 +00005
Brett Cannon0bb79502008-03-18 01:58:56 +00006 >>> from UserList import UserList
7 >>> from UserDict import UserDict
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +00008
Brett Cannon0bb79502008-03-18 01:58:56 +00009We're defining four helper functions
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +000010
Brett Cannon0bb79502008-03-18 01:58:56 +000011 >>> def e(a,b):
12 ... print a, b
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +000013
Brett Cannon0bb79502008-03-18 01:58:56 +000014 >>> def f(*a, **k):
15 ... print a, test_support.sortdict(k)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +000016
Brett Cannon0bb79502008-03-18 01:58:56 +000017 >>> def g(x, *y, **z):
18 ... print x, y, test_support.sortdict(z)
19
20 >>> def h(j=1, a=2, h=3):
21 ... print j, a, h
22
23Argument 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
40Here 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
56Examples with invalid arguments (TypeErrors). We're also testing the function
57names in the exception messages.
58
59Verify 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 ...
69 TypeError: g() takes at least 1 argument (0 given)
70
71 >>> g(*())
72 Traceback (most recent call last):
73 ...
74 TypeError: g() takes at least 1 argument (0 given)
75
76 >>> g(*(), **{})
77 Traceback (most recent call last):
78 ...
79 TypeError: g() takes at least 1 argument (0 given)
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 ...
95 TypeError: g() argument after * must be a sequence, not instance
96
97 >>> class Nothing:
98 ... def __len__(self): return 5
99 ...
100
101 >>> g(*Nothing())
102 Traceback (most recent call last):
103 ...
104 TypeError: g() argument after * must be a sequence, not instance
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
119 ... def next(self):
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
130Make 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
139What 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 Hyltonaed0d8d2000-03-28 23:51:17 +0000149
Georg Brandl2134e752007-05-21 20:34:16 +0000150
Brett Cannon0bb79502008-03-18 01:58:56 +0000151 >>> g(1, 2, 3, **{'x': 4, 'y': 5})
152 Traceback (most recent call last):
153 ...
154 TypeError: g() got multiple values for keyword argument 'x'
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, \
180not 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, \
196not 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
203Another helper function
204
205 >>> def f2(*a, **b):
206 ... return a, b
Georg Brandl2134e752007-05-21 20:34:16 +0000207
208
Brett Cannon0bb79502008-03-18 01:58:56 +0000209 >>> d = {}
210 >>> for i in xrange(512):
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 Hettinger40174c32003-05-31 07:04:16 +0000216
Brett Cannon0bb79502008-03-18 01:58:56 +0000217 >>> class Foo:
218 ... def method(self, arg1, arg2):
219 ... return arg1+arg2
Fred Drake004d5e62000-10-23 17:22:08 +0000220
Brett Cannon0bb79502008-03-18 01:58:56 +0000221 >>> x = Foo()
222 >>> Foo.method(*(x, 1, 2))
223 3
224 >>> Foo.method(x, *(1, 2))
225 3
226 >>> Foo.method(*(1, 2, 3))
227 Traceback (most recent call last):
228 ...
229 TypeError: unbound method method() must be called with Foo instance as \
230first argument (got int instance instead)
Fred Drake004d5e62000-10-23 17:22:08 +0000231
Brett Cannon0bb79502008-03-18 01:58:56 +0000232 >>> Foo.method(1, *[2, 3])
233 Traceback (most recent call last):
234 ...
235 TypeError: unbound method method() must be called with Foo instance as \
236first argument (got int instance instead)
Fred Drake004d5e62000-10-23 17:22:08 +0000237
Brett Cannon0bb79502008-03-18 01:58:56 +0000238A PyCFunction that takes only positional parameters shoud allow an
239empty keyword dictionary to pass without a complaint, but raise a
240TypeError if te dictionary is not empty
Jeremy Hylton074c3e62000-03-30 23:55:31 +0000241
Brett Cannon0bb79502008-03-18 01:58:56 +0000242 >>> try:
243 ... silence = id(1, *{})
244 ... True
245 ... except:
246 ... False
247 True
Fred Drake004d5e62000-10-23 17:22:08 +0000248
Brett Cannon0bb79502008-03-18 01:58:56 +0000249 >>> id(1, **{'foo': 1})
250 Traceback (most recent call last):
251 ...
252 TypeError: id() takes no keyword arguments
Jeremy Hylton074c3e62000-03-30 23:55:31 +0000253
Amaury Forgeot d'Arc595f7a52009-06-25 22:29:29 +0000254A corner case of keyword dictionary items being deleted during
255the function call setup. See <http://bugs.python.org/issue2016>.
256
257 >>> class Name(str):
258 ... def __eq__(self, other):
259 ... try:
260 ... del x[self]
261 ... except KeyError:
262 ... pass
263 ... return str.__eq__(self, other)
264 ... def __hash__(self):
265 ... return str.__hash__(self)
266
267 >>> x = {Name("a"):1, Name("b"):2}
268 >>> def f(a, b):
269 ... print a,b
270 >>> f(**x)
271 1 2
Brett Cannon0bb79502008-03-18 01:58:56 +0000272"""
Samuele Pedroni8036c832004-02-21 21:03:30 +0000273
Benjamin Petersone18ef192009-01-20 14:21:16 +0000274import unittest
Brett Cannon0bb79502008-03-18 01:58:56 +0000275from test import test_support
Samuele Pedroni8036c832004-02-21 21:03:30 +0000276
Benjamin Petersone18ef192009-01-20 14:21:16 +0000277
278class UnicodeKeywordArgsTest(unittest.TestCase):
279
280 def test_unicode_keywords(self):
281 def f(a):
282 return a
283 self.assertEqual(f(**{u'a': 4}), 4)
284 self.assertRaises(TypeError, f, **{u'stören': 4})
285 self.assertRaises(TypeError, f, **{u'someLongString':2})
286 try:
287 f(a=4, **{u'a': 4})
288 except TypeError:
289 pass
290 else:
291 self.fail("duplicate arguments didn't raise")
292
293
Brett Cannon0bb79502008-03-18 01:58:56 +0000294def test_main():
Neal Norwitz0c1ef472008-03-18 20:30:38 +0000295 from test import test_extcall # self import
Brett Cannon0bb79502008-03-18 01:58:56 +0000296 test_support.run_doctest(test_extcall, True)
Benjamin Petersone18ef192009-01-20 14:21:16 +0000297 test_support.run_unittest(UnicodeKeywordArgsTest)
Jeremy Hylton074c3e62000-03-30 23:55:31 +0000298
Brett Cannon0bb79502008-03-18 01:58:56 +0000299if __name__ == '__main__':
300 test_main()