Skip to content

Commit 66fdf6b

Browse files
committed
python: add test for capturing by value
1 parent 003fece commit 66fdf6b

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

python/ql/test/experimental/dataflow/validTest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def check_tests_valid_after_version(testFile, version):
7171
check_tests_valid("variable-capture.global")
7272
check_tests_valid("variable-capture.dict")
7373
check_tests_valid("variable-capture.test_collections")
74+
check_tests_valid("variable-capture.by_value")
7475
check_tests_valid("module-initialization.multiphase")
7576
check_tests_valid("fieldflow.test")
7677
check_tests_valid_after_version("match.test", (3, 10))
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Here we test writing to a captured variable via the `nonlocal` keyword (see `out`).
2+
# We also test reading one captured variable and writing the value to another (see `through`).
3+
4+
# All functions starting with "test_" should run and execute `print("OK")` exactly once.
5+
# This can be checked by running validTest.py.
6+
7+
import sys
8+
import os
9+
10+
sys.path.append(os.path.dirname(os.path.dirname((__file__))))
11+
from testlib import expects
12+
13+
# These are defined so that we can evaluate the test code.
14+
NONSOURCE = "not a source"
15+
SOURCE = "source"
16+
17+
def is_source(x):
18+
return x == "source" or x == b"source" or x == 42 or x == 42.0 or x == 42j
19+
20+
21+
def SINK(x):
22+
if is_source(x):
23+
print("OK")
24+
else:
25+
print("Unexpected flow", x)
26+
27+
28+
def SINK_F(x):
29+
if is_source(x):
30+
print("Unexpected flow", x)
31+
else:
32+
print("OK")
33+
34+
35+
def by_value1():
36+
a = SOURCE
37+
def inner(a_val=a):
38+
SINK(a_val) #$ captured
39+
SINK_F(a)
40+
a = NONSOURCE
41+
inner()
42+
43+
def by_value2():
44+
a = NONSOURCE
45+
def inner(a_val=a):
46+
SINK(a) #$ MISSING:captured
47+
SINK_F(a_val)
48+
a = SOURCE
49+
inner()
50+
51+
@expects(4)
52+
def test_by_value():
53+
by_value1()
54+
by_value2()

0 commit comments

Comments
 (0)