1
1
import python
2
2
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 }
6
4
7
5
ModuleValue module_imported_by ( ModuleValue m ) {
8
6
exists ( Stmt imp |
@@ -16,12 +14,12 @@ ModuleValue module_imported_by(ModuleValue m) {
16
14
/** Is there a circular import of 'm1' beginning with 'm2'? */
17
15
predicate circular_import ( ModuleValue m1 , ModuleValue m2 ) {
18
16
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 )
20
19
}
21
20
22
21
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__" |
25
23
name = s .getAnImportedModuleName ( ) and
26
24
s .getASubExpression ( ) .pointsTo ( result )
27
25
)
@@ -45,8 +43,8 @@ predicate import_time_transitive_import(ModuleValue base, Stmt imp, ModuleValue
45
43
(
46
44
import_time_imported_module ( base , last , imp )
47
45
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
50
48
import_time_imported_module ( mid , last , _)
51
49
)
52
50
) and
@@ -58,11 +56,11 @@ predicate import_time_transitive_import(ModuleValue base, Stmt imp, ModuleValue
58
56
* Returns import-time usages of module 'm' in module 'enclosing'
59
57
*/
60
58
predicate import_time_module_use ( ModuleValue m , ModuleValue enclosing , Expr use , string attr ) {
61
- exists ( Expr mod |
59
+ exists ( Expr mod |
62
60
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
+ |
66
64
// either 'M.foo'
67
65
use .( Attribute ) .getObject ( ) = mod and use .( Attribute ) .getName ( ) = attr
68
66
or
@@ -71,20 +69,24 @@ predicate import_time_module_use(ModuleValue m, ModuleValue enclosing, Expr use,
71
69
)
72
70
}
73
71
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
+ ) {
80
80
import_time_imported_module ( other , first , _) and
81
81
import_time_transitive_import ( first , imp , other ) and
82
82
import_time_module_use ( first , other , use , attr ) and
83
83
exists ( ImportTimeScope n , SsaVariable v |
84
84
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
+ |
86
89
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 ) )
89
92
}
90
-
0 commit comments