Python: Add utility predicate for finding ModuleObject by name.

This commit is contained in:
Mark Shannon
2018-11-27 17:06:40 +00:00
parent 588defd6b6
commit 435b309852
5 changed files with 14 additions and 13 deletions

View File

@@ -84,11 +84,7 @@ predicate in_notebook(Expr e) {
}
FunctionObject assertRaises() {
exists(ModuleObject unittest, ClassObject testcase |
unittest.getName() = "unittest" and
testcase = unittest.getAttribute("TestCase") and
result = testcase.lookupAttribute("assertRaises")
)
result = ModuleObject::named("unittest").getAttribute("TestCase").(ClassObject).lookupAttribute("assertRaises")
}
/** Holds if expression `e` is in a `with` block that tests for exceptions being raised. */

View File

@@ -7,9 +7,8 @@ class ZopeInterfaceMethod extends PyFunctionObject {
/** Holds if this method belongs to a class that sub-classes `zope.interface.Interface` */
ZopeInterfaceMethod() {
exists(ModuleObject zope, Object interface, ClassObject owner |
zope.getAttribute("Interface") = interface and
zope.getName() = "zope.interface" and
exists(Object interface, ClassObject owner |
ModuleObject::named("zope.interface").getAttribute("Interface") = interface and
owner.declaredAttribute(_) = this and
owner.getAnImproperSuperType().getABaseType() = interface
)

View File

@@ -40,7 +40,7 @@ string mode_from_mode_object(Object obj) {
result = "MULTILINE" or result = "DOTALL" or result = "UNICODE" or
result = "VERBOSE"
) and
exists(ModuleObject re | re.getName() = "re" and re.getAttribute(result) = obj)
ModuleObject::named("re").getAttribute(result) = obj
or
exists(BinaryExpr be, Object sub | obj.getOrigin() = be |
be.getOp() instanceof BitOr and

View File

@@ -33,10 +33,7 @@ private class ExpatParser extends TaintKind {
}
private FunctionObject expatCreateParseFunction() {
exists(ModuleObject expat |
expat.getName() = "xml.parsers.expat" and
result = expat.getAttribute("ParserCreate")
)
result = ModuleObject::named("xml.parsers.expat").getAttribute("ParserCreate")
}
private class ExpatCreateParser extends TaintSource {

View File

@@ -257,3 +257,12 @@ class PackageObject extends ModuleObject {
}
/** Utility module for predicates relevant to the `ModuleObject` class. */
module ModuleObject {
/** Gets a `ModuleObject` who's name is `name` */
ModuleObject named(string name) {
result.getName() = name
}
}