Skip to content

Commit d67577e

Browse files
committed
Python: Modernise import related queries
Except for Metrics/Dependencies/ExternalDependenciesSourceLinks.ql, since it is rather tricky :D
1 parent 647b9cd commit d67577e

File tree

6 files changed

+23
-18
lines changed

6 files changed

+23
-18
lines changed

python/ql/src/Imports/FromImportOfMutableAttribute.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414
import python
1515
import semmle.python.filters.Tests
1616

17-
from ImportMember im, ModuleObject m, AttrNode store_attr, string name
17+
from ImportMember im, ModuleValue m, AttrNode store_attr, string name
1818
where
19-
im.getModule().(ImportExpr).getImportedModuleName() = m.getName() and
19+
m.importedAs(im.getModule().(ImportExpr).getImportedModuleName()) and
2020
im.getName() = name and
2121
/* Modification must be in a function, so it can occur during lifetime of the import value */
2222
store_attr.getScope() instanceof Function and
2323
/* variable resulting from import must have a long lifetime */
2424
not im.getScope() instanceof Function and
2525
store_attr.isStore() and
26-
store_attr.getObject(name).refersTo(m) and
26+
store_attr.getObject(name).pointsTo(m) and
2727
/* Import not in same module as modification. */
2828
not im.getEnclosingModule() = store_attr.getScope().getEnclosingModule() and
2929
/* Modification is not in a test */

python/ql/src/Imports/ModuleImportsItself.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212

1313
import python
1414

15-
predicate modules_imports_itself(Import i, ModuleObject m) {
16-
i.getEnclosingModule() = m.getModule() and
15+
predicate modules_imports_itself(Import i, ModuleValue m) {
16+
i.getEnclosingModule() = m.getScope() and
1717
m.importedAs(i.getAnImportedModuleName())
1818
}
1919

20-
from Import i, ModuleObject m
20+
from Import i, ModuleValue m
2121
where modules_imports_itself(i, m)
2222
select i, "The module '" + m.getName() + "' imports itself."

python/ql/src/Imports/UnintentionalImport.ql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@
1313

1414
import python
1515

16-
predicate import_star(ImportStar imp, ModuleObject exporter) {
16+
predicate import_star(ImportStar imp, ModuleValue exporter) {
1717
exporter.importedAs(imp.getImportedModuleName())
1818
}
1919

20-
predicate all_defined(ModuleObject exporter) {
21-
exporter.isC()
20+
predicate all_defined(ModuleValue exporter) {
21+
exporter.isBuiltin()
2222
or
23-
exporter.getModule().(ImportTimeScope).definesName("__all__")
23+
exporter.getScope().(ImportTimeScope).definesName("__all__")
2424
or
25-
exporter.getModule().getInitModule().(ImportTimeScope).definesName("__all__")
25+
exporter.getScope().getInitModule().(ImportTimeScope).definesName("__all__")
2626
}
2727

2828

29-
from ImportStar imp, ModuleObject exporter
29+
from ImportStar imp, ModuleValue exporter
3030
where import_star(imp, exporter) and not all_defined(exporter)
3131
select imp, "Import pollutes the enclosing namespace, as the imported module $@ does not define '__all__'.",
3232
exporter, exporter.getName()

python/ql/src/Metrics/DirectImports.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
*/
1212
import python
1313

14-
from ModuleObject m, int n
15-
where n = count(ModuleObject imp | imp = m.getAnImportedModule())
16-
select m.getModule(), n
14+
from ModuleValue m, int n
15+
where n = count(ModuleValue imp | imp = m.getAnImportedModule())
16+
select m.getScope(), n

python/ql/src/Metrics/TransitiveImports.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
*/
1212
import python
1313

14-
from ModuleObject m, int n
15-
where n = count(ModuleObject imp | imp = m.getAnImportedModule+() and imp != m)
16-
select m.getModule(), n
14+
from ModuleValue m, int n
15+
where n = count(ModuleValue imp | imp = m.getAnImportedModule+() and imp != m)
16+
select m.getScope(), n

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ class ModuleValue extends Value {
167167
this.(ModuleObjectInternal).hasCompleteExportInfo()
168168
}
169169

170+
/** Get a module that this module imports */
171+
ModuleValue getAnImportedModule() {
172+
result.importedAs(this.getScope().getAnImportedModuleName())
173+
}
174+
170175
}
171176

172177
module Module {

0 commit comments

Comments
 (0)