Skip to content

Commit c5c84a1

Browse files
committed
Python: Autoformat.
1 parent aa16d20 commit c5c84a1

File tree

3 files changed

+37
-37
lines changed

3 files changed

+37
-37
lines changed

python/ql/src/Imports/Cyclic.qll

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import python
22

3-
predicate is_import_time(Stmt s) {
4-
not s.getScope+() instanceof Function
5-
}
3+
predicate is_import_time(Stmt s) { not s.getScope+() instanceof Function }
64

75
ModuleValue module_imported_by(ModuleValue m) {
86
exists(Stmt imp |
@@ -16,12 +14,12 @@ ModuleValue module_imported_by(ModuleValue m) {
1614
/** Is there a circular import of 'm1' beginning with 'm2'? */
1715
predicate circular_import(ModuleValue m1, ModuleValue m2) {
1816
m1 != m2 and
19-
m2 = module_imported_by(m1) and m1 = module_imported_by+(m2)
17+
m2 = module_imported_by(m1) and
18+
m1 = module_imported_by+(m2)
2019
}
2120

2221
ModuleValue stmt_imports(ImportingStmt s) {
23-
exists(string name |
24-
result.importedAs(name) and not name = "__main__" |
22+
exists(string name | result.importedAs(name) and not name = "__main__" |
2523
name = s.getAnImportedModuleName() and
2624
s.getASubExpression().pointsTo(result)
2725
)
@@ -45,8 +43,8 @@ predicate import_time_transitive_import(ModuleValue base, Stmt imp, ModuleValue
4543
(
4644
import_time_imported_module(base, last, imp)
4745
or
48-
exists(ModuleValue mid |
49-
import_time_transitive_import(base, imp, mid) and
46+
exists(ModuleValue mid |
47+
import_time_transitive_import(base, imp, mid) and
5048
import_time_imported_module(mid, last, _)
5149
)
5250
) and
@@ -58,11 +56,11 @@ predicate import_time_transitive_import(ModuleValue base, Stmt imp, ModuleValue
5856
* Returns import-time usages of module 'm' in module 'enclosing'
5957
*/
6058
predicate import_time_module_use(ModuleValue m, ModuleValue enclosing, Expr use, string attr) {
61-
exists(Expr mod |
59+
exists(Expr mod |
6260
use.getEnclosingModule() = enclosing.getScope() and
63-
not use.getScope+() instanceof Function
64-
and mod.pointsTo(m)
65-
|
61+
not use.getScope+() instanceof Function and
62+
mod.pointsTo(m)
63+
|
6664
// either 'M.foo'
6765
use.(Attribute).getObject() = mod and use.(Attribute).getName() = attr
6866
or
@@ -71,20 +69,24 @@ predicate import_time_module_use(ModuleValue m, ModuleValue enclosing, Expr use,
7169
)
7270
}
7371

74-
/** Whether importing module 'first' before importing module 'other' will fail at runtime, due to an
75-
AttributeError at 'use' (in module 'other') caused by 'first.attr' not being defined as its definition can
76-
occur after the import 'other' in 'first'.
77-
*/
78-
predicate failing_import_due_to_cycle(ModuleValue first, ModuleValue other, Stmt imp,
79-
ControlFlowNode defn, Expr use, string attr) {
72+
/**
73+
* Whether importing module 'first' before importing module 'other' will fail at runtime, due to an
74+
* AttributeError at 'use' (in module 'other') caused by 'first.attr' not being defined as its definition can
75+
* occur after the import 'other' in 'first'.
76+
*/
77+
predicate failing_import_due_to_cycle(
78+
ModuleValue first, ModuleValue other, Stmt imp, ControlFlowNode defn, Expr use, string attr
79+
) {
8080
import_time_imported_module(other, first, _) and
8181
import_time_transitive_import(first, imp, other) and
8282
import_time_module_use(first, other, use, attr) and
8383
exists(ImportTimeScope n, SsaVariable v |
8484
defn = v.getDefinition() and
85-
n = first.getScope() and v.getVariable().getScope() = n and v.getId() = attr |
85+
n = first.getScope() and
86+
v.getVariable().getScope() = n and
87+
v.getId() = attr
88+
|
8689
not defn.strictlyDominates(imp.getAnEntryNode())
87-
)
88-
and not exists(If i | i.isNameEqMain() and i.contains(use))
90+
) and
91+
not exists(If i | i.isNameEqMain() and i.contains(use))
8992
}
90-

python/ql/src/Imports/CyclicImport.ql

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@ import python
1515
import Cyclic
1616

1717
from ModuleValue m1, ModuleValue m2, Stmt imp
18-
where
19-
imp.getEnclosingModule() = m1.getScope()
20-
and stmt_imports(imp) = m2
21-
and circular_import(m1, m2)
22-
and m1 != m2
23-
// this query finds all cyclic imports that are *not* flagged by ModuleLevelCyclicImport
24-
and not failing_import_due_to_cycle(m2, m1, _, _, _, _)
25-
and not exists(If i | i.isNameEqMain() and i.contains(imp))
18+
where
19+
imp.getEnclosingModule() = m1.getScope() and
20+
stmt_imports(imp) = m2 and
21+
circular_import(m1, m2) and
22+
m1 != m2 and
23+
// this query finds all cyclic imports that are *not* flagged by ModuleLevelCyclicImport
24+
not failing_import_due_to_cycle(m2, m1, _, _, _, _) and
25+
not exists(If i | i.isNameEqMain() and i.contains(imp))
2626
select imp, "Import of module $@ begins an import cycle.", m2, m2.getName()
27-

python/ql/src/Imports/ModuleLevelCyclicImport.ql

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ import Cyclic
2121
// 3. 'foo' is defined in M after the import in M which completes the cycle.
2222
// then if we import the 'used' module, we will reach the cyclic import, start importing the 'using'
2323
// module, hit the 'use', and then crash due to the imported symbol not having been defined yet
24-
2524
from ModuleValue m1, Stmt imp, ModuleValue m2, string attr, Expr use, ControlFlowNode defn
2625
where failing_import_due_to_cycle(m1, m2, imp, defn, use, attr)
27-
select use, "'" + attr + "' may not be defined if module $@ is imported before module $@, " +
28-
"as the $@ of " + attr + " occurs after the cyclic $@ of " + m2.getName() + ".",
29-
m1, m1.getName(), m2, m2.getName(), defn, "definition", imp, "import"
30-
31-
26+
select use,
27+
"'" + attr + "' may not be defined if module $@ is imported before module $@, as the $@ of " +
28+
attr + " occurs after the cyclic $@ of " + m2.getName() + ".",
29+
// Arguments for the placeholders in the above message:
30+
m1, m1.getName(), m2, m2.getName(), defn, "definition", imp, "import"

0 commit comments

Comments
 (0)