Require implementations for warnings.showwarning() support the 'line' argument.
[python.git] / Lib / test / test_extcall.py
blobf4ab204279a5855b473aab8091ca4f29a5ba1a91
1 # -*- coding: utf-8 -*-
2 """Doctest for method/function calls.
4 We're going the use these types for extra testing
6 >>> from UserList import UserList
7 >>> from UserDict import UserDict
9 We're defining four helper functions
11 >>> def e(a,b):
12 ... print a, b
14 >>> def f(*a, **k):
15 ... print a, test_support.sortdict(k)
17 >>> def g(x, *y, **z):
18 ... print x, y, test_support.sortdict(z)
20 >>> def h(j=1, a=2, h=3):
21 ... print j, a, h
23 Argument list examples
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) {}
40 Here we add keyword arguments
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}
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}
56 Examples with invalid arguments (TypeErrors). We're also testing the function
57 names in the exception messages.
59 Verify clearing of SF bug #733667
61 >>> e(c=4)
62 Traceback (most recent call last):
63 ...
64 TypeError: e() got an unexpected keyword argument 'c'
66 >>> g()
67 Traceback (most recent call last):
68 ...
69 TypeError: g() takes at least 1 argument (0 given)
71 >>> g(*())
72 Traceback (most recent call last):
73 ...
74 TypeError: g() takes at least 1 argument (0 given)
76 >>> g(*(), **{})
77 Traceback (most recent call last):
78 ...
79 TypeError: g() takes at least 1 argument (0 given)
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) {}
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
97 >>> class Nothing:
98 ... def __len__(self): return 5
99 ...
101 >>> g(*Nothing())
102 Traceback (most recent call last):
104 TypeError: g() argument after * must be a sequence, not instance
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)
113 >>> g(*Nothing())
114 0 (1, 2) {}
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
127 >>> g(*Nothing())
128 0 (1, 2, 3) {}
130 Make sure that the function doesn't stomp the dictionary
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
139 What about willful misconduct?
141 >>> def saboteur(**kw):
142 ... kw['x'] = 'm'
143 ... return kw
145 >>> d = {}
146 >>> kw = saboteur(a=1, **d)
147 >>> d
151 >>> g(1, 2, 3, **{'x': 4, 'y': 5})
152 Traceback (most recent call last):
154 TypeError: g() got multiple values for keyword argument 'x'
156 >>> f(**{1:2})
157 Traceback (most recent call last):
159 TypeError: f() keywords must be strings
161 >>> h(**{'e': 2})
162 Traceback (most recent call last):
164 TypeError: h() got an unexpected keyword argument 'e'
166 >>> h(*h)
167 Traceback (most recent call last):
169 TypeError: h() argument after * must be a sequence, not function
171 >>> dir(*h)
172 Traceback (most recent call last):
174 TypeError: dir() argument after * must be a sequence, not function
176 >>> None(*h)
177 Traceback (most recent call last):
179 TypeError: NoneType object argument after * must be a sequence, \
180 not function
182 >>> h(**h)
183 Traceback (most recent call last):
185 TypeError: h() argument after ** must be a mapping, not function
187 >>> dir(**h)
188 Traceback (most recent call last):
190 TypeError: dir() argument after ** must be a mapping, not function
192 >>> None(**h)
193 Traceback (most recent call last):
195 TypeError: NoneType object argument after ** must be a mapping, \
196 not function
198 >>> dir(b=1, **{'b': 1})
199 Traceback (most recent call last):
201 TypeError: dir() got multiple values for keyword argument 'b'
203 Another helper function
205 >>> def f2(*a, **b):
206 ... return a, b
209 >>> 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)
217 >>> class Foo:
218 ... def method(self, arg1, arg2):
219 ... return arg1+arg2
221 >>> x = Foo()
222 >>> Foo.method(*(x, 1, 2))
224 >>> Foo.method(x, *(1, 2))
226 >>> Foo.method(*(1, 2, 3))
227 Traceback (most recent call last):
229 TypeError: unbound method method() must be called with Foo instance as \
230 first argument (got int instance instead)
232 >>> Foo.method(1, *[2, 3])
233 Traceback (most recent call last):
235 TypeError: unbound method method() must be called with Foo instance as \
236 first argument (got int instance instead)
238 A PyCFunction that takes only positional parameters shoud allow an
239 empty keyword dictionary to pass without a complaint, but raise a
240 TypeError if te dictionary is not empty
242 >>> try:
243 ... silence = id(1, *{})
244 ... True
245 ... except:
246 ... False
247 True
249 >>> id(1, **{'foo': 1})
250 Traceback (most recent call last):
252 TypeError: id() takes no keyword arguments
256 import unittest
257 from test import test_support
260 class UnicodeKeywordArgsTest(unittest.TestCase):
262 def test_unicode_keywords(self):
263 def f(a):
264 return a
265 self.assertEqual(f(**{u'a': 4}), 4)
266 self.assertRaises(TypeError, f, **{u'stören': 4})
267 self.assertRaises(TypeError, f, **{u'someLongString':2})
268 try:
269 f(a=4, **{u'a': 4})
270 except TypeError:
271 pass
272 else:
273 self.fail("duplicate arguments didn't raise")
276 def test_main():
277 from test import test_extcall # self import
278 test_support.run_doctest(test_extcall, True)
279 test_support.run_unittest(UnicodeKeywordArgsTest)
281 if __name__ == '__main__':
282 test_main()