Skip to content

Commit 5db6270

Browse files
authored
Merge pull request github#6091 from tausbn/python-exclude-main-py-files
Python: Avoid `__main__.py` files as entry points.
2 parents e05d6e7 + ba6ab8f commit 5db6270

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

python/ql/src/semmle/python/Files.qll

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,15 @@ class File extends Container {
8989
i.getTest().(Compare).compares(name, op, main) and
9090
name.getId() = "__name__" and
9191
main.getText() = "__main__"
92-
)
92+
) and
93+
// Exclude files named `__main__.py`. These are often _not_ meant to be run directly, but
94+
// contain this construct anyway.
95+
//
96+
// Their presence in a package (say, `foo`) means one can execute the package directly using
97+
// `python -m foo` (which will run the `foo/__main__.py` file). Since being an entry point for
98+
// execution means treating imports as absolute, this causes trouble, since when run with
99+
// `python -m`, the interpreter uses the usual package semantics.
100+
not this.getShortName() = "__main__.py"
93101
or
94102
// The file contains a `#!` line referencing the python interpreter
95103
exists(Comment c |

0 commit comments

Comments
 (0)