Skip to content

Commit b892734

Browse files
authored
Merge pull request #2701 from RasmusWL/python-modernise-metrics
Python: modernise import related queries
2 parents 3158b84 + d67577e commit b892734

File tree

6 files changed

+39
-31
lines changed

6 files changed

+39
-31
lines changed

python/ql/src/Imports/FromImportOfMutableAttribute.ql

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,25 @@
1010
* @precision medium
1111
* @id py/import-of-mutable-attribute
1212
*/
13+
1314
import python
1415
import semmle.python.filters.Tests
1516

16-
from ImportMember im, ModuleObject m, AttrNode store_attr, string name
17-
where im.getModule().(ImportExpr).getImportedModuleName() = m.getName() and
18-
im.getName() = name and
19-
/* Modification must be in a function, so it can occur during lifetime of the import value */
20-
store_attr.getScope() instanceof Function and
21-
/* variable resulting from import must have a long lifetime */
22-
not im.getScope() instanceof Function and
23-
store_attr.isStore() and
24-
store_attr.getObject(name).refersTo(m) and
25-
/* Import not in same module as modification. */
26-
not im.getEnclosingModule() = store_attr.getScope().getEnclosingModule() and
27-
/* Modification is not in a test */
28-
not store_attr.getScope().getScope*() instanceof TestScope
29-
30-
select im, "Importing the value of '" + name + "' from $@ means that any change made to $@ will be not be observed locally.",
31-
m, "module " + m.getName(), store_attr, m.getName() + "." + store_attr.getName()
17+
from ImportMember im, ModuleValue m, AttrNode store_attr, string name
18+
where
19+
m.importedAs(im.getModule().(ImportExpr).getImportedModuleName()) and
20+
im.getName() = name and
21+
/* Modification must be in a function, so it can occur during lifetime of the import value */
22+
store_attr.getScope() instanceof Function and
23+
/* variable resulting from import must have a long lifetime */
24+
not im.getScope() instanceof Function and
25+
store_attr.isStore() and
26+
store_attr.getObject(name).pointsTo(m) and
27+
/* Import not in same module as modification. */
28+
not im.getEnclosingModule() = store_attr.getScope().getEnclosingModule() and
29+
/* Modification is not in a test */
30+
not store_attr.getScope().getScope*() instanceof TestScope
31+
select im,
32+
"Importing the value of '" + name +
33+
"' from $@ means that any change made to $@ will be not be observed locally.", m,
34+
"module " + m.getName(), store_attr, m.getName() + "." + store_attr.getName()

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)