mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +01:00
Python: Autoformat.
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
import python
|
||||
|
||||
predicate is_import_time(Stmt s) {
|
||||
not s.getScope+() instanceof Function
|
||||
}
|
||||
predicate is_import_time(Stmt s) { not s.getScope+() instanceof Function }
|
||||
|
||||
ModuleValue module_imported_by(ModuleValue m) {
|
||||
exists(Stmt imp |
|
||||
@@ -16,12 +14,12 @@ ModuleValue module_imported_by(ModuleValue m) {
|
||||
/** Is there a circular import of 'm1' beginning with 'm2'? */
|
||||
predicate circular_import(ModuleValue m1, ModuleValue m2) {
|
||||
m1 != m2 and
|
||||
m2 = module_imported_by(m1) and m1 = module_imported_by+(m2)
|
||||
m2 = module_imported_by(m1) and
|
||||
m1 = module_imported_by+(m2)
|
||||
}
|
||||
|
||||
ModuleValue stmt_imports(ImportingStmt s) {
|
||||
exists(string name |
|
||||
result.importedAs(name) and not name = "__main__" |
|
||||
exists(string name | result.importedAs(name) and not name = "__main__" |
|
||||
name = s.getAnImportedModuleName() and
|
||||
s.getASubExpression().pointsTo(result)
|
||||
)
|
||||
@@ -45,8 +43,8 @@ predicate import_time_transitive_import(ModuleValue base, Stmt imp, ModuleValue
|
||||
(
|
||||
import_time_imported_module(base, last, imp)
|
||||
or
|
||||
exists(ModuleValue mid |
|
||||
import_time_transitive_import(base, imp, mid) and
|
||||
exists(ModuleValue mid |
|
||||
import_time_transitive_import(base, imp, mid) and
|
||||
import_time_imported_module(mid, last, _)
|
||||
)
|
||||
) and
|
||||
@@ -58,11 +56,11 @@ predicate import_time_transitive_import(ModuleValue base, Stmt imp, ModuleValue
|
||||
* Returns import-time usages of module 'm' in module 'enclosing'
|
||||
*/
|
||||
predicate import_time_module_use(ModuleValue m, ModuleValue enclosing, Expr use, string attr) {
|
||||
exists(Expr mod |
|
||||
exists(Expr mod |
|
||||
use.getEnclosingModule() = enclosing.getScope() and
|
||||
not use.getScope+() instanceof Function
|
||||
and mod.pointsTo(m)
|
||||
|
|
||||
not use.getScope+() instanceof Function and
|
||||
mod.pointsTo(m)
|
||||
|
|
||||
// either 'M.foo'
|
||||
use.(Attribute).getObject() = mod and use.(Attribute).getName() = attr
|
||||
or
|
||||
@@ -71,20 +69,24 @@ predicate import_time_module_use(ModuleValue m, ModuleValue enclosing, Expr use,
|
||||
)
|
||||
}
|
||||
|
||||
/** Whether importing module 'first' before importing module 'other' will fail at runtime, due to an
|
||||
AttributeError at 'use' (in module 'other') caused by 'first.attr' not being defined as its definition can
|
||||
occur after the import 'other' in 'first'.
|
||||
*/
|
||||
predicate failing_import_due_to_cycle(ModuleValue first, ModuleValue other, Stmt imp,
|
||||
ControlFlowNode defn, Expr use, string attr) {
|
||||
/**
|
||||
* Whether importing module 'first' before importing module 'other' will fail at runtime, due to an
|
||||
* AttributeError at 'use' (in module 'other') caused by 'first.attr' not being defined as its definition can
|
||||
* occur after the import 'other' in 'first'.
|
||||
*/
|
||||
predicate failing_import_due_to_cycle(
|
||||
ModuleValue first, ModuleValue other, Stmt imp, ControlFlowNode defn, Expr use, string attr
|
||||
) {
|
||||
import_time_imported_module(other, first, _) and
|
||||
import_time_transitive_import(first, imp, other) and
|
||||
import_time_module_use(first, other, use, attr) and
|
||||
exists(ImportTimeScope n, SsaVariable v |
|
||||
defn = v.getDefinition() and
|
||||
n = first.getScope() and v.getVariable().getScope() = n and v.getId() = attr |
|
||||
n = first.getScope() and
|
||||
v.getVariable().getScope() = n and
|
||||
v.getId() = attr
|
||||
|
|
||||
not defn.strictlyDominates(imp.getAnEntryNode())
|
||||
)
|
||||
and not exists(If i | i.isNameEqMain() and i.contains(use))
|
||||
) and
|
||||
not exists(If i | i.isNameEqMain() and i.contains(use))
|
||||
}
|
||||
|
||||
|
||||
@@ -15,13 +15,12 @@ import python
|
||||
import Cyclic
|
||||
|
||||
from ModuleValue m1, ModuleValue m2, Stmt imp
|
||||
where
|
||||
imp.getEnclosingModule() = m1.getScope()
|
||||
and stmt_imports(imp) = m2
|
||||
and circular_import(m1, m2)
|
||||
and m1 != m2
|
||||
// this query finds all cyclic imports that are *not* flagged by ModuleLevelCyclicImport
|
||||
and not failing_import_due_to_cycle(m2, m1, _, _, _, _)
|
||||
and not exists(If i | i.isNameEqMain() and i.contains(imp))
|
||||
where
|
||||
imp.getEnclosingModule() = m1.getScope() and
|
||||
stmt_imports(imp) = m2 and
|
||||
circular_import(m1, m2) and
|
||||
m1 != m2 and
|
||||
// this query finds all cyclic imports that are *not* flagged by ModuleLevelCyclicImport
|
||||
not failing_import_due_to_cycle(m2, m1, _, _, _, _) and
|
||||
not exists(If i | i.isNameEqMain() and i.contains(imp))
|
||||
select imp, "Import of module $@ begins an import cycle.", m2, m2.getName()
|
||||
|
||||
|
||||
@@ -21,11 +21,10 @@ import Cyclic
|
||||
// 3. 'foo' is defined in M after the import in M which completes the cycle.
|
||||
// then if we import the 'used' module, we will reach the cyclic import, start importing the 'using'
|
||||
// module, hit the 'use', and then crash due to the imported symbol not having been defined yet
|
||||
|
||||
from ModuleValue m1, Stmt imp, ModuleValue m2, string attr, Expr use, ControlFlowNode defn
|
||||
where failing_import_due_to_cycle(m1, m2, imp, defn, use, attr)
|
||||
select use, "'" + attr + "' may not be defined if module $@ is imported before module $@, " +
|
||||
"as the $@ of " + attr + " occurs after the cyclic $@ of " + m2.getName() + ".",
|
||||
m1, m1.getName(), m2, m2.getName(), defn, "definition", imp, "import"
|
||||
|
||||
|
||||
select use,
|
||||
"'" + attr + "' may not be defined if module $@ is imported before module $@, as the $@ of " +
|
||||
attr + " occurs after the cyclic $@ of " + m2.getName() + ".",
|
||||
// Arguments for the placeholders in the above message:
|
||||
m1, m1.getName(), m2, m2.getName(), defn, "definition", imp, "import"
|
||||
|
||||
Reference in New Issue
Block a user