Skip to content

Commit b871342

Browse files
authored
Python: A small further performance improvement
Unrolling the transitive closure had slightly better performance here. Also, we exclude names of builtins, since those will be handled by a separate case of `isDefinedLocally`.
1 parent 8517eff commit b871342

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

python/ql/lib/semmle/python/dataflow/new/internal/ImportStar.qll

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module ImportStar {
2323
cached
2424
private predicate isDefinedLocally(Name n) {
2525
// Defined in an enclosing scope
26-
scope_defines_name(n.getScope().getEnclosingScope*(), n.getId())
26+
enclosing_scope_defines_name(n.getScope(), n.getId())
2727
or
2828
// Defined as a built-in
2929
n.getId() = Builtins::getBuiltinName()
@@ -35,10 +35,13 @@ module ImportStar {
3535
n.getId() in ["__name__", "__package__"]
3636
}
3737

38-
/** Holds if the name `name` is defined in the scope `s` */
3938
pragma[nomagic]
40-
private predicate scope_defines_name(Scope s, string name) {
41-
exists(LocalVariable v | v.getId() = name and v.getScope() = s)
39+
private predicate enclosing_scope_defines_name(Scope s, string name) {
40+
exists(LocalVariable v |
41+
v.getId() = name and v.getScope() = s and not name = Builtins::getBuiltinName()
42+
)
43+
or
44+
enclosing_scope_defines_name(s.getEnclosingScope(), name)
4245
}
4346

4447
/** Holds if a global variable called `name` is assigned a value in the module `m`. */

0 commit comments

Comments
 (0)