Skip to content

Commit 9b198c6

Browse files
committed
Python: Add some module initialization tests
1 parent f90d1fd commit 9b198c6

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo = 3
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# constant
2+
foo = 42
3+
4+
import base
5+
6+
def passOn(x):
7+
return x
8+
9+
# depends on other constant
10+
bar = passOn(base.foo)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# These are defined so that we can evaluate the test code.
2+
NONSOURCE = "not a source"
3+
SOURCE = "source"
4+
5+
6+
def is_source(x):
7+
return x == "source" or x == b"source" or x == 42 or x == 42.0 or x == 42j
8+
9+
10+
def SINK(x):
11+
if is_source(x):
12+
print("OK")
13+
else:
14+
print("Unexpected flow", x)
15+
16+
17+
def SINK_F(x):
18+
if is_source(x):
19+
print("Unexpected flow", x)
20+
else:
21+
print("OK")
22+
23+
import base
24+
25+
base.foo = 42
26+
27+
import m1
28+
29+
def test_const():
30+
SINK(m1.foo)
31+
32+
def test_overwritten():
33+
SINK(m1.bar)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# These are defined so that we can evaluate the test code.
2+
NONSOURCE = "not a source"
3+
SOURCE = "source"
4+
5+
6+
def is_source(x):
7+
return x == "source" or x == b"source" or x == 42 or x == 42.0 or x == 42j
8+
9+
10+
def SINK(x):
11+
if is_source(x):
12+
print("OK")
13+
else:
14+
print("Unexpected flow", x)
15+
16+
17+
def SINK_F(x):
18+
if is_source(x):
19+
print("Unexpected flow", x)
20+
else:
21+
print("OK")
22+
23+
import m1
24+
25+
import base
26+
27+
base.foo = 42
28+
29+
def test_const():
30+
SINK(m1.foo)
31+
32+
def test_unoverwritten():
33+
SINK_F(m1.bar)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,6 @@ def check_tests_valid(testFile):
5656
check_tests_valid("variable-capture.in")
5757
check_tests_valid("variable-capture.nonlocal")
5858
check_tests_valid("variable-capture.dict")
59+
# The below fails when trying to import modules
60+
# check_tests_valid("module-initialization.test")
61+
# check_tests_valid("module-initialization.testOnce")

0 commit comments

Comments
 (0)