Skip to content

Commit aeaaab6

Browse files
committed
Python: Modernise Resources/ queries
1 parent 47b932d commit aeaaab6

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

python/ql/src/Resources/FileNotAlwaysClosed.ql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@ import FileOpen
1717

1818
/**
1919
* Whether resource is opened and closed in in a matched pair of methods,
20-
* either __enter__ and __exit__ or __init__ and __del__
20+
* either `__enter__` and `__exit__` or `__init__` and `__del__`
2121
*/
2222
predicate opened_in_enter_closed_in_exit(ControlFlowNode open) {
2323
file_not_closed_at_scope_exit(open) and
24-
exists(FunctionObject entry, FunctionObject exit |
25-
open.getScope() = entry.getFunction() and
26-
exists(ClassObject cls |
24+
exists(FunctionValue entry, FunctionValue exit |
25+
open.getScope() = entry.getScope() and
26+
exists(ClassValue cls |
2727
cls.declaredAttribute("__enter__") = entry and cls.declaredAttribute("__exit__") = exit
2828
or
2929
cls.declaredAttribute("__init__") = entry and cls.declaredAttribute("__del__") = exit
3030
) and
3131
exists(AttrNode attr_open, AttrNode attrclose |
32-
attr_open.getScope() = entry.getFunction() and
33-
attrclose.getScope() = exit.getFunction() and
32+
attr_open.getScope() = entry.getScope() and
33+
attrclose.getScope() = exit.getScope() and
3434
expr_is_open(attr_open.(DefinitionNode).getValue(), open) and
3535
attr_open.getName() = attrclose.getName() and
3636
close_method_call(_, attrclose)

python/ql/src/Resources/FileOpen.qll

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import semmle.python.pointsto.Filters
44

55
/** Holds if `open` is a call that returns a newly opened file */
66
predicate call_to_open(ControlFlowNode open) {
7-
exists(FunctionObject f |
7+
exists(FunctionValue f |
88
function_opens_file(f) and
99
f.getACall() = open
1010
) and
@@ -28,7 +28,7 @@ predicate expr_is_open(ControlFlowNode n, ControlFlowNode open) {
2828

2929
/** Holds if `call` wraps the object referred to by `v` and returns it */
3030
private predicate wraps_file(CallNode call, EssaVariable v) {
31-
exists(ClassObject cls |
31+
exists(ClassValue cls |
3232
call = cls.getACall() and
3333
call.getAnArg() = v.getAUse()
3434
)
@@ -99,7 +99,7 @@ predicate closes_file(EssaNodeRefinement call) {
9999
predicate closes_arg(CallNode call, Variable v) {
100100
call.getAnArg() = v.getAUse() and
101101
(
102-
exists(FunctionObject close | call = close.getACall() and function_closes_file(close))
102+
exists(FunctionValue close | call = close.getACall() and function_closes_file(close))
103103
or
104104
call.getFunction().(NameNode).getId() = "close"
105105
)
@@ -108,15 +108,15 @@ predicate closes_arg(CallNode call, Variable v) {
108108
/** Holds if `call` closes its 'self' argument, which is an open file referred to by `v` */
109109
predicate close_method_call(CallNode call, ControlFlowNode self) {
110110
call.getFunction().(AttrNode).getObject() = self and
111-
exists(FunctionObject close | call = close.getACall() and function_closes_file(close))
111+
exists(FunctionValue close | call = close.getACall() and function_closes_file(close))
112112
or
113113
call.getFunction().(AttrNode).getObject("close") = self
114114
}
115115

116-
predicate function_closes_file(FunctionObject close) {
117-
close.hasLongName("os.close")
116+
predicate function_closes_file(FunctionValue close) {
117+
close = Value::named("os.close")
118118
or
119-
function_should_close_parameter(close.getFunction())
119+
function_should_close_parameter(close.getScope())
120120
}
121121

122122
predicate function_should_close_parameter(Function func) {
@@ -126,15 +126,15 @@ predicate function_should_close_parameter(Function func) {
126126
)
127127
}
128128

129-
predicate function_opens_file(FunctionObject f) {
130-
f = Object::builtin("open")
129+
predicate function_opens_file(FunctionValue f) {
130+
f = Value::named("open")
131131
or
132-
exists(EssaVariable v, Return ret | ret.getScope() = f.getFunction() |
132+
exists(EssaVariable v, Return ret | ret.getScope() = f.getScope() |
133133
ret.getValue().getAFlowNode() = v.getAUse() and
134134
var_is_open(v, _)
135135
)
136136
or
137-
exists(Return ret, FunctionObject callee | ret.getScope() = f.getFunction() |
137+
exists(Return ret, FunctionValue callee | ret.getScope() = f.getScope() |
138138
ret.getValue().getAFlowNode() = callee.getACall() and
139139
function_opens_file(callee)
140140
)

0 commit comments

Comments
 (0)