Skip to content

Commit 0e3d520

Browse files
committed
Python: Add variables regression test
As illustrated when running the python file, the non qualified reads in the `use` method all refer to the global variables, whereas `ex = func(baz)` are to the things defined on the class. The important part of the .expected changes is that the _global_ variable `bar` is used inside the function, whereas it's the local variable for `foo` (on class scope) that is used inside the function (which is wrong).
1 parent 98db1af commit 0e3d520

File tree

7 files changed

+133
-0
lines changed

7 files changed

+133
-0
lines changed

python/ql/test/library-tests/variables/scopes/access.expected

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,40 @@
1+
| in_class.py:0:0:0:0 | Module in_class | Global Variable MyClass | in_class.py:5:7:5:13 | MyClass |
2+
| in_class.py:0:0:0:0 | Module in_class | Global Variable MyClass | in_class.py:30:6:30:12 | MyClass |
3+
| in_class.py:0:0:0:0 | Module in_class | Global Variable NameError | in_class.py:17:16:17:24 | NameError |
4+
| in_class.py:0:0:0:0 | Module in_class | Global Variable bar | in_class.py:2:1:2:3 | bar |
5+
| in_class.py:0:0:0:0 | Module in_class | Global Variable bar | in_class.py:14:15:14:17 | bar |
6+
| in_class.py:0:0:0:0 | Module in_class | Global Variable foo | in_class.py:1:1:1:3 | foo |
7+
| in_class.py:0:0:0:0 | Module in_class | Global Variable mc | in_class.py:30:1:30:2 | mc |
8+
| in_class.py:0:0:0:0 | Module in_class | Global Variable mc | in_class.py:32:1:32:2 | mc |
9+
| in_class.py:0:0:0:0 | Module in_class | Global Variable mc | in_class.py:35:7:35:8 | mc |
10+
| in_class.py:0:0:0:0 | Module in_class | Global Variable object | in_class.py:5:15:5:20 | object |
11+
| in_class.py:0:0:0:0 | Module in_class | Global Variable print | in_class.py:9:9:9:13 | print |
12+
| in_class.py:0:0:0:0 | Module in_class | Global Variable print | in_class.py:12:9:12:13 | print |
13+
| in_class.py:0:0:0:0 | Module in_class | Global Variable print | in_class.py:13:9:13:13 | print |
14+
| in_class.py:0:0:0:0 | Module in_class | Global Variable print | in_class.py:14:9:14:13 | print |
15+
| in_class.py:0:0:0:0 | Module in_class | Global Variable print | in_class.py:16:13:16:17 | print |
16+
| in_class.py:0:0:0:0 | Module in_class | Global Variable print | in_class.py:20:9:20:13 | print |
17+
| in_class.py:0:0:0:0 | Module in_class | Global Variable print | in_class.py:21:9:21:13 | print |
18+
| in_class.py:0:0:0:0 | Module in_class | Global Variable print | in_class.py:22:9:22:13 | print |
19+
| in_class.py:0:0:0:0 | Module in_class | Global Variable print | in_class.py:25:9:25:13 | print |
20+
| in_class.py:0:0:0:0 | Module in_class | Global Variable print | in_class.py:31:1:31:5 | print |
21+
| in_class.py:0:0:0:0 | Module in_class | Global Variable print | in_class.py:34:1:34:5 | print |
22+
| in_class.py:0:0:0:0 | Module in_class | Global Variable print | in_class.py:35:1:35:5 | print |
23+
| in_class.py:5:1:5:22 | Class MyClass | Local Variable baz | in_class.py:6:5:6:7 | baz |
24+
| in_class.py:5:1:5:22 | Class MyClass | Local Variable baz | in_class.py:16:19:16:21 | baz |
25+
| in_class.py:5:1:5:22 | Class MyClass | Local Variable baz | in_class.py:28:15:28:17 | baz |
26+
| in_class.py:5:1:5:22 | Class MyClass | Local Variable ex | in_class.py:28:5:28:6 | ex |
27+
| in_class.py:5:1:5:22 | Class MyClass | Local Variable foo | in_class.py:8:9:8:11 | foo |
28+
| in_class.py:5:1:5:22 | Class MyClass | Local Variable foo | in_class.py:13:15:13:17 | foo |
29+
| in_class.py:5:1:5:22 | Class MyClass | Local Variable func | in_class.py:24:9:24:12 | func |
30+
| in_class.py:5:1:5:22 | Class MyClass | Local Variable func | in_class.py:28:10:28:13 | func |
31+
| in_class.py:5:1:5:22 | Class MyClass | Local Variable use | in_class.py:11:9:11:11 | use |
32+
| in_class.py:8:5:8:18 | Function foo | Local Variable self | in_class.py:8:13:8:16 | self |
33+
| in_class.py:11:5:11:18 | Function use | Local Variable self | in_class.py:11:13:11:16 | self |
34+
| in_class.py:11:5:11:18 | Function use | Local Variable self | in_class.py:21:15:21:18 | self |
35+
| in_class.py:11:5:11:18 | Function use | Local Variable self | in_class.py:22:15:22:18 | self |
36+
| in_class.py:24:5:24:18 | Function func | Local Variable arg | in_class.py:24:14:24:16 | arg |
37+
| in_class.py:24:5:24:18 | Function func | Local Variable arg | in_class.py:25:27:25:29 | arg |
138
| test.py:0:0:0:0 | Module test | Global Variable C | test.py:30:7:30:7 | C |
239
| test.py:0:0:0:0 | Module test | Global Variable base | test.py:30:9:30:12 | base |
340
| test.py:0:0:0:0 | Module test | Global Variable func0 | test.py:5:5:5:9 | func0 |

python/ql/test/library-tests/variables/scopes/free.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
| Local Variable baz | in_class.py:5:1:5:22 | Class MyClass | in_class.py:11:5:11:18 | Function use |
2+
| Local Variable foo | in_class.py:5:1:5:22 | Class MyClass | in_class.py:11:5:11:18 | Function use |
13
| Local Variable local2 | test.py:15:1:15:12 | Function func2 | test.py:17:5:17:23 | Function inner1 |
24
| Local Variable local4 | test.py:22:1:22:26 | Function func3 | test.py:24:5:24:22 | Function inner_outer |
35
| Local Variable local4 | test.py:22:1:22:26 | Function func3 | test.py:25:9:25:27 | Function inner2 |

python/ql/test/library-tests/variables/scopes/globals.expected

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
| Global Variable C | test.py:0:0:0:0 | Module test |
2+
| Global Variable MyClass | in_class.py:0:0:0:0 | Module in_class |
3+
| Global Variable NameError | in_class.py:0:0:0:0 | Module in_class |
4+
| Global Variable __name__ | in_class.py:0:0:0:0 | Module in_class |
25
| Global Variable __name__ | test.py:0:0:0:0 | Module test |
6+
| Global Variable __package__ | in_class.py:0:0:0:0 | Module in_class |
37
| Global Variable __package__ | test.py:0:0:0:0 | Module test |
8+
| Global Variable bar | in_class.py:0:0:0:0 | Module in_class |
49
| Global Variable base | test.py:0:0:0:0 | Module test |
10+
| Global Variable foo | in_class.py:0:0:0:0 | Module in_class |
511
| Global Variable func0 | test.py:0:0:0:0 | Module test |
612
| Global Variable func1 | test.py:0:0:0:0 | Module test |
713
| Global Variable func2 | test.py:0:0:0:0 | Module test |
@@ -12,6 +18,9 @@
1218
| Global Variable global0 | test.py:0:0:0:0 | Module test |
1319
| Global Variable global1 | test.py:0:0:0:0 | Module test |
1420
| Global Variable global_local | test.py:0:0:0:0 | Module test |
21+
| Global Variable mc | in_class.py:0:0:0:0 | Module in_class |
22+
| Global Variable object | in_class.py:0:0:0:0 | Module in_class |
23+
| Global Variable print | in_class.py:0:0:0:0 | Module in_class |
1524
| Global Variable range | test.py:0:0:0:0 | Module test |
1625
| Global Variable seq | test.py:0:0:0:0 | Module test |
1726
| Global Variable use_in_loop | test.py:0:0:0:0 | Module test |
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
foo = "foo"
2+
bar = "bar"
3+
4+
5+
class MyClass(object):
6+
baz = "baz"
7+
8+
def foo(self):
9+
print("foo()")
10+
11+
def use(self):
12+
print("! use()")
13+
print(foo)
14+
print(bar)
15+
try:
16+
print(baz)
17+
except NameError:
18+
pass
19+
20+
print("=== access on self ===")
21+
print(self.foo)
22+
print(self.baz)
23+
24+
def func(arg):
25+
print("! func()", arg)
26+
return 42
27+
28+
ex = func(baz)
29+
30+
mc = MyClass()
31+
print()
32+
mc.use()
33+
34+
print("\n! mc.ex")
35+
print(mc.ex)

python/ql/test/library-tests/variables/scopes/locals.expected

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
| Local Variable .0 | test.py:48:12:48:29 | Function listcomp | fast |
33
| Local Variable .0 | test.py:52:5:52:25 | Function listcomp | fast |
44
| Local Variable Local | test.py:38:1:38:18 | Function func4 | fast |
5+
| Local Variable arg | in_class.py:24:5:24:18 | Function func | fast |
6+
| Local Variable baz | in_class.py:5:1:5:22 | Class MyClass | name |
57
| Local Variable class_local | test.py:30:1:30:14 | Class C | name |
8+
| Local Variable ex | in_class.py:5:1:5:22 | Class MyClass | name |
9+
| Local Variable foo | in_class.py:5:1:5:22 | Class MyClass | name |
10+
| Local Variable func | in_class.py:5:1:5:22 | Class MyClass | name |
611
| Local Variable inner1 | test.py:15:1:15:12 | Function func2 | fast |
712
| Local Variable inner2 | test.py:24:5:24:22 | Function inner_outer | fast |
813
| Local Variable inner_outer | test.py:22:1:22:26 | Function func3 | fast |
@@ -22,10 +27,13 @@
2227
| Local Variable param4 | test.py:22:1:22:26 | Function func3 | fast |
2328
| Local Variable param5 | test.py:22:1:22:26 | Function func3 | fast |
2429
| Local Variable param6 | test.py:38:1:38:18 | Function func4 | fast |
30+
| Local Variable self | in_class.py:8:5:8:18 | Function foo | fast |
31+
| Local Variable self | in_class.py:11:5:11:18 | Function use | fast |
2532
| Local Variable self | test.py:34:5:34:19 | Function meth | fast |
2633
| Local Variable self | test.py:40:9:40:29 | Function meth_inner | fast |
2734
| Local Variable seq | test.py:44:1:44:15 | Function func5 | fast |
2835
| Local Variable seq | test.py:51:1:51:21 | Function use_in_loop | fast |
36+
| Local Variable use | in_class.py:5:1:5:22 | Class MyClass | name |
2937
| Local Variable v | test.py:51:1:51:21 | Function use_in_loop | fast |
3038
| Local Variable v | test.py:52:5:52:25 | Function listcomp | fast |
3139
| Local Variable x | test.py:45:12:45:27 | Function listcomp | fast |

python/ql/test/library-tests/variables/scopes/lookup.expected

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
| in_class.py:5:15:5:20 | ControlFlowNode for object | global |
2+
| in_class.py:9:9:9:13 | ControlFlowNode for print | global |
3+
| in_class.py:12:9:12:13 | ControlFlowNode for print | global |
4+
| in_class.py:13:9:13:13 | ControlFlowNode for print | global |
5+
| in_class.py:13:15:13:17 | ControlFlowNode for foo | global |
6+
| in_class.py:14:9:14:13 | ControlFlowNode for print | global |
7+
| in_class.py:14:15:14:17 | ControlFlowNode for bar | global |
8+
| in_class.py:16:13:16:17 | ControlFlowNode for print | global |
9+
| in_class.py:16:19:16:21 | ControlFlowNode for baz | global |
10+
| in_class.py:17:16:17:24 | ControlFlowNode for NameError | global |
11+
| in_class.py:20:9:20:13 | ControlFlowNode for print | global |
12+
| in_class.py:21:9:21:13 | ControlFlowNode for print | global |
13+
| in_class.py:21:15:21:18 | ControlFlowNode for self | local |
14+
| in_class.py:22:9:22:13 | ControlFlowNode for print | global |
15+
| in_class.py:22:15:22:18 | ControlFlowNode for self | local |
16+
| in_class.py:25:9:25:13 | ControlFlowNode for print | global |
17+
| in_class.py:25:27:25:29 | ControlFlowNode for arg | local |
18+
| in_class.py:28:10:28:13 | ControlFlowNode for func | local |
19+
| in_class.py:28:15:28:17 | ControlFlowNode for baz | local |
20+
| in_class.py:30:6:30:12 | ControlFlowNode for MyClass | global |
21+
| in_class.py:31:1:31:5 | ControlFlowNode for print | global |
22+
| in_class.py:32:1:32:2 | ControlFlowNode for mc | global |
23+
| in_class.py:34:1:34:5 | ControlFlowNode for print | global |
24+
| in_class.py:35:1:35:5 | ControlFlowNode for print | global |
25+
| in_class.py:35:7:35:8 | ControlFlowNode for mc | global |
126
| test.py:6:12:6:17 | ControlFlowNode for param0 | local |
227
| test.py:6:21:6:26 | ControlFlowNode for param1 | local |
328
| test.py:12:5:12:16 | ControlFlowNode for global_local | global |

python/ql/test/library-tests/variables/scopes/scopes.expected

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
| Global Variable C | test.py:0:0:0:0 | Module test |
2+
| Global Variable MyClass | in_class.py:0:0:0:0 | Module in_class |
3+
| Global Variable NameError | in_class.py:0:0:0:0 | Module in_class |
4+
| Global Variable __name__ | in_class.py:0:0:0:0 | Module in_class |
25
| Global Variable __name__ | test.py:0:0:0:0 | Module test |
6+
| Global Variable __package__ | in_class.py:0:0:0:0 | Module in_class |
37
| Global Variable __package__ | test.py:0:0:0:0 | Module test |
8+
| Global Variable bar | in_class.py:0:0:0:0 | Module in_class |
49
| Global Variable base | test.py:0:0:0:0 | Module test |
10+
| Global Variable foo | in_class.py:0:0:0:0 | Module in_class |
511
| Global Variable func0 | test.py:0:0:0:0 | Module test |
612
| Global Variable func1 | test.py:0:0:0:0 | Module test |
713
| Global Variable func2 | test.py:0:0:0:0 | Module test |
@@ -12,14 +18,22 @@
1218
| Global Variable global0 | test.py:0:0:0:0 | Module test |
1319
| Global Variable global1 | test.py:0:0:0:0 | Module test |
1420
| Global Variable global_local | test.py:0:0:0:0 | Module test |
21+
| Global Variable mc | in_class.py:0:0:0:0 | Module in_class |
22+
| Global Variable object | in_class.py:0:0:0:0 | Module in_class |
23+
| Global Variable print | in_class.py:0:0:0:0 | Module in_class |
1524
| Global Variable range | test.py:0:0:0:0 | Module test |
1625
| Global Variable seq | test.py:0:0:0:0 | Module test |
1726
| Global Variable use_in_loop | test.py:0:0:0:0 | Module test |
1827
| Local Variable .0 | test.py:45:12:45:27 | Function listcomp |
1928
| Local Variable .0 | test.py:48:12:48:29 | Function listcomp |
2029
| Local Variable .0 | test.py:52:5:52:25 | Function listcomp |
2130
| Local Variable Local | test.py:38:1:38:18 | Function func4 |
31+
| Local Variable arg | in_class.py:24:5:24:18 | Function func |
32+
| Local Variable baz | in_class.py:5:1:5:22 | Class MyClass |
2233
| Local Variable class_local | test.py:30:1:30:14 | Class C |
34+
| Local Variable ex | in_class.py:5:1:5:22 | Class MyClass |
35+
| Local Variable foo | in_class.py:5:1:5:22 | Class MyClass |
36+
| Local Variable func | in_class.py:5:1:5:22 | Class MyClass |
2337
| Local Variable inner1 | test.py:15:1:15:12 | Function func2 |
2438
| Local Variable inner2 | test.py:24:5:24:22 | Function inner_outer |
2539
| Local Variable inner_outer | test.py:22:1:22:26 | Function func3 |
@@ -39,10 +53,13 @@
3953
| Local Variable param4 | test.py:22:1:22:26 | Function func3 |
4054
| Local Variable param5 | test.py:22:1:22:26 | Function func3 |
4155
| Local Variable param6 | test.py:38:1:38:18 | Function func4 |
56+
| Local Variable self | in_class.py:8:5:8:18 | Function foo |
57+
| Local Variable self | in_class.py:11:5:11:18 | Function use |
4258
| Local Variable self | test.py:34:5:34:19 | Function meth |
4359
| Local Variable self | test.py:40:9:40:29 | Function meth_inner |
4460
| Local Variable seq | test.py:44:1:44:15 | Function func5 |
4561
| Local Variable seq | test.py:51:1:51:21 | Function use_in_loop |
62+
| Local Variable use | in_class.py:5:1:5:22 | Class MyClass |
4663
| Local Variable v | test.py:51:1:51:21 | Function use_in_loop |
4764
| Local Variable v | test.py:52:5:52:25 | Function listcomp |
4865
| Local Variable x | test.py:45:12:45:27 | Function listcomp |

0 commit comments

Comments
 (0)