Skip to content

Commit 3bb7e28

Browse files
authored
Merge pull request #10176 from RasmusWL/import-problem
Python: Add testcase for import problem
2 parents ff731f1 + 0728ece commit 3bb7e28

16 files changed

+95
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
A testcase observed in real code, where mixing `from .this import that` with `from .other import *` (in that order) causes import resolution to not work properly.
2+
3+
This needs to be in a separate folder, since using relative imports requires a valid top-level package. We emulate real extractor behavior using `-R` extractor option.
4+
5+
From this directory, you can run the code with `python -m pkg.use`.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
| pkg/alias_only_direct.py:0:0:0:0 | Module pkg.alias_only_direct | pkg/alias_only_direct.py:1:22:1:24 | GSSA Variable foo | use to normal exit |
2+
| pkg/alias_problem.py:0:0:0:0 | Module pkg.alias_problem | pkg/alias_problem.py:1:22:1:24 | GSSA Variable foo | no use to normal exit |
3+
| pkg/alias_problem.py:0:0:0:0 | Module pkg.alias_problem | pkg/alias_problem.py:2:1:2:20 | GSSA Variable foo | use to normal exit |
4+
| pkg/alias_problem_fixed.py:0:0:0:0 | Module pkg.alias_problem_fixed | pkg/alias_problem_fixed.py:0:0:0:0 | GSSA Variable foo | no use to normal exit |
5+
| pkg/alias_problem_fixed.py:0:0:0:0 | Module pkg.alias_problem_fixed | pkg/alias_problem_fixed.py:3:22:3:24 | GSSA Variable foo | use to normal exit |
6+
| pkg/problem_absolute_import.py:0:0:0:0 | Module pkg.problem_absolute_import | pkg/problem_absolute_import.py:1:25:1:27 | GSSA Variable foo | no use to normal exit |
7+
| pkg/problem_absolute_import.py:0:0:0:0 | Module pkg.problem_absolute_import | pkg/problem_absolute_import.py:2:1:2:23 | GSSA Variable foo | use to normal exit |
8+
| pkg/works_absolute_import.py:0:0:0:0 | Module pkg.works_absolute_import | pkg/works_absolute_import.py:0:0:0:0 | GSSA Variable foo | no use to normal exit |
9+
| pkg/works_absolute_import.py:0:0:0:0 | Module pkg.works_absolute_import | pkg/works_absolute_import.py:2:25:2:27 | GSSA Variable foo | use to normal exit |
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import python
2+
3+
// looking at `module_export` predicate in DataFlowPrivate, the core of the problem is
4+
// that in alias_problem.py, the direct import of `foo` does not flow to a normal exit of
5+
// the module. Instead there is a second variable foo coming from `from .other import*` that
6+
// goes to the normal exit of the module.
7+
from Module m, EssaVariable v, string useToNormalExit
8+
where
9+
m = v.getScope().getEnclosingModule() and
10+
not m.getName() in ["pkg.use", "pkg.foo_def"] and
11+
v.getName() = "foo" and
12+
if v.getAUse() = m.getANormalExit()
13+
then useToNormalExit = "use to normal exit"
14+
else useToNormalExit = "no use to normal exit"
15+
select m, v, useToNormalExit
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle-extractor-options: --max-import-depth=1 -R ./pkg/

python/ql/test/experimental/dataflow/typetracking_imports/pkg/__init__.py

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .foo_def import foo # $ tracked
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .foo_def import foo # $ tracked
2+
from .other import *
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# this ordering makes the problem go away
2+
from .other import *
3+
from .foo_def import foo # $ tracked
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .foo_def import *
2+
from .other import *
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# apparently adding the assignment makes type-tracker unhappy, so we add this eval so
2+
# it's possible to run the example and see that everything works
3+
exec("tracked = 'tracked'")
4+
foo = tracked # $ tracked
5+
print(foo) # $ tracked
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bar = "bar-text"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from pkg.foo_def import foo # $ tracked
2+
from pkg.other import *
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
def test_direct_import():
2+
from .foo_def import foo # $ tracked
3+
print(foo) # $ tracked
4+
5+
test_direct_import()
6+
7+
8+
def test_alias_problem():
9+
from .alias_problem import foo # $ MISSING: tracked
10+
print(foo) # $ MISSING: tracked
11+
12+
test_alias_problem()
13+
14+
15+
def test_alias_problem_fixed():
16+
from .alias_problem_fixed import foo # $ tracked
17+
print(foo) # $ tracked
18+
19+
test_alias_problem_fixed()
20+
21+
22+
def test_alias_star():
23+
from .alias_star import foo # $ tracked
24+
print(foo) # $ tracked
25+
26+
test_alias_star()
27+
28+
29+
def test_alias_only_direct():
30+
from .alias_only_direct import foo # $ tracked
31+
print(foo) # $ tracked
32+
33+
test_alias_only_direct()
34+
35+
36+
def test_problem_absolute_import():
37+
from pkg.problem_absolute_import import foo # $ MISSING: tracked
38+
print(foo) # $ MISSING: tracked
39+
40+
test_problem_absolute_import()
41+
42+
def test_works_absolute_import():
43+
from pkg.works_absolute_import import foo # $ tracked
44+
print(foo) # $ tracked
45+
46+
test_works_absolute_import()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from pkg.other import *
2+
from pkg.foo_def import foo # $ tracked

python/ql/test/experimental/dataflow/typetracking_imports/tracked.expected

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../typetracking/tracked.ql

0 commit comments

Comments
 (0)