Python: Modernise import related queries

Except for Metrics/Dependencies/ExternalDependenciesSourceLinks.ql, since it is
rather tricky :D
This commit is contained in:
Rasmus Wriedt Larsen
2020-01-27 15:48:44 +01:00
parent 647b9cdcb0
commit d67577e66c
6 changed files with 23 additions and 18 deletions

View File

@@ -14,16 +14,16 @@
import python
import semmle.python.filters.Tests
from ImportMember im, ModuleObject m, AttrNode store_attr, string name
from ImportMember im, ModuleValue m, AttrNode store_attr, string name
where
im.getModule().(ImportExpr).getImportedModuleName() = m.getName() and
m.importedAs(im.getModule().(ImportExpr).getImportedModuleName()) and
im.getName() = name and
/* Modification must be in a function, so it can occur during lifetime of the import value */
store_attr.getScope() instanceof Function and
/* variable resulting from import must have a long lifetime */
not im.getScope() instanceof Function and
store_attr.isStore() and
store_attr.getObject(name).refersTo(m) and
store_attr.getObject(name).pointsTo(m) and
/* Import not in same module as modification. */
not im.getEnclosingModule() = store_attr.getScope().getEnclosingModule() and
/* Modification is not in a test */

View File

@@ -12,11 +12,11 @@
import python
predicate modules_imports_itself(Import i, ModuleObject m) {
i.getEnclosingModule() = m.getModule() and
predicate modules_imports_itself(Import i, ModuleValue m) {
i.getEnclosingModule() = m.getScope() and
m.importedAs(i.getAnImportedModuleName())
}
from Import i, ModuleObject m
from Import i, ModuleValue m
where modules_imports_itself(i, m)
select i, "The module '" + m.getName() + "' imports itself."

View File

@@ -13,20 +13,20 @@
import python
predicate import_star(ImportStar imp, ModuleObject exporter) {
predicate import_star(ImportStar imp, ModuleValue exporter) {
exporter.importedAs(imp.getImportedModuleName())
}
predicate all_defined(ModuleObject exporter) {
exporter.isC()
predicate all_defined(ModuleValue exporter) {
exporter.isBuiltin()
or
exporter.getModule().(ImportTimeScope).definesName("__all__")
exporter.getScope().(ImportTimeScope).definesName("__all__")
or
exporter.getModule().getInitModule().(ImportTimeScope).definesName("__all__")
exporter.getScope().getInitModule().(ImportTimeScope).definesName("__all__")
}
from ImportStar imp, ModuleObject exporter
from ImportStar imp, ModuleValue exporter
where import_star(imp, exporter) and not all_defined(exporter)
select imp, "Import pollutes the enclosing namespace, as the imported module $@ does not define '__all__'.",
exporter, exporter.getName()

View File

@@ -11,6 +11,6 @@
*/
import python
from ModuleObject m, int n
where n = count(ModuleObject imp | imp = m.getAnImportedModule())
select m.getModule(), n
from ModuleValue m, int n
where n = count(ModuleValue imp | imp = m.getAnImportedModule())
select m.getScope(), n

View File

@@ -11,6 +11,6 @@
*/
import python
from ModuleObject m, int n
where n = count(ModuleObject imp | imp = m.getAnImportedModule+() and imp != m)
select m.getModule(), n
from ModuleValue m, int n
where n = count(ModuleValue imp | imp = m.getAnImportedModule+() and imp != m)
select m.getScope(), n

View File

@@ -167,6 +167,11 @@ class ModuleValue extends Value {
this.(ModuleObjectInternal).hasCompleteExportInfo()
}
/** Get a module that this module imports */
ModuleValue getAnImportedModule() {
result.importedAs(this.getScope().getAnImportedModuleName())
}
}
module Module {