Skip to content

Commit 17c9ba9

Browse files
authored
Merge pull request github#12464 from yoff/python/add-test-captured-in-collection
python: add test for captured variables in lists
2 parents 8c738b7 + bbb43a5 commit 17c9ba9

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Here we test the case where a captured variable is being read.
2+
3+
# All functions starting with "test_" should run and execute `print("OK")` exactly once.
4+
# This can be checked by running validTest.py.
5+
6+
import sys
7+
import os
8+
9+
sys.path.append(os.path.dirname(os.path.dirname((__file__))))
10+
from testlib import expects
11+
12+
# These are defined so that we can evaluate the test code.
13+
NONSOURCE = "not a source"
14+
SOURCE = "source"
15+
16+
def is_source(x):
17+
return x == "source" or x == b"source" or x == 42 or x == 42.0 or x == 42j
18+
19+
20+
def SINK(x):
21+
if is_source(x):
22+
print("OK")
23+
else:
24+
print("Unexpected flow", x)
25+
26+
27+
def SINK_F(x):
28+
if is_source(x):
29+
print("Unexpected flow", x)
30+
else:
31+
print("OK")
32+
33+
l = [NONSOURCE]
34+
SINK_F(l_mod[0])
35+
36+
l_mod = [SOURCE for x in l]
37+
SINK(l_mod[0]) #$ captured
38+
39+
l_mod_lambda = [(lambda a : SOURCE)(x) for x in l]
40+
SINK(l_mod_lambda[0]) #$ captured
41+
42+
def mod(x):
43+
return SOURCE
44+
45+
l_mod_function = [mod(x) for x in l]
46+
SINK(l_mod_function[0]) #$ captured
47+
48+
def mod_list(l):
49+
def mod_local(x):
50+
return SOURCE
51+
52+
return [mod_local(x) for x in l]
53+
54+
l_modded = mod_list(l)
55+
SINK(l_modded[0]) #$ MISSING: captured
56+
57+
def mod_list_first(l):
58+
def mod_local(x):
59+
return SOURCE
60+
61+
return [mod_local(l[0])]
62+
63+
l_modded_first = mod_list_first(l)
64+
SINK(l_modded_first[0]) #$ captured

0 commit comments

Comments
 (0)