~ubuntu-branches/ubuntu/trusty/python3.4/trusty-proposed

« back to all changes in this revision

Viewing changes to Lib/test/test_extcall.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-11-25 09:44:27 UTC
  • Revision ID: package-import@ubuntu.com-20131125094427-lzxj8ap5w01lmo7f
Tags: upstream-3.4~b1
ImportĀ upstreamĀ versionĀ 3.4~b1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
"""Doctest for method/function calls.
 
3
 
 
4
We're going the use these types for extra testing
 
5
 
 
6
    >>> from collections import UserList
 
7
    >>> from collections import UserDict
 
8
 
 
9
We're defining four helper functions
 
10
 
 
11
    >>> def e(a,b):
 
12
    ...     print(a, b)
 
13
 
 
14
    >>> def f(*a, **k):
 
15
    ...     print(a, support.sortdict(k))
 
16
 
 
17
    >>> def g(x, *y, **z):
 
18
    ...     print(x, y, support.sortdict(z))
 
19
 
 
20
    >>> def h(j=1, a=2, h=3):
 
21
    ...     print(j, a, h)
 
22
 
 
23
Argument 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
 
 
40
Here 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
 
 
56
Examples with invalid arguments (TypeErrors). We're also testing the function
 
57
names in the exception messages.
 
58
 
 
59
Verify 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() missing 1 required positional argument: 'x'
 
70
 
 
71
    >>> g(*())
 
72
    Traceback (most recent call last):
 
73
      ...
 
74
    TypeError: g() missing 1 required positional argument: 'x'
 
75
 
 
76
    >>> g(*(), **{})
 
77
    Traceback (most recent call last):
 
78
      ...
 
79
    TypeError: g() missing 1 required positional argument: 'x'
 
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 Nothing
 
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 Nothing
 
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
 
 
130
Make 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
 
 
139
What 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
    {}
 
149
 
 
150
 
 
151
    >>> g(1, 2, 3, **{'x': 4, 'y': 5})
 
152
    Traceback (most recent call last):
 
153
      ...
 
154
    TypeError: g() got multiple values for 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, \
 
180
not 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, \
 
196
not 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
 
 
203
Another helper function
 
204
 
 
205
    >>> def f2(*a, **b):
 
206
    ...     return a, b
 
207
 
 
208
 
 
209
    >>> d = {}
 
210
    >>> for i in range(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)
 
216
 
 
217
    >>> class Foo:
 
218
    ...     def method(self, arg1, arg2):
 
219
    ...         return arg1+arg2
 
220
 
 
221
    >>> 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
    5
 
228
    >>> Foo.method(1, *[2, 3])
 
229
    5
 
230
 
 
231
A PyCFunction that takes only positional parameters should allow an
 
232
empty keyword dictionary to pass without a complaint, but raise a
 
233
TypeError if te dictionary is not empty
 
234
 
 
235
    >>> try:
 
236
    ...     silence = id(1, *{})
 
237
    ...     True
 
238
    ... except:
 
239
    ...     False
 
240
    True
 
241
 
 
242
    >>> id(1, **{'foo': 1})
 
243
    Traceback (most recent call last):
 
244
      ...
 
245
    TypeError: id() takes no keyword arguments
 
246
 
 
247
A corner case of keyword dictionary items being deleted during
 
248
the function call setup. See <http://bugs.python.org/issue2016>.
 
249
 
 
250
    >>> class Name(str):
 
251
    ...     def __eq__(self, other):
 
252
    ...         try:
 
253
    ...              del x[self]
 
254
    ...         except KeyError:
 
255
    ...              pass
 
256
    ...         return str.__eq__(self, other)
 
257
    ...     def __hash__(self):
 
258
    ...         return str.__hash__(self)
 
259
 
 
260
    >>> x = {Name("a"):1, Name("b"):2}
 
261
    >>> def f(a, b):
 
262
    ...     print(a,b)
 
263
    >>> f(**x)
 
264
    1 2
 
265
 
 
266
Too many arguments:
 
267
 
 
268
    >>> def f(): pass
 
269
    >>> f(1)
 
270
    Traceback (most recent call last):
 
271
      ...
 
272
    TypeError: f() takes 0 positional arguments but 1 was given
 
273
    >>> def f(a): pass
 
274
    >>> f(1, 2)
 
275
    Traceback (most recent call last):
 
276
      ...
 
277
    TypeError: f() takes 1 positional argument but 2 were given
 
278
    >>> def f(a, b=1): pass
 
279
    >>> f(1, 2, 3)
 
280
    Traceback (most recent call last):
 
281
      ...
 
282
    TypeError: f() takes from 1 to 2 positional arguments but 3 were given
 
283
    >>> def f(*, kw): pass
 
284
    >>> f(1, kw=3)
 
285
    Traceback (most recent call last):
 
286
      ...
 
287
    TypeError: f() takes 0 positional arguments but 1 positional argument (and 1 keyword-only argument) were given
 
288
    >>> def f(*, kw, b): pass
 
289
    >>> f(1, 2, 3, b=3, kw=3)
 
290
    Traceback (most recent call last):
 
291
      ...
 
292
    TypeError: f() takes 0 positional arguments but 3 positional arguments (and 2 keyword-only arguments) were given
 
293
    >>> def f(a, b=2, *, kw): pass
 
294
    >>> f(2, 3, 4, kw=4)
 
295
    Traceback (most recent call last):
 
296
      ...
 
297
    TypeError: f() takes from 1 to 2 positional arguments but 3 positional arguments (and 1 keyword-only argument) were given
 
298
 
 
299
Too few and missing arguments:
 
300
 
 
301
    >>> def f(a): pass
 
302
    >>> f()
 
303
    Traceback (most recent call last):
 
304
      ...
 
305
    TypeError: f() missing 1 required positional argument: 'a'
 
306
    >>> def f(a, b): pass
 
307
    >>> f()
 
308
    Traceback (most recent call last):
 
309
      ...
 
310
    TypeError: f() missing 2 required positional arguments: 'a' and 'b'
 
311
    >>> def f(a, b, c): pass
 
312
    >>> f()
 
313
    Traceback (most recent call last):
 
314
      ...
 
315
    TypeError: f() missing 3 required positional arguments: 'a', 'b', and 'c'
 
316
    >>> def f(a, b, c, d, e): pass
 
317
    >>> f()
 
318
    Traceback (most recent call last):
 
319
      ...
 
320
    TypeError: f() missing 5 required positional arguments: 'a', 'b', 'c', 'd', and 'e'
 
321
    >>> def f(a, b=4, c=5, d=5): pass
 
322
    >>> f(c=12, b=9)
 
323
    Traceback (most recent call last):
 
324
      ...
 
325
    TypeError: f() missing 1 required positional argument: 'a'
 
326
 
 
327
Same with keyword only args:
 
328
 
 
329
    >>> def f(*, w): pass
 
330
    >>> f()
 
331
    Traceback (most recent call last):
 
332
      ...
 
333
    TypeError: f() missing 1 required keyword-only argument: 'w'
 
334
    >>> def f(*, a, b, c, d, e): pass
 
335
    >>> f()
 
336
    Traceback (most recent call last):
 
337
      ...
 
338
    TypeError: f() missing 5 required keyword-only arguments: 'a', 'b', 'c', 'd', and 'e'
 
339
 
 
340
"""
 
341
 
 
342
import sys
 
343
from test import support
 
344
 
 
345
def test_main():
 
346
    support.run_doctest(sys.modules[__name__], True)
 
347
 
 
348
if __name__ == '__main__':
 
349
    test_main()