11import 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
75ModuleValue 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'? */
1715predicate 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
2221ModuleValue 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 */
6058predicate 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-
0 commit comments