-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathUndefinedExport.ql
More file actions
52 lines (48 loc) · 1.79 KB
/
UndefinedExport.ql
File metadata and controls
52 lines (48 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* @name Explicit export is not defined
* @description Including an undefined attribute in __all__ causes an exception when
* the module is imported using '*'
* @kind problem
* @tags reliability
* maintainability
* @problem.severity error
* @sub-severity low
* @precision high
* @id py/undefined-export
*/
import python
/** Whether name is declared in the __all__ list of this module */
predicate declaredInAll(Module m, StrConst name)
{
exists(Assign a, GlobalVariable all |
a.defines(all) and a.getScope() = m and
all.getId() = "__all__" and ((List)a.getValue()).getAnElt() = name
)
}
predicate mutates_globals(PythonModuleObject m) {
exists(CallNode globals |
globals = theGlobalsFunction().(FunctionObject).getACall() and
globals.getScope() = m.getModule() |
exists(AttrNode attr | attr.getObject() = globals)
or
exists(SubscriptNode sub | sub.getValue() = globals and sub.isStore())
)
or
exists(Object enum_convert |
enum_convert.hasLongName("enum.Enum._convert") and
exists(CallNode call |
call.getScope() = m.getModule()
|
enum_convert.(FunctionObject).getACall() = call or
call.getFunction().refersTo(enum_convert)
)
)
}
from PythonModuleObject m, StrConst name, string exported_name
where declaredInAll(m.getModule(), name) and
exported_name = name.strValue() and
not m.hasAttribute(exported_name) and
not (m.getShortName() = "__init__" and exists(m.getPackage().getModule().getSubModule(exported_name))) and
not exists(ImportStarNode imp | imp.getEnclosingModule() = m.getModule() | not imp.getModule().refersTo(_)) and
not mutates_globals(m)
select name, "The name '" + exported_name + "' is exported by __all__ but is not defined."