|
10 | 10 | * @precision medium |
11 | 11 | * @id py/import-of-mutable-attribute |
12 | 12 | */ |
| 13 | + |
13 | 14 | import python |
14 | 15 | import semmle.python.filters.Tests |
15 | 16 |
|
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() |
0 commit comments