mirror of
https://github.com/github/codeql.git
synced 2025-12-20 10:46:30 +01:00
Python: Modernise import related queries
Except for Metrics/Dependencies/ExternalDependenciesSourceLinks.ql, since it is rather tricky :D
This commit is contained in:
@@ -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 */
|
||||
|
||||
@@ -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."
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user