Python: Port py/print-during-import

Uses a (perhaps) slightly coarser approximation of what modules are
imported, but it's probably fine.
This commit is contained in:
Taus
2026-02-19 15:06:47 +00:00
parent f4841e1f39
commit 1dcc76996d

View File

@@ -12,7 +12,6 @@
*/
import python
private import LegacyPointsTo
predicate main_eq_name(If i) {
exists(Name n, StringLiteral m, Compare c |
@@ -32,10 +31,19 @@ predicate is_print_stmt(Stmt s) {
)
}
/**
* Holds if module `m` is likely used as a module (imported by another module),
* as opposed to being exclusively used as a script.
*/
predicate is_used_as_module(Module m) {
m.isPackageInit()
or
exists(ImportingStmt i | i.getAnImportedModuleName() = m.getName())
}
from Stmt p
where
is_print_stmt(p) and
// TODO: Need to discuss how we would like to handle ModuleObject.getKind in the glorious future
exists(ModuleValue m | m.getScope() = p.getScope() and m.isUsedAsModule()) and
is_used_as_module(p.getScope()) and
not exists(If i | main_eq_name(i) and i.getASubStatement().getASubStatement*() = p)
select p, "Print statement may execute during import."