mirror of
https://github.com/github/codeql.git
synced 2026-04-30 19:26:02 +02:00
Python: Modernise and false positive in py/undefined-export.
This commit is contained in:
@@ -22,10 +22,10 @@ predicate declaredInAll(Module m, StrConst 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.getScope() = m.getScope() |
|
||||
exists(AttrNode attr | attr.getObject() = globals)
|
||||
or
|
||||
exists(SubscriptNode sub | sub.getValue() = globals and sub.isStore())
|
||||
@@ -34,7 +34,7 @@ predicate mutates_globals(PythonModuleObject m) {
|
||||
exists(Object enum_convert |
|
||||
enum_convert.hasLongName("enum.Enum._convert") and
|
||||
exists(CallNode call |
|
||||
call.getScope() = m.getModule()
|
||||
call.getScope() = m.getScope()
|
||||
|
|
||||
enum_convert.(FunctionObject).getACall() = call or
|
||||
call.getFunction().refersTo(enum_convert)
|
||||
@@ -42,11 +42,11 @@ predicate mutates_globals(PythonModuleObject m) {
|
||||
)
|
||||
}
|
||||
|
||||
from PythonModuleObject m, StrConst name, string exported_name
|
||||
where declaredInAll(m.getModule(), name) and
|
||||
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 (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 (m.getScope().getShortName() = "__init__" and exists(m.getScope().getPackage().getSubModule(exported_name))) and
|
||||
not exists(ImportStarNode imp | imp.getEnclosingModule() = m.getScope() | imp.getModule().pointsTo().isAbsent()) and
|
||||
not mutates_globals(m)
|
||||
select name, "The name '" + exported_name + "' is exported by __all__ but is not defined."
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user