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

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 8 years, 11 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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
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}
(...skipping 64 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):
269 246 ...
270 A PyCFunction that takes only positional parameters shoud allow an 247 TypeError: unbound method method() must be called with Foo instance as \
248 first argument (got int instance instead)
249
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
279 True 259 True
280 260
(...skipping 10 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 A obscure message: 285 A obscure message:
306 286
307 >>> def f(a, b): 287 >>> def f(a, b):
308 ... pass 288 ... pass
309 >>> f(b=1) 289 >>> f(b=1)
310 Traceback (most recent call last): 290 Traceback (most recent call last):
311 ... 291 ...
312 TypeError: f() takes exactly 2 arguments (1 given) 292 TypeError: f() takes exactly 2 arguments (1 given)
313 293
314 The number of arguments passed in includes keywords: 294 The number of arguments passed in includes keywords:
315 295
316 >>> def f(a): 296 >>> def f(a):
317 ... pass 297 ... pass
318 >>> f(6, a=4, *(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 exactly 1 positional argument (5 given) 301 TypeError: f() takes exactly 1 argument (5 given)
322 >>> def f(a, *, kw):
323 ... pass
324 >>> f(6, 4, kw=4)
325 Traceback (most recent call last):
326 ...
327 TypeError: f() takes exactly 1 positional argument (3 given)
328 """ 302 """
329 303
304 import unittest
330 import sys 305 import sys
331 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
332 324
333 def test_main(): 325 def test_main():
334 support.run_doctest(sys.modules[__name__], True) 326 test_support.run_doctest(sys.modules[__name__], True)
327 test_support.run_unittest(ExtCallTest)
335 328
336 if __name__ == '__main__': 329 if __name__ == '__main__':
337 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+