1 | n/a | import unittest |
---|
2 | n/a | import weakref |
---|
3 | n/a | |
---|
4 | n/a | from test.support import check_syntax_error, cpython_only |
---|
5 | n/a | |
---|
6 | n/a | |
---|
7 | n/a | class ScopeTests(unittest.TestCase): |
---|
8 | n/a | |
---|
9 | n/a | def testSimpleNesting(self): |
---|
10 | n/a | |
---|
11 | n/a | def make_adder(x): |
---|
12 | n/a | def adder(y): |
---|
13 | n/a | return x + y |
---|
14 | n/a | return adder |
---|
15 | n/a | |
---|
16 | n/a | inc = make_adder(1) |
---|
17 | n/a | plus10 = make_adder(10) |
---|
18 | n/a | |
---|
19 | n/a | self.assertEqual(inc(1), 2) |
---|
20 | n/a | self.assertEqual(plus10(-2), 8) |
---|
21 | n/a | |
---|
22 | n/a | def testExtraNesting(self): |
---|
23 | n/a | |
---|
24 | n/a | def make_adder2(x): |
---|
25 | n/a | def extra(): # check freevars passing through non-use scopes |
---|
26 | n/a | def adder(y): |
---|
27 | n/a | return x + y |
---|
28 | n/a | return adder |
---|
29 | n/a | return extra() |
---|
30 | n/a | |
---|
31 | n/a | inc = make_adder2(1) |
---|
32 | n/a | plus10 = make_adder2(10) |
---|
33 | n/a | |
---|
34 | n/a | self.assertEqual(inc(1), 2) |
---|
35 | n/a | self.assertEqual(plus10(-2), 8) |
---|
36 | n/a | |
---|
37 | n/a | def testSimpleAndRebinding(self): |
---|
38 | n/a | |
---|
39 | n/a | def make_adder3(x): |
---|
40 | n/a | def adder(y): |
---|
41 | n/a | return x + y |
---|
42 | n/a | x = x + 1 # check tracking of assignment to x in defining scope |
---|
43 | n/a | return adder |
---|
44 | n/a | |
---|
45 | n/a | inc = make_adder3(0) |
---|
46 | n/a | plus10 = make_adder3(9) |
---|
47 | n/a | |
---|
48 | n/a | self.assertEqual(inc(1), 2) |
---|
49 | n/a | self.assertEqual(plus10(-2), 8) |
---|
50 | n/a | |
---|
51 | n/a | def testNestingGlobalNoFree(self): |
---|
52 | n/a | |
---|
53 | n/a | def make_adder4(): # XXX add exta level of indirection |
---|
54 | n/a | def nest(): |
---|
55 | n/a | def nest(): |
---|
56 | n/a | def adder(y): |
---|
57 | n/a | return global_x + y # check that plain old globals work |
---|
58 | n/a | return adder |
---|
59 | n/a | return nest() |
---|
60 | n/a | return nest() |
---|
61 | n/a | |
---|
62 | n/a | global_x = 1 |
---|
63 | n/a | adder = make_adder4() |
---|
64 | n/a | self.assertEqual(adder(1), 2) |
---|
65 | n/a | |
---|
66 | n/a | global_x = 10 |
---|
67 | n/a | self.assertEqual(adder(-2), 8) |
---|
68 | n/a | |
---|
69 | n/a | def testNestingThroughClass(self): |
---|
70 | n/a | |
---|
71 | n/a | def make_adder5(x): |
---|
72 | n/a | class Adder: |
---|
73 | n/a | def __call__(self, y): |
---|
74 | n/a | return x + y |
---|
75 | n/a | return Adder() |
---|
76 | n/a | |
---|
77 | n/a | inc = make_adder5(1) |
---|
78 | n/a | plus10 = make_adder5(10) |
---|
79 | n/a | |
---|
80 | n/a | self.assertEqual(inc(1), 2) |
---|
81 | n/a | self.assertEqual(plus10(-2), 8) |
---|
82 | n/a | |
---|
83 | n/a | def testNestingPlusFreeRefToGlobal(self): |
---|
84 | n/a | |
---|
85 | n/a | def make_adder6(x): |
---|
86 | n/a | global global_nest_x |
---|
87 | n/a | def adder(y): |
---|
88 | n/a | return global_nest_x + y |
---|
89 | n/a | global_nest_x = x |
---|
90 | n/a | return adder |
---|
91 | n/a | |
---|
92 | n/a | inc = make_adder6(1) |
---|
93 | n/a | plus10 = make_adder6(10) |
---|
94 | n/a | |
---|
95 | n/a | self.assertEqual(inc(1), 11) # there's only one global |
---|
96 | n/a | self.assertEqual(plus10(-2), 8) |
---|
97 | n/a | |
---|
98 | n/a | def testNearestEnclosingScope(self): |
---|
99 | n/a | |
---|
100 | n/a | def f(x): |
---|
101 | n/a | def g(y): |
---|
102 | n/a | x = 42 # check that this masks binding in f() |
---|
103 | n/a | def h(z): |
---|
104 | n/a | return x + z |
---|
105 | n/a | return h |
---|
106 | n/a | return g(2) |
---|
107 | n/a | |
---|
108 | n/a | test_func = f(10) |
---|
109 | n/a | self.assertEqual(test_func(5), 47) |
---|
110 | n/a | |
---|
111 | n/a | def testMixedFreevarsAndCellvars(self): |
---|
112 | n/a | |
---|
113 | n/a | def identity(x): |
---|
114 | n/a | return x |
---|
115 | n/a | |
---|
116 | n/a | def f(x, y, z): |
---|
117 | n/a | def g(a, b, c): |
---|
118 | n/a | a = a + x # 3 |
---|
119 | n/a | def h(): |
---|
120 | n/a | # z * (4 + 9) |
---|
121 | n/a | # 3 * 13 |
---|
122 | n/a | return identity(z * (b + y)) |
---|
123 | n/a | y = c + z # 9 |
---|
124 | n/a | return h |
---|
125 | n/a | return g |
---|
126 | n/a | |
---|
127 | n/a | g = f(1, 2, 3) |
---|
128 | n/a | h = g(2, 4, 6) |
---|
129 | n/a | self.assertEqual(h(), 39) |
---|
130 | n/a | |
---|
131 | n/a | def testFreeVarInMethod(self): |
---|
132 | n/a | |
---|
133 | n/a | def test(): |
---|
134 | n/a | method_and_var = "var" |
---|
135 | n/a | class Test: |
---|
136 | n/a | def method_and_var(self): |
---|
137 | n/a | return "method" |
---|
138 | n/a | def test(self): |
---|
139 | n/a | return method_and_var |
---|
140 | n/a | def actual_global(self): |
---|
141 | n/a | return str("global") |
---|
142 | n/a | def str(self): |
---|
143 | n/a | return str(self) |
---|
144 | n/a | return Test() |
---|
145 | n/a | |
---|
146 | n/a | t = test() |
---|
147 | n/a | self.assertEqual(t.test(), "var") |
---|
148 | n/a | self.assertEqual(t.method_and_var(), "method") |
---|
149 | n/a | self.assertEqual(t.actual_global(), "global") |
---|
150 | n/a | |
---|
151 | n/a | method_and_var = "var" |
---|
152 | n/a | class Test: |
---|
153 | n/a | # this class is not nested, so the rules are different |
---|
154 | n/a | def method_and_var(self): |
---|
155 | n/a | return "method" |
---|
156 | n/a | def test(self): |
---|
157 | n/a | return method_and_var |
---|
158 | n/a | def actual_global(self): |
---|
159 | n/a | return str("global") |
---|
160 | n/a | def str(self): |
---|
161 | n/a | return str(self) |
---|
162 | n/a | |
---|
163 | n/a | t = Test() |
---|
164 | n/a | self.assertEqual(t.test(), "var") |
---|
165 | n/a | self.assertEqual(t.method_and_var(), "method") |
---|
166 | n/a | self.assertEqual(t.actual_global(), "global") |
---|
167 | n/a | |
---|
168 | n/a | def testCellIsKwonlyArg(self): |
---|
169 | n/a | # Issue 1409: Initialisation of a cell value, |
---|
170 | n/a | # when it comes from a keyword-only parameter |
---|
171 | n/a | def foo(*, a=17): |
---|
172 | n/a | def bar(): |
---|
173 | n/a | return a + 5 |
---|
174 | n/a | return bar() + 3 |
---|
175 | n/a | |
---|
176 | n/a | self.assertEqual(foo(a=42), 50) |
---|
177 | n/a | self.assertEqual(foo(), 25) |
---|
178 | n/a | |
---|
179 | n/a | def testRecursion(self): |
---|
180 | n/a | |
---|
181 | n/a | def f(x): |
---|
182 | n/a | def fact(n): |
---|
183 | n/a | if n == 0: |
---|
184 | n/a | return 1 |
---|
185 | n/a | else: |
---|
186 | n/a | return n * fact(n - 1) |
---|
187 | n/a | if x >= 0: |
---|
188 | n/a | return fact(x) |
---|
189 | n/a | else: |
---|
190 | n/a | raise ValueError("x must be >= 0") |
---|
191 | n/a | |
---|
192 | n/a | self.assertEqual(f(6), 720) |
---|
193 | n/a | |
---|
194 | n/a | |
---|
195 | n/a | def testUnoptimizedNamespaces(self): |
---|
196 | n/a | |
---|
197 | n/a | check_syntax_error(self, """if 1: |
---|
198 | n/a | def unoptimized_clash1(strip): |
---|
199 | n/a | def f(s): |
---|
200 | n/a | from sys import * |
---|
201 | n/a | return getrefcount(s) # ambiguity: free or local |
---|
202 | n/a | return f |
---|
203 | n/a | """) |
---|
204 | n/a | |
---|
205 | n/a | check_syntax_error(self, """if 1: |
---|
206 | n/a | def unoptimized_clash2(): |
---|
207 | n/a | from sys import * |
---|
208 | n/a | def f(s): |
---|
209 | n/a | return getrefcount(s) # ambiguity: global or local |
---|
210 | n/a | return f |
---|
211 | n/a | """) |
---|
212 | n/a | |
---|
213 | n/a | check_syntax_error(self, """if 1: |
---|
214 | n/a | def unoptimized_clash2(): |
---|
215 | n/a | from sys import * |
---|
216 | n/a | def g(): |
---|
217 | n/a | def f(s): |
---|
218 | n/a | return getrefcount(s) # ambiguity: global or local |
---|
219 | n/a | return f |
---|
220 | n/a | """) |
---|
221 | n/a | |
---|
222 | n/a | check_syntax_error(self, """if 1: |
---|
223 | n/a | def f(): |
---|
224 | n/a | def g(): |
---|
225 | n/a | from sys import * |
---|
226 | n/a | return getrefcount # global or local? |
---|
227 | n/a | """) |
---|
228 | n/a | |
---|
229 | n/a | def testLambdas(self): |
---|
230 | n/a | |
---|
231 | n/a | f1 = lambda x: lambda y: x + y |
---|
232 | n/a | inc = f1(1) |
---|
233 | n/a | plus10 = f1(10) |
---|
234 | n/a | self.assertEqual(inc(1), 2) |
---|
235 | n/a | self.assertEqual(plus10(5), 15) |
---|
236 | n/a | |
---|
237 | n/a | f2 = lambda x: (lambda : lambda y: x + y)() |
---|
238 | n/a | inc = f2(1) |
---|
239 | n/a | plus10 = f2(10) |
---|
240 | n/a | self.assertEqual(inc(1), 2) |
---|
241 | n/a | self.assertEqual(plus10(5), 15) |
---|
242 | n/a | |
---|
243 | n/a | f3 = lambda x: lambda y: global_x + y |
---|
244 | n/a | global_x = 1 |
---|
245 | n/a | inc = f3(None) |
---|
246 | n/a | self.assertEqual(inc(2), 3) |
---|
247 | n/a | |
---|
248 | n/a | f8 = lambda x, y, z: lambda a, b, c: lambda : z * (b + y) |
---|
249 | n/a | g = f8(1, 2, 3) |
---|
250 | n/a | h = g(2, 4, 6) |
---|
251 | n/a | self.assertEqual(h(), 18) |
---|
252 | n/a | |
---|
253 | n/a | def testUnboundLocal(self): |
---|
254 | n/a | |
---|
255 | n/a | def errorInOuter(): |
---|
256 | n/a | print(y) |
---|
257 | n/a | def inner(): |
---|
258 | n/a | return y |
---|
259 | n/a | y = 1 |
---|
260 | n/a | |
---|
261 | n/a | def errorInInner(): |
---|
262 | n/a | def inner(): |
---|
263 | n/a | return y |
---|
264 | n/a | inner() |
---|
265 | n/a | y = 1 |
---|
266 | n/a | |
---|
267 | n/a | self.assertRaises(UnboundLocalError, errorInOuter) |
---|
268 | n/a | self.assertRaises(NameError, errorInInner) |
---|
269 | n/a | |
---|
270 | n/a | def testUnboundLocal_AfterDel(self): |
---|
271 | n/a | # #4617: It is now legal to delete a cell variable. |
---|
272 | n/a | # The following functions must obviously compile, |
---|
273 | n/a | # and give the correct error when accessing the deleted name. |
---|
274 | n/a | def errorInOuter(): |
---|
275 | n/a | y = 1 |
---|
276 | n/a | del y |
---|
277 | n/a | print(y) |
---|
278 | n/a | def inner(): |
---|
279 | n/a | return y |
---|
280 | n/a | |
---|
281 | n/a | def errorInInner(): |
---|
282 | n/a | def inner(): |
---|
283 | n/a | return y |
---|
284 | n/a | y = 1 |
---|
285 | n/a | del y |
---|
286 | n/a | inner() |
---|
287 | n/a | |
---|
288 | n/a | self.assertRaises(UnboundLocalError, errorInOuter) |
---|
289 | n/a | self.assertRaises(NameError, errorInInner) |
---|
290 | n/a | |
---|
291 | n/a | def testUnboundLocal_AugAssign(self): |
---|
292 | n/a | # test for bug #1501934: incorrect LOAD/STORE_GLOBAL generation |
---|
293 | n/a | exec("""if 1: |
---|
294 | n/a | global_x = 1 |
---|
295 | n/a | def f(): |
---|
296 | n/a | global_x += 1 |
---|
297 | n/a | try: |
---|
298 | n/a | f() |
---|
299 | n/a | except UnboundLocalError: |
---|
300 | n/a | pass |
---|
301 | n/a | else: |
---|
302 | n/a | fail('scope of global_x not correctly determined') |
---|
303 | n/a | """, {'fail': self.fail}) |
---|
304 | n/a | |
---|
305 | n/a | def testComplexDefinitions(self): |
---|
306 | n/a | |
---|
307 | n/a | def makeReturner(*lst): |
---|
308 | n/a | def returner(): |
---|
309 | n/a | return lst |
---|
310 | n/a | return returner |
---|
311 | n/a | |
---|
312 | n/a | self.assertEqual(makeReturner(1,2,3)(), (1,2,3)) |
---|
313 | n/a | |
---|
314 | n/a | def makeReturner2(**kwargs): |
---|
315 | n/a | def returner(): |
---|
316 | n/a | return kwargs |
---|
317 | n/a | return returner |
---|
318 | n/a | |
---|
319 | n/a | self.assertEqual(makeReturner2(a=11)()['a'], 11) |
---|
320 | n/a | |
---|
321 | n/a | def testScopeOfGlobalStmt(self): |
---|
322 | n/a | # Examples posted by Samuele Pedroni to python-dev on 3/1/2001 |
---|
323 | n/a | |
---|
324 | n/a | exec("""if 1: |
---|
325 | n/a | # I |
---|
326 | n/a | x = 7 |
---|
327 | n/a | def f(): |
---|
328 | n/a | x = 1 |
---|
329 | n/a | def g(): |
---|
330 | n/a | global x |
---|
331 | n/a | def i(): |
---|
332 | n/a | def h(): |
---|
333 | n/a | return x |
---|
334 | n/a | return h() |
---|
335 | n/a | return i() |
---|
336 | n/a | return g() |
---|
337 | n/a | self.assertEqual(f(), 7) |
---|
338 | n/a | self.assertEqual(x, 7) |
---|
339 | n/a | |
---|
340 | n/a | # II |
---|
341 | n/a | x = 7 |
---|
342 | n/a | def f(): |
---|
343 | n/a | x = 1 |
---|
344 | n/a | def g(): |
---|
345 | n/a | x = 2 |
---|
346 | n/a | def i(): |
---|
347 | n/a | def h(): |
---|
348 | n/a | return x |
---|
349 | n/a | return h() |
---|
350 | n/a | return i() |
---|
351 | n/a | return g() |
---|
352 | n/a | self.assertEqual(f(), 2) |
---|
353 | n/a | self.assertEqual(x, 7) |
---|
354 | n/a | |
---|
355 | n/a | # III |
---|
356 | n/a | x = 7 |
---|
357 | n/a | def f(): |
---|
358 | n/a | x = 1 |
---|
359 | n/a | def g(): |
---|
360 | n/a | global x |
---|
361 | n/a | x = 2 |
---|
362 | n/a | def i(): |
---|
363 | n/a | def h(): |
---|
364 | n/a | return x |
---|
365 | n/a | return h() |
---|
366 | n/a | return i() |
---|
367 | n/a | return g() |
---|
368 | n/a | self.assertEqual(f(), 2) |
---|
369 | n/a | self.assertEqual(x, 2) |
---|
370 | n/a | |
---|
371 | n/a | # IV |
---|
372 | n/a | x = 7 |
---|
373 | n/a | def f(): |
---|
374 | n/a | x = 3 |
---|
375 | n/a | def g(): |
---|
376 | n/a | global x |
---|
377 | n/a | x = 2 |
---|
378 | n/a | def i(): |
---|
379 | n/a | def h(): |
---|
380 | n/a | return x |
---|
381 | n/a | return h() |
---|
382 | n/a | return i() |
---|
383 | n/a | return g() |
---|
384 | n/a | self.assertEqual(f(), 2) |
---|
385 | n/a | self.assertEqual(x, 2) |
---|
386 | n/a | |
---|
387 | n/a | # XXX what about global statements in class blocks? |
---|
388 | n/a | # do they affect methods? |
---|
389 | n/a | |
---|
390 | n/a | x = 12 |
---|
391 | n/a | class Global: |
---|
392 | n/a | global x |
---|
393 | n/a | x = 13 |
---|
394 | n/a | def set(self, val): |
---|
395 | n/a | x = val |
---|
396 | n/a | def get(self): |
---|
397 | n/a | return x |
---|
398 | n/a | |
---|
399 | n/a | g = Global() |
---|
400 | n/a | self.assertEqual(g.get(), 13) |
---|
401 | n/a | g.set(15) |
---|
402 | n/a | self.assertEqual(g.get(), 13) |
---|
403 | n/a | """) |
---|
404 | n/a | |
---|
405 | n/a | def testLeaks(self): |
---|
406 | n/a | |
---|
407 | n/a | class Foo: |
---|
408 | n/a | count = 0 |
---|
409 | n/a | |
---|
410 | n/a | def __init__(self): |
---|
411 | n/a | Foo.count += 1 |
---|
412 | n/a | |
---|
413 | n/a | def __del__(self): |
---|
414 | n/a | Foo.count -= 1 |
---|
415 | n/a | |
---|
416 | n/a | def f1(): |
---|
417 | n/a | x = Foo() |
---|
418 | n/a | def f2(): |
---|
419 | n/a | return x |
---|
420 | n/a | f2() |
---|
421 | n/a | |
---|
422 | n/a | for i in range(100): |
---|
423 | n/a | f1() |
---|
424 | n/a | |
---|
425 | n/a | self.assertEqual(Foo.count, 0) |
---|
426 | n/a | |
---|
427 | n/a | def testClassAndGlobal(self): |
---|
428 | n/a | |
---|
429 | n/a | exec("""if 1: |
---|
430 | n/a | def test(x): |
---|
431 | n/a | class Foo: |
---|
432 | n/a | global x |
---|
433 | n/a | def __call__(self, y): |
---|
434 | n/a | return x + y |
---|
435 | n/a | return Foo() |
---|
436 | n/a | |
---|
437 | n/a | x = 0 |
---|
438 | n/a | self.assertEqual(test(6)(2), 8) |
---|
439 | n/a | x = -1 |
---|
440 | n/a | self.assertEqual(test(3)(2), 5) |
---|
441 | n/a | |
---|
442 | n/a | looked_up_by_load_name = False |
---|
443 | n/a | class X: |
---|
444 | n/a | # Implicit globals inside classes are be looked up by LOAD_NAME, not |
---|
445 | n/a | # LOAD_GLOBAL. |
---|
446 | n/a | locals()['looked_up_by_load_name'] = True |
---|
447 | n/a | passed = looked_up_by_load_name |
---|
448 | n/a | |
---|
449 | n/a | self.assertTrue(X.passed) |
---|
450 | n/a | """) |
---|
451 | n/a | |
---|
452 | n/a | def testLocalsFunction(self): |
---|
453 | n/a | |
---|
454 | n/a | def f(x): |
---|
455 | n/a | def g(y): |
---|
456 | n/a | def h(z): |
---|
457 | n/a | return y + z |
---|
458 | n/a | w = x + y |
---|
459 | n/a | y += 3 |
---|
460 | n/a | return locals() |
---|
461 | n/a | return g |
---|
462 | n/a | |
---|
463 | n/a | d = f(2)(4) |
---|
464 | n/a | self.assertIn('h', d) |
---|
465 | n/a | del d['h'] |
---|
466 | n/a | self.assertEqual(d, {'x': 2, 'y': 7, 'w': 6}) |
---|
467 | n/a | |
---|
468 | n/a | def testLocalsClass(self): |
---|
469 | n/a | # This test verifies that calling locals() does not pollute |
---|
470 | n/a | # the local namespace of the class with free variables. Old |
---|
471 | n/a | # versions of Python had a bug, where a free variable being |
---|
472 | n/a | # passed through a class namespace would be inserted into |
---|
473 | n/a | # locals() by locals() or exec or a trace function. |
---|
474 | n/a | # |
---|
475 | n/a | # The real bug lies in frame code that copies variables |
---|
476 | n/a | # between fast locals and the locals dict, e.g. when executing |
---|
477 | n/a | # a trace function. |
---|
478 | n/a | |
---|
479 | n/a | def f(x): |
---|
480 | n/a | class C: |
---|
481 | n/a | x = 12 |
---|
482 | n/a | def m(self): |
---|
483 | n/a | return x |
---|
484 | n/a | locals() |
---|
485 | n/a | return C |
---|
486 | n/a | |
---|
487 | n/a | self.assertEqual(f(1).x, 12) |
---|
488 | n/a | |
---|
489 | n/a | def f(x): |
---|
490 | n/a | class C: |
---|
491 | n/a | y = x |
---|
492 | n/a | def m(self): |
---|
493 | n/a | return x |
---|
494 | n/a | z = list(locals()) |
---|
495 | n/a | return C |
---|
496 | n/a | |
---|
497 | n/a | varnames = f(1).z |
---|
498 | n/a | self.assertNotIn("x", varnames) |
---|
499 | n/a | self.assertIn("y", varnames) |
---|
500 | n/a | |
---|
501 | n/a | @cpython_only |
---|
502 | n/a | def testLocalsClass_WithTrace(self): |
---|
503 | n/a | # Issue23728: after the trace function returns, the locals() |
---|
504 | n/a | # dictionary is used to update all variables, this used to |
---|
505 | n/a | # include free variables. But in class statements, free |
---|
506 | n/a | # variables are not inserted... |
---|
507 | n/a | import sys |
---|
508 | n/a | self.addCleanup(sys.settrace, sys.gettrace()) |
---|
509 | n/a | sys.settrace(lambda a,b,c:None) |
---|
510 | n/a | x = 12 |
---|
511 | n/a | |
---|
512 | n/a | class C: |
---|
513 | n/a | def f(self): |
---|
514 | n/a | return x |
---|
515 | n/a | |
---|
516 | n/a | self.assertEqual(x, 12) # Used to raise UnboundLocalError |
---|
517 | n/a | |
---|
518 | n/a | def testBoundAndFree(self): |
---|
519 | n/a | # var is bound and free in class |
---|
520 | n/a | |
---|
521 | n/a | def f(x): |
---|
522 | n/a | class C: |
---|
523 | n/a | def m(self): |
---|
524 | n/a | return x |
---|
525 | n/a | a = x |
---|
526 | n/a | return C |
---|
527 | n/a | |
---|
528 | n/a | inst = f(3)() |
---|
529 | n/a | self.assertEqual(inst.a, inst.m()) |
---|
530 | n/a | |
---|
531 | n/a | @cpython_only |
---|
532 | n/a | def testInteractionWithTraceFunc(self): |
---|
533 | n/a | |
---|
534 | n/a | import sys |
---|
535 | n/a | def tracer(a,b,c): |
---|
536 | n/a | return tracer |
---|
537 | n/a | |
---|
538 | n/a | def adaptgetter(name, klass, getter): |
---|
539 | n/a | kind, des = getter |
---|
540 | n/a | if kind == 1: # AV happens when stepping from this line to next |
---|
541 | n/a | if des == "": |
---|
542 | n/a | des = "_%s__%s" % (klass.__name__, name) |
---|
543 | n/a | return lambda obj: getattr(obj, des) |
---|
544 | n/a | |
---|
545 | n/a | class TestClass: |
---|
546 | n/a | pass |
---|
547 | n/a | |
---|
548 | n/a | self.addCleanup(sys.settrace, sys.gettrace()) |
---|
549 | n/a | sys.settrace(tracer) |
---|
550 | n/a | adaptgetter("foo", TestClass, (1, "")) |
---|
551 | n/a | sys.settrace(None) |
---|
552 | n/a | |
---|
553 | n/a | self.assertRaises(TypeError, sys.settrace) |
---|
554 | n/a | |
---|
555 | n/a | def testEvalExecFreeVars(self): |
---|
556 | n/a | |
---|
557 | n/a | def f(x): |
---|
558 | n/a | return lambda: x + 1 |
---|
559 | n/a | |
---|
560 | n/a | g = f(3) |
---|
561 | n/a | self.assertRaises(TypeError, eval, g.__code__) |
---|
562 | n/a | |
---|
563 | n/a | try: |
---|
564 | n/a | exec(g.__code__, {}) |
---|
565 | n/a | except TypeError: |
---|
566 | n/a | pass |
---|
567 | n/a | else: |
---|
568 | n/a | self.fail("exec should have failed, because code contained free vars") |
---|
569 | n/a | |
---|
570 | n/a | def testListCompLocalVars(self): |
---|
571 | n/a | |
---|
572 | n/a | try: |
---|
573 | n/a | print(bad) |
---|
574 | n/a | except NameError: |
---|
575 | n/a | pass |
---|
576 | n/a | else: |
---|
577 | n/a | print("bad should not be defined") |
---|
578 | n/a | |
---|
579 | n/a | def x(): |
---|
580 | n/a | [bad for s in 'a b' for bad in s.split()] |
---|
581 | n/a | |
---|
582 | n/a | x() |
---|
583 | n/a | try: |
---|
584 | n/a | print(bad) |
---|
585 | n/a | except NameError: |
---|
586 | n/a | pass |
---|
587 | n/a | |
---|
588 | n/a | def testEvalFreeVars(self): |
---|
589 | n/a | |
---|
590 | n/a | def f(x): |
---|
591 | n/a | def g(): |
---|
592 | n/a | x |
---|
593 | n/a | eval("x + 1") |
---|
594 | n/a | return g |
---|
595 | n/a | |
---|
596 | n/a | f(4)() |
---|
597 | n/a | |
---|
598 | n/a | def testFreeingCell(self): |
---|
599 | n/a | # Test what happens when a finalizer accesses |
---|
600 | n/a | # the cell where the object was stored. |
---|
601 | n/a | class Special: |
---|
602 | n/a | def __del__(self): |
---|
603 | n/a | nestedcell_get() |
---|
604 | n/a | |
---|
605 | n/a | def testNonLocalFunction(self): |
---|
606 | n/a | |
---|
607 | n/a | def f(x): |
---|
608 | n/a | def inc(): |
---|
609 | n/a | nonlocal x |
---|
610 | n/a | x += 1 |
---|
611 | n/a | return x |
---|
612 | n/a | def dec(): |
---|
613 | n/a | nonlocal x |
---|
614 | n/a | x -= 1 |
---|
615 | n/a | return x |
---|
616 | n/a | return inc, dec |
---|
617 | n/a | |
---|
618 | n/a | inc, dec = f(0) |
---|
619 | n/a | self.assertEqual(inc(), 1) |
---|
620 | n/a | self.assertEqual(inc(), 2) |
---|
621 | n/a | self.assertEqual(dec(), 1) |
---|
622 | n/a | self.assertEqual(dec(), 0) |
---|
623 | n/a | |
---|
624 | n/a | def testNonLocalMethod(self): |
---|
625 | n/a | def f(x): |
---|
626 | n/a | class c: |
---|
627 | n/a | def inc(self): |
---|
628 | n/a | nonlocal x |
---|
629 | n/a | x += 1 |
---|
630 | n/a | return x |
---|
631 | n/a | def dec(self): |
---|
632 | n/a | nonlocal x |
---|
633 | n/a | x -= 1 |
---|
634 | n/a | return x |
---|
635 | n/a | return c() |
---|
636 | n/a | c = f(0) |
---|
637 | n/a | self.assertEqual(c.inc(), 1) |
---|
638 | n/a | self.assertEqual(c.inc(), 2) |
---|
639 | n/a | self.assertEqual(c.dec(), 1) |
---|
640 | n/a | self.assertEqual(c.dec(), 0) |
---|
641 | n/a | |
---|
642 | n/a | def testGlobalInParallelNestedFunctions(self): |
---|
643 | n/a | # A symbol table bug leaked the global statement from one |
---|
644 | n/a | # function to other nested functions in the same block. |
---|
645 | n/a | # This test verifies that a global statement in the first |
---|
646 | n/a | # function does not affect the second function. |
---|
647 | n/a | local_ns = {} |
---|
648 | n/a | global_ns = {} |
---|
649 | n/a | exec("""if 1: |
---|
650 | n/a | def f(): |
---|
651 | n/a | y = 1 |
---|
652 | n/a | def g(): |
---|
653 | n/a | global y |
---|
654 | n/a | return y |
---|
655 | n/a | def h(): |
---|
656 | n/a | return y + 1 |
---|
657 | n/a | return g, h |
---|
658 | n/a | y = 9 |
---|
659 | n/a | g, h = f() |
---|
660 | n/a | result9 = g() |
---|
661 | n/a | result2 = h() |
---|
662 | n/a | """, local_ns, global_ns) |
---|
663 | n/a | self.assertEqual(2, global_ns["result2"]) |
---|
664 | n/a | self.assertEqual(9, global_ns["result9"]) |
---|
665 | n/a | |
---|
666 | n/a | def testNonLocalClass(self): |
---|
667 | n/a | |
---|
668 | n/a | def f(x): |
---|
669 | n/a | class c: |
---|
670 | n/a | nonlocal x |
---|
671 | n/a | x += 1 |
---|
672 | n/a | def get(self): |
---|
673 | n/a | return x |
---|
674 | n/a | return c() |
---|
675 | n/a | |
---|
676 | n/a | c = f(0) |
---|
677 | n/a | self.assertEqual(c.get(), 1) |
---|
678 | n/a | self.assertNotIn("x", c.__class__.__dict__) |
---|
679 | n/a | |
---|
680 | n/a | |
---|
681 | n/a | def testNonLocalGenerator(self): |
---|
682 | n/a | |
---|
683 | n/a | def f(x): |
---|
684 | n/a | def g(y): |
---|
685 | n/a | nonlocal x |
---|
686 | n/a | for i in range(y): |
---|
687 | n/a | x += 1 |
---|
688 | n/a | yield x |
---|
689 | n/a | return g |
---|
690 | n/a | |
---|
691 | n/a | g = f(0) |
---|
692 | n/a | self.assertEqual(list(g(5)), [1, 2, 3, 4, 5]) |
---|
693 | n/a | |
---|
694 | n/a | def testNestedNonLocal(self): |
---|
695 | n/a | |
---|
696 | n/a | def f(x): |
---|
697 | n/a | def g(): |
---|
698 | n/a | nonlocal x |
---|
699 | n/a | x -= 2 |
---|
700 | n/a | def h(): |
---|
701 | n/a | nonlocal x |
---|
702 | n/a | x += 4 |
---|
703 | n/a | return x |
---|
704 | n/a | return h |
---|
705 | n/a | return g |
---|
706 | n/a | |
---|
707 | n/a | g = f(1) |
---|
708 | n/a | h = g() |
---|
709 | n/a | self.assertEqual(h(), 3) |
---|
710 | n/a | |
---|
711 | n/a | def testTopIsNotSignificant(self): |
---|
712 | n/a | # See #9997. |
---|
713 | n/a | def top(a): |
---|
714 | n/a | pass |
---|
715 | n/a | def b(): |
---|
716 | n/a | global a |
---|
717 | n/a | |
---|
718 | n/a | def testClassNamespaceOverridesClosure(self): |
---|
719 | n/a | # See #17853. |
---|
720 | n/a | x = 42 |
---|
721 | n/a | class X: |
---|
722 | n/a | locals()["x"] = 43 |
---|
723 | n/a | y = x |
---|
724 | n/a | self.assertEqual(X.y, 43) |
---|
725 | n/a | class X: |
---|
726 | n/a | locals()["x"] = 43 |
---|
727 | n/a | del x |
---|
728 | n/a | self.assertFalse(hasattr(X, "x")) |
---|
729 | n/a | self.assertEqual(x, 42) |
---|
730 | n/a | |
---|
731 | n/a | @cpython_only |
---|
732 | n/a | def testCellLeak(self): |
---|
733 | n/a | # Issue 17927. |
---|
734 | n/a | # |
---|
735 | n/a | # The issue was that if self was part of a cycle involving the |
---|
736 | n/a | # frame of a method call, *and* the method contained a nested |
---|
737 | n/a | # function referencing self, thereby forcing 'self' into a |
---|
738 | n/a | # cell, setting self to None would not be enough to break the |
---|
739 | n/a | # frame -- the frame had another reference to the instance, |
---|
740 | n/a | # which could not be cleared by the code running in the frame |
---|
741 | n/a | # (though it will be cleared when the frame is collected). |
---|
742 | n/a | # Without the lambda, setting self to None is enough to break |
---|
743 | n/a | # the cycle. |
---|
744 | n/a | class Tester: |
---|
745 | n/a | def dig(self): |
---|
746 | n/a | if 0: |
---|
747 | n/a | lambda: self |
---|
748 | n/a | try: |
---|
749 | n/a | 1/0 |
---|
750 | n/a | except Exception as exc: |
---|
751 | n/a | self.exc = exc |
---|
752 | n/a | self = None # Break the cycle |
---|
753 | n/a | tester = Tester() |
---|
754 | n/a | tester.dig() |
---|
755 | n/a | ref = weakref.ref(tester) |
---|
756 | n/a | del tester |
---|
757 | n/a | self.assertIsNone(ref()) |
---|
758 | n/a | |
---|
759 | n/a | |
---|
760 | n/a | if __name__ == '__main__': |
---|
761 | n/a | unittest.main() |
---|