Skip to content

Commit f1004b1

Browse files
authored
Merge pull request #2147 from tausbn/python-cyclic-import-package-fp
Python: Fix cyclic import FP relating to packages.
2 parents 89896c0 + 32de65c commit f1004b1

File tree

22 files changed

+29
-1
lines changed

22 files changed

+29
-1
lines changed

python/ql/src/Imports/Cyclic.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ predicate circular_import(ModuleValue m1, ModuleValue m2) {
2121
ModuleValue stmt_imports(ImportingStmt s) {
2222
exists(string name | result.importedAs(name) and not name = "__main__" |
2323
name = s.getAnImportedModuleName() and
24-
s.getASubExpression().pointsTo(result)
24+
s.getASubExpression().pointsTo(result) and
25+
not result.isPackage()
2526
)
2627
}
2728

python/ql/src/semmle/python/objects/ObjectAPI.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ class ModuleValue extends Value {
139139
PointsToInternal::module_imported_as(this, name)
140140
}
141141

142+
/** Whether this module is a package. */
143+
predicate isPackage() {
144+
this instanceof PackageObjectInternal
145+
}
146+
142147
}
143148

144149
module Module {

python/ql/test/query-tests/Imports/cyclic-module-package-fp/false-negative/CyclicImport.expected

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

python/ql/test/query-tests/Imports/cyclic-module-package-fp/false-negative/ModuleLevelCyclicImport.expected

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

python/ql/test/query-tests/Imports/cyclic-module-package-fp/false-negative/bar/__init__.py

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from package import p
2+
foo = 5
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle-extractor-options: -R .
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from bar.foo import foo
2+
from package import baz
3+
p = 1

0 commit comments

Comments
 (0)