Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 82b29b5

Browse files
committed
Python: Recognize shebangs in module usage detection
1 parent 01f5b3d commit 82b29b5

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

python/ql/src/semmle/python/objects/ObjectAPI.qll

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,12 @@ class ModuleValue extends Value {
210210
i.getTest().(Compare).compares(name, op, main) and
211211
name.getId() = "__name__" and main.getText() = "__main__"
212212
)
213-
// TODO: Add she-bang handling
213+
or
214+
exists(Comment c |
215+
c.getLocation().getFile() = this.getPath() and
216+
c.getLocation().getStartLine() = 1 and
217+
c.getText().regexpMatch("^#!/.*python(2|3)?[ \\\\t]*$")
218+
)
214219
)
215220
}
216221
}

python/ql/test/library-tests/modules/usage/ModuleUsage.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
| file://:0:0:0:0 | Module sys | isUsedAsModule |
33
| imported.py:0:0:0:0 | Module imported | isUsedAsModule |
44
| main.py:0:0:0:0 | Module main | isUsedAsScript |
5-
| myscript.py:0:0:0:0 | Script myscript | <UNKNOWN> |
5+
| myscript.py:0:0:0:0 | Script myscript | isUsedAsScript |
66
| script:0:0:0:0 | Script script | isUsedAsScript |

0 commit comments

Comments
 (0)