mirror of
https://github.com/github/codeql.git
synced 2026-03-31 12:48:17 +02:00
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:
@@ -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."
|
||||
|
||||
Reference in New Issue
Block a user