Skip to content

Commit 8a1d1e7

Browse files
committed
Python: Modernise and false positive in py/undefined-export.
1 parent 37291c5 commit 8a1d1e7

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

python/ql/src/Variables/UndefinedExport.ql

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ predicate declaredInAll(Module m, StrConst name)
2222
)
2323
}
2424

25-
predicate mutates_globals(PythonModuleObject m) {
25+
predicate mutates_globals(ModuleValue m) {
2626
exists(CallNode globals |
2727
globals = Object::builtin("globals").(FunctionObject).getACall() and
28-
globals.getScope() = m.getModule() |
28+
globals.getScope() = m.getScope() |
2929
exists(AttrNode attr | attr.getObject() = globals)
3030
or
3131
exists(SubscriptNode sub | sub.getValue() = globals and sub.isStore())
@@ -34,19 +34,19 @@ predicate mutates_globals(PythonModuleObject m) {
3434
exists(Object enum_convert |
3535
enum_convert.hasLongName("enum.Enum._convert") and
3636
exists(CallNode call |
37-
call.getScope() = m.getModule()
37+
call.getScope() = m.getScope()
3838
|
3939
enum_convert.(FunctionObject).getACall() = call or
4040
call.getFunction().refersTo(enum_convert)
4141
)
4242
)
4343
}
4444

45-
from PythonModuleObject m, StrConst name, string exported_name
46-
where declaredInAll(m.getModule(), name) and
45+
from ModuleValue m, StrConst name, string exported_name
46+
where declaredInAll(m.getScope(), name) and
4747
exported_name = name.strValue() and
4848
not m.hasAttribute(exported_name) and
49-
not (m.getShortName() = "__init__" and exists(m.getPackage().getModule().getSubModule(exported_name))) and
50-
not exists(ImportStarNode imp | imp.getEnclosingModule() = m.getModule() | not imp.getModule().refersTo(_)) and
49+
not (m.getScope().getShortName() = "__init__" and exists(m.getScope().getPackage().getSubModule(exported_name))) and
50+
not exists(ImportStarNode imp | imp.getEnclosingModule() = m.getScope() | imp.getModule().pointsTo().isAbsent()) and
5151
not mutates_globals(m)
5252
select name, "The name '" + exported_name + "' is exported by __all__ but is not defined."

python/ql/src/semmle/python/Module.qll

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ class Module extends Module_, Scope, AstNode {
5252
result = moduleNameFromFile(this.getPath())
5353
}
5454

55+
/** Gets the short name of the module. For example the short name of module x.y.z is 'z' */
56+
string getShortName() {
57+
result = this.getName().suffix(this.getPackage().getName().length()+1)
58+
or
59+
result = this.getName() and not exists(this.getPackage())
60+
}
61+
5562
/** Gets this module */
5663
override Module getEnclosingModule() {
5764
result = this

python/ql/src/semmle/python/objects/ObjectAPI.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ class Value extends TObject {
9999
this.(ObjectInternal).hasAttribute(name)
100100
}
101101

102+
/** Whether this value is absent from the database, but has been inferred to likely exist */
103+
predicate isAbsent() {
104+
this instanceof AbsentModuleObjectInternal
105+
or
106+
this instanceof AbsentModuleAttributeObjectInternal
107+
}
102108
}
103109

104110
/** Class representing modules in the Python program

0 commit comments

Comments
 (0)