Merge pull request #2163 from tausbn/python-undefined-export-fp

Python: Modernise and fix FP in `py/undefined-export`
This commit is contained in:
Rasmus Wriedt Larsen
2019-11-04 13:10:48 +01:00
committed by GitHub
3 changed files with 51 additions and 23 deletions

View File

@@ -14,39 +14,54 @@
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 declaredInAll(Module m, StrConst name) {
exists(Assign a, GlobalVariable all |
a.defines(all) and
a.getScope() = m and
all.getId() = "__all__" and
a.getValue().(List).getAnElt() = name
)
}
predicate mutates_globals(PythonModuleObject m) {
predicate mutates_globals(ModuleValue m) {
exists(CallNode globals |
globals = Object::builtin("globals").(FunctionObject).getACall() and
globals.getScope() = m.getModule() |
globals = Value::named("globals").(FunctionValue).getACall() and
globals.getScope() = m.getScope()
|
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)
exists(Value enum_convert, ClassValue enum_class |
enum_class.getASuperType() = Value::named("enum.Enum") and
enum_convert = enum_class.attr("_convert") and
exists(CallNode call | call.getScope() = m.getScope() |
enum_convert.getACall() = call or
call.getFunction().pointsTo(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."
predicate is_exported_submodule_name(ModuleValue m, string exported_name) {
m.getScope().getShortName() = "__init__" and
exists(m.getScope().getPackage().getSubModule(exported_name))
}
predicate contains_unknown_import_star(ModuleValue m) {
exists(ImportStarNode imp | imp.getEnclosingModule() = m.getScope() |
imp.getModule().pointsTo().isAbsent()
or
not exists(imp.getModule().pointsTo())
)
}
from ModuleValue m, StrConst name, string exported_name
where
declaredInAll(m.getScope(), name) and
exported_name = name.strValue() and
not m.hasAttribute(exported_name) and
not is_exported_submodule_name(m, exported_name) and
not contains_unknown_import_star(m) and
not mutates_globals(m)
select name, "The name '" + exported_name + "' is exported by __all__ but is not defined."

View File

@@ -52,6 +52,13 @@ class Module extends Module_, Scope, AstNode {
result = moduleNameFromFile(this.getPath())
}
/** Gets the short name of the module. For example the short name of module x.y.z is 'z' */
string getShortName() {
result = this.getName().suffix(this.getPackage().getName().length()+1)
or
result = this.getName() and not exists(this.getPackage())
}
/** Gets this module */
override Module getEnclosingModule() {
result = this

View File

@@ -99,6 +99,12 @@ class Value extends TObject {
this.(ObjectInternal).hasAttribute(name)
}
/** Whether this value is absent from the database, but has been inferred to likely exist */
predicate isAbsent() {
this instanceof AbsentModuleObjectInternal
or
this instanceof AbsentModuleAttributeObjectInternal
}
}
/** Class representing modules in the Python program