Skip to content

Commit e6f5cef

Browse files
committed
Python: Port py/print-during-import
Uses a (perhaps) slightly coarser approximation of what modules are imported, but it's probably fine.
1 parent 8a67917 commit e6f5cef

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

python/ql/src/Statements/TopLevelPrint.ql

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
*/
1313

1414
import python
15-
private import LegacyPointsTo
1615

1716
predicate main_eq_name(If i) {
1817
exists(Name n, StringLiteral m, Compare c |
@@ -32,10 +31,19 @@ predicate is_print_stmt(Stmt s) {
3231
)
3332
}
3433

34+
/**
35+
* Holds if module `m` is likely used as a module (imported by another module),
36+
* as opposed to being exclusively used as a script.
37+
*/
38+
predicate is_used_as_module(Module m) {
39+
m.isPackageInit()
40+
or
41+
exists(ImportingStmt i | i.getAnImportedModuleName() = m.getName())
42+
}
43+
3544
from Stmt p
3645
where
3746
is_print_stmt(p) and
38-
// TODO: Need to discuss how we would like to handle ModuleObject.getKind in the glorious future
39-
exists(ModuleValue m | m.getScope() = p.getScope() and m.isUsedAsModule()) and
47+
is_used_as_module(p.getScope().(Module)) and
4048
not exists(If i | main_eq_name(i) and i.getASubStatement().getASubStatement*() = p)
4149
select p, "Print statement may execute during import."

0 commit comments

Comments
 (0)