Skip to content

Commit 4ae3a23

Browse files
authored
Python: Limit absolute imports
Limits the behaviour of github#5614 in two ways: First, we only consider files that are contained in the source archive. This prevents unnecessary computation involving files in e.g. the standard library. Secondly, we ignore any relative imports (e.g. `from .foo import ...`), as these only work inside packages anyway. This fixes an observed performance regression on projects that include `google-cloud-sdk` as part of their source code.
1 parent 4cc8866 commit 4ae3a23

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

python/ql/src/semmle/python/Module.qll

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,15 @@ private string moduleNameFromBase(Container file) {
212212
private predicate transitively_imported_from_entry_point(File file) {
213213
file.getExtension().matches("%py%") and
214214
exists(File importer |
215+
// Only consider files that are in the source archive
216+
exists(importer.getRelativePath()) and
215217
importer.getParent() = file.getParent() and
216-
exists(ImportExpr i | i.getLocation().getFile() = importer and i.getName() = file.getStem())
218+
exists(ImportExpr i |
219+
i.getLocation().getFile() = importer and
220+
i.getName() = file.getStem() and
221+
// Disregard relative imports
222+
i.getLevel() = 0
223+
)
217224
|
218225
importer.isPossibleEntryPoint() or transitively_imported_from_entry_point(importer)
219226
)

0 commit comments

Comments
 (0)