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

0 commit comments

Comments
 (0)