Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(7)

Delta Between Two Patch Sets: Lib/test/test_extcall.py

Issue 4806: Function calls taking a generator as star argument can mask TypeErrors in the generator
Left Patch Set: Created 4 years, 12 months ago
Right Patch Set: Created 4 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | Misc/NEWS » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # -*- coding: utf-8 -*-
1 2
2 """Doctest for method/function calls. 3 """Doctest for method/function calls.
3 4
4 We're going the use these types for extra testing 5 We're going the use these types for extra testing
5 6
6 >>> from collections import UserList 7 >>> from UserList import UserList
7 >>> from collections import UserDict 8 >>> from UserDict import UserDict
8 9
9 We're defining four helper functions 10 We're defining four helper functions
10 11
11 >>> def e(a,b): 12 >>> def e(a,b):
12 ... print(a, b) 13 ... print a, b
13 14
14 >>> def f(*a, **k): 15 >>> def f(*a, **k):
15 ... print(a, support.sortdict(k)) 16 ... print a, test_support.sortdict(k)
16 17
17 >>> def g(x, *y, **z): 18 >>> def g(x, *y, **z):
18 ... print(x, y, support.sortdict(z)) 19 ... print x, y, test_support.sortdict(z)
19 20
20 >>> def h(j=1, a=2, h=3): 21 >>> def h(j=1, a=2, h=3):
21 ... print(j, a, h) 22 ... print j, a, h
22 23
23 Argument list examples 24 Argument list examples
24 25
25 >>> f() 26 >>> f()
26 () {} 27 () {}
27 >>> f(1) 28 >>> f(1)
28 (1,) {} 29 (1,) {}
29 >>> f(1, 2) 30 >>> f(1, 2)
30 (1, 2) {} 31 (1, 2) {}
31 >>> f(1, 2, 3) 32 >>> f(1, 2, 3)
(...skipping 27 matching lines...) Expand all
59 Verify clearing of SF bug #733667 60 Verify clearing of SF bug #733667
60 61
61 >>> e(c=4) 62 >>> e(c=4)
62 Traceback (most recent call last): 63 Traceback (most recent call last):
63 ... 64 ...
64 TypeError: e() got an unexpected keyword argument 'c' 65 TypeError: e() got an unexpected keyword argument 'c'
65 66
66 >>> g() 67 >>> g()
67 Traceback (most recent call last): 68 Traceback (most recent call last):
68 ... 69 ...
69 TypeError: g() missing 1 required positional argument: 'x' 70 TypeError: g() takes at least 1 argument (0 given)
70 71
71 >>> g(*()) 72 >>> g(*())
72 Traceback (most recent call last): 73 Traceback (most recent call last):
73 ... 74 ...
74 TypeError: g() missing 1 required positional argument: 'x' 75 TypeError: g() takes at least 1 argument (0 given)
75 76
76 >>> g(*(), **{}) 77 >>> g(*(), **{})
77 Traceback (most recent call last): 78 Traceback (most recent call last):
78 ... 79 ...
79 TypeError: g() missing 1 required positional argument: 'x' 80 TypeError: g() takes at least 1 argument (0 given)
80 81
81 >>> g(1) 82 >>> g(1)
82 1 () {} 83 1 () {}
83 >>> g(1, 2) 84 >>> g(1, 2)
84 1 (2,) {} 85 1 (2,) {}
85 >>> g(1, 2, 3) 86 >>> g(1, 2, 3)
86 1 (2, 3) {} 87 1 (2, 3) {}
87 >>> g(1, 2, 3, *(4, 5)) 88 >>> g(1, 2, 3, *(4, 5))
88 1 (2, 3, 4, 5) {} 89 1 (2, 3, 4, 5) {}
89 90
90 >>> class Nothing: pass 91 >>> class Nothing: pass
91 ... 92 ...
92 >>> g(*Nothing()) 93 >>> g(*Nothing())
93 Traceback (most recent call last): 94 Traceback (most recent call last):
94 ... 95 ...
95 TypeError: g() argument after * must be an iterable, not Nothing 96 TypeError: g() argument after * must be an iterable, not instance
96 97
97 >>> class Nothing: 98 >>> class Nothing:
98 ... def __len__(self): return 5 99 ... def __len__(self): return 5
99 ... 100 ...
100 101
101 >>> g(*Nothing()) 102 >>> g(*Nothing())
102 Traceback (most recent call last): 103 Traceback (most recent call last):
103 ... 104 ...
104 TypeError: g() argument after * must be an iterable, not Nothing 105 TypeError: g() argument after * must be an iterable, not instance
105 106
106 >>> class Nothing(): 107 >>> class Nothing():
107 ... def __len__(self): return 5 108 ... def __len__(self): return 5
108 ... def __getitem__(self, i): 109 ... def __getitem__(self, i):
109 ... if i<3: return i 110 ... if i<3: return i
110 ... else: raise IndexError(i) 111 ... else: raise IndexError(i)
111 ... 112 ...
112 113
113 >>> g(*Nothing()) 114 >>> g(*Nothing())
114 0 (1, 2) {} 115 0 (1, 2) {}
115 116
116 >>> class Nothing: 117 >>> class Nothing:
117 ... def __init__(self): self.c = 0 118 ... def __init__(self): self.c = 0
118 ... def __iter__(self): return self 119 ... def __iter__(self): return self
119 ... def __next__(self): 120 ... def next(self):
120 ... if self.c == 4: 121 ... if self.c == 4:
121 ... raise StopIteration 122 ... raise StopIteration
122 ... c = self.c 123 ... c = self.c
123 ... self.c += 1 124 ... self.c += 1
124 ... return c 125 ... return c
125 ... 126 ...
126 127
127 >>> g(*Nothing()) 128 >>> g(*Nothing())
128 0 (1, 2, 3) {} 129 0 (1, 2, 3) {}
129 130
130 Check for issue #4806: Does a TypeError in a generator get propagated with the 131 Check for issue #4806: Does a TypeError in a generator get propagated with the
131 right error message? (Also check with other iterables.) 132 right error message?
132 133
133 >>> def broken(): raise TypeError("myerror") 134 >>> def broken(): raise TypeError("myerror")
134 ... 135 ...
135 136
136 >>> g(*(broken() for i in range(1))) 137 >>> g(*(broken() for i in range(1)))
137 Traceback (most recent call last):
138 ...
139 TypeError: myerror
140
141 >>> class BrokenIterable1:
142 ... def __iter__(self):
143 ... raise TypeError('myerror')
144 ...
145 >>> g(*BrokenIterable1())
146 Traceback (most recent call last):
147 ...
148 TypeError: myerror
149
150 >>> class BrokenIterable2:
151 ... def __iter__(self):
152 ... yield 0
153 ... raise TypeError('myerror')
154 ...
155 >>> g(*BrokenIterable2())
156 Traceback (most recent call last):
157 ...
158 TypeError: myerror
159
160 >>> class BrokenSequence:
161 ... def __getitem__(self, idx):
162 ... raise TypeError('myerror')
163 ...
164 >>> f(*BrokenSequence())
165 Traceback (most recent call last): 138 Traceback (most recent call last):
166 ... 139 ...
167 TypeError: myerror 140 TypeError: myerror
168 141
169 Make sure that the function doesn't stomp the dictionary 142 Make sure that the function doesn't stomp the dictionary
170 143
171 >>> d = {'a': 1, 'b': 2, 'c': 3} 144 >>> d = {'a': 1, 'b': 2, 'c': 3}
172 >>> d2 = d.copy() 145 >>> d2 = d.copy()
173 >>> g(1, d=4, **d) 146 >>> g(1, d=4, **d)
174 1 () {'a': 1, 'b': 2, 'c': 3, 'd': 4} 147 1 () {'a': 1, 'b': 2, 'c': 3, 'd': 4}
175 >>> d == d2 148 >>> d == d2
176 True 149 True
177 150
178 What about willful misconduct? 151 What about willful misconduct?
179 152
180 >>> def saboteur(**kw): 153 >>> def saboteur(**kw):
181 ... kw['x'] = 'm' 154 ... kw['x'] = 'm'
182 ... return kw 155 ... return kw
183 156
184 >>> d = {} 157 >>> d = {}
185 >>> kw = saboteur(a=1, **d) 158 >>> kw = saboteur(a=1, **d)
186 >>> d 159 >>> d
187 {} 160 {}
188 161
189 162
190 >>> g(1, 2, 3, **{'x': 4, 'y': 5}) 163 >>> g(1, 2, 3, **{'x': 4, 'y': 5})
191 Traceback (most recent call last): 164 Traceback (most recent call last):
192 ... 165 ...
193 TypeError: g() got multiple values for argument 'x' 166 TypeError: g() got multiple values for keyword argument 'x'
194 167
195 >>> f(**{1:2}) 168 >>> f(**{1:2})
196 Traceback (most recent call last): 169 Traceback (most recent call last):
197 ... 170 ...
198 TypeError: f() keywords must be strings 171 TypeError: f() keywords must be strings
199 172
200 >>> h(**{'e': 2}) 173 >>> h(**{'e': 2})
201 Traceback (most recent call last): 174 Traceback (most recent call last):
202 ... 175 ...
203 TypeError: h() got an unexpected keyword argument 'e' 176 TypeError: h() got an unexpected keyword argument 'e'
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 ... 212 ...
240 TypeError: dir() got multiple values for keyword argument 'b' 213 TypeError: dir() got multiple values for keyword argument 'b'
241 214
242 Another helper function 215 Another helper function
243 216
244 >>> def f2(*a, **b): 217 >>> def f2(*a, **b):
245 ... return a, b 218 ... return a, b
246 219
247 220
248 >>> d = {} 221 >>> d = {}
249 >>> for i in range(512): 222 >>> for i in xrange(512):
250 ... key = 'k%d' % i 223 ... key = 'k%d' % i
251 ... d[key] = i 224 ... d[key] = i
252 >>> a, b = f2(1, *(2,3), **d) 225 >>> a, b = f2(1, *(2,3), **d)
253 >>> len(a), len(b), b == d 226 >>> len(a), len(b), b == d
254 (3, 512, True) 227 (3, 512, True)
255 228
256 >>> class Foo: 229 >>> class Foo:
257 ... def method(self, arg1, arg2): 230 ... def method(self, arg1, arg2):
258 ... return arg1+arg2 231 ... return arg1+arg2
259 232
260 >>> x = Foo() 233 >>> x = Foo()
261 >>> Foo.method(*(x, 1, 2)) 234 >>> Foo.method(*(x, 1, 2))
262 3 235 3
263 >>> Foo.method(x, *(1, 2)) 236 >>> Foo.method(x, *(1, 2))
264 3 237 3
265 >>> Foo.method(*(1, 2, 3)) 238 >>> Foo.method(*(1, 2, 3))
266 5 239 Traceback (most recent call last):
240 ...
241 TypeError: unbound method method() must be called with Foo instance as \
242 first argument (got int instance instead)
243
267 >>> Foo.method(1, *[2, 3]) 244 >>> Foo.method(1, *[2, 3])
268 5 245 Traceback (most recent call last):
246 ...
247 TypeError: unbound method method() must be called with Foo instance as \
248 first argument (got int instance instead)
269 249
270 A PyCFunction that takes only positional parameters should allow an 250 A PyCFunction that takes only positional parameters should allow an
271 empty keyword dictionary to pass without a complaint, but raise a 251 empty keyword dictionary to pass without a complaint, but raise a
272 TypeError if te dictionary is not empty 252 TypeError if te dictionary is not empty
273 253
274 >>> try: 254 >>> try:
275 ... silence = id(1, *{}) 255 ... silence = id(1, *{})
276 ... True 256 ... True
277 ... except: 257 ... except:
278 ... False 258 ... False
(...skipping 12 matching lines...) Expand all
291 ... try: 271 ... try:
292 ... del x[self] 272 ... del x[self]
293 ... except KeyError: 273 ... except KeyError:
294 ... pass 274 ... pass
295 ... return str.__eq__(self, other) 275 ... return str.__eq__(self, other)
296 ... def __hash__(self): 276 ... def __hash__(self):
297 ... return str.__hash__(self) 277 ... return str.__hash__(self)
298 278
299 >>> x = {Name("a"):1, Name("b"):2} 279 >>> x = {Name("a"):1, Name("b"):2}
300 >>> def f(a, b): 280 >>> def f(a, b):
301 ... print(a,b) 281 ... print a,b
302 >>> f(**x) 282 >>> f(**x)
303 1 2 283 1 2
304 284
305 Too many arguments: 285 A obscure message:
306 286
307 >>> def f(): pass 287 >>> def f(a, b):
308 >>> f(1) 288 ... pass
309 Traceback (most recent call last): 289 >>> f(b=1)
310 ... 290 Traceback (most recent call last):
311 TypeError: f() takes 0 positional arguments but 1 was given 291 ...
312 >>> def f(a): pass 292 TypeError: f() takes exactly 2 arguments (1 given)
313 >>> f(1, 2) 293
314 Traceback (most recent call last): 294 The number of arguments passed in includes keywords:
315 ... 295
316 TypeError: f() takes 1 positional argument but 2 were given 296 >>> def f(a):
317 >>> def f(a, b=1): pass 297 ... pass
318 >>> f(1, 2, 3) 298 >>> f(6, a=4, *(1, 2, 3))
319 Traceback (most recent call last): 299 Traceback (most recent call last):
320 ... 300 ...
321 TypeError: f() takes from 1 to 2 positional arguments but 3 were given 301 TypeError: f() takes exactly 1 argument (5 given)
322 >>> def f(*, kw): pass
323 >>> f(1, kw=3)
324 Traceback (most recent call last):
325 ...
326 TypeError: f() takes 0 positional arguments but 1 positional argument (and 1 keyword-only argument) were given
327 >>> def f(*, kw, b): pass
328 >>> f(1, 2, 3, b=3, kw=3)
329 Traceback (most recent call last):
330 ...
331 TypeError: f() takes 0 positional arguments but 3 positional arguments (and 2 keyword-only arguments) were given
332 >>> def f(a, b=2, *, kw): pass
333 >>> f(2, 3, 4, kw=4)
334 Traceback (most recent call last):
335 ...
336 TypeError: f() takes from 1 to 2 positional arguments but 3 positional argum ents (and 1 keyword-only argument) were given
337
338 Too few and missing arguments:
339
340 >>> def f(a): pass
341 >>> f()
342 Traceback (most recent call last):
343 ...
344 TypeError: f() missing 1 required positional argument: 'a'
345 >>> def f(a, b): pass
346 >>> f()
347 Traceback (most recent call last):
348 ...
349 TypeError: f() missing 2 required positional arguments: 'a' and 'b'
350 >>> def f(a, b, c): pass
351 >>> f()
352 Traceback (most recent call last):
353 ...
354 TypeError: f() missing 3 required positional arguments: 'a', 'b', and 'c'
355 >>> def f(a, b, c, d, e): pass
356 >>> f()
357 Traceback (most recent call last):
358 ...
359 TypeError: f() missing 5 required positional arguments: 'a', 'b', 'c', 'd', and 'e'
360 >>> def f(a, b=4, c=5, d=5): pass
361 >>> f(c=12, b=9)
362 Traceback (most recent call last):
363 ...
364 TypeError: f() missing 1 required positional argument: 'a'
365
366 Same with keyword only args:
367
368 >>> def f(*, w): pass
369 >>> f()
370 Traceback (most recent call last):
371 ...
372 TypeError: f() missing 1 required keyword-only argument: 'w'
373 >>> def f(*, a, b, c, d, e): pass
374 >>> f()
375 Traceback (most recent call last):
376 ...
377 TypeError: f() missing 5 required keyword-only arguments: 'a', 'b', 'c', 'd' , and 'e'
378
379 """ 302 """
380 303
304 import unittest
381 import sys 305 import sys
382 from test import support 306 from test import test_support
307
308
309 class ExtCallTest(unittest.TestCase):
310
311 def test_unicode_keywords(self):
312 def f(a):
313 return a
314 self.assertEqual(f(**{u'a': 4}), 4)
315 self.assertRaises(TypeError, f, **{u'stören': 4})
316 self.assertRaises(TypeError, f, **{u'someLongString':2})
317 try:
318 f(a=4, **{u'a': 4})
319 except TypeError:
320 pass
321 else:
322 self.fail("duplicate arguments didn't raise")
323
383 324
384 def test_main(): 325 def test_main():
385 support.run_doctest(sys.modules[__name__], True) 326 test_support.run_doctest(sys.modules[__name__], True)
327 test_support.run_unittest(ExtCallTest)
386 328
387 if __name__ == '__main__': 329 if __name__ == '__main__':
388 test_main() 330 test_main()
LEFTRIGHT
« no previous file | Misc/NEWS » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+