Skip to content

Commit bcc65db

Browse files
authored
Merge pull request #554 from markshannon/python-named-module-utility
Python: named module utility
2 parents 8d99186 + c1a549d commit bcc65db

File tree

5 files changed

+14
-13
lines changed

5 files changed

+14
-13
lines changed

python/ql/src/Statements/StatementNoEffect.ql

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,7 @@ predicate in_notebook(Expr e) {
8484
}
8585

8686
FunctionObject assertRaises() {
87-
exists(ModuleObject unittest, ClassObject testcase |
88-
unittest.getName() = "unittest" and
89-
testcase = unittest.getAttribute("TestCase") and
90-
result = testcase.lookupAttribute("assertRaises")
91-
)
87+
result = ModuleObject::named("unittest").getAttribute("TestCase").(ClassObject).lookupAttribute("assertRaises")
9288
}
9389

9490
/** Holds if expression `e` is in a `with` block that tests for exceptions being raised. */

python/ql/src/semmle/python/libraries/Zope.qll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ class ZopeInterfaceMethod extends PyFunctionObject {
77

88
/** Holds if this method belongs to a class that sub-classes `zope.interface.Interface` */
99
ZopeInterfaceMethod() {
10-
exists(ModuleObject zope, Object interface, ClassObject owner |
11-
zope.getAttribute("Interface") = interface and
12-
zope.getName() = "zope.interface" and
10+
exists(Object interface, ClassObject owner |
11+
ModuleObject::named("zope.interface").getAttribute("Interface") = interface and
1312
owner.declaredAttribute(_) = this and
1413
owner.getAnImproperSuperType().getABaseType() = interface
1514
)

python/ql/src/semmle/python/regex.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ string mode_from_mode_object(Object obj) {
4040
result = "MULTILINE" or result = "DOTALL" or result = "UNICODE" or
4141
result = "VERBOSE"
4242
) and
43-
exists(ModuleObject re | re.getName() = "re" and re.getAttribute(result) = obj)
43+
ModuleObject::named("re").getAttribute(result) = obj
4444
or
4545
exists(BinaryExpr be, Object sub | obj.getOrigin() = be |
4646
be.getOp() instanceof BitOr and

python/ql/src/semmle/python/security/injection/Xml.qll

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ private class ExpatParser extends TaintKind {
3333
}
3434

3535
private FunctionObject expatCreateParseFunction() {
36-
exists(ModuleObject expat |
37-
expat.getName() = "xml.parsers.expat" and
38-
result = expat.getAttribute("ParserCreate")
39-
)
36+
result = ModuleObject::named("xml.parsers.expat").getAttribute("ParserCreate")
4037
}
4138

4239
private class ExpatCreateParser extends TaintSource {

python/ql/src/semmle/python/types/ModuleObject.qll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,12 @@ class PackageObject extends ModuleObject {
257257

258258
}
259259

260+
/** Utility module for predicates relevant to the `ModuleObject` class. */
261+
module ModuleObject {
262+
263+
/** Gets a `ModuleObject` called `name`, if it exists. */
264+
ModuleObject named(string name) {
265+
result.getName() = name
266+
}
267+
268+
}

0 commit comments

Comments
 (0)