Skip to content

Commit f3b62e1

Browse files
authored
Merge pull request github#2840 from BekaValentine/python-objectapi-to-valueapi-useofapply
Python: ObjectAPI to ValueAPI: UseofApply
2 parents 0b74d56 + d19957f commit f3b62e1

File tree

7 files changed

+101
-2
lines changed

7 files changed

+101
-2
lines changed

python/ql/src/Expressions/UseofApply.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
*/
1111

1212
import python
13+
private import semmle.python.types.Builtins
1314

1415
from CallNode call, ControlFlowNode func
15-
where
16-
major_version() = 2 and call.getFunction() = func and func.refersTo(Object::builtin("apply"))
16+
where major_version() = 2 and call.getFunction() = func and func.pointsTo(Value::named("apply"))
1717
select call, "Call to the obsolete builtin function 'apply'."
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
| UseofApply.py:19:3:19:17 | ControlFlowNode for apply() | Call to the obsolete builtin function 'apply'. |
12
| expressions_test.py:3:5:3:21 | ControlFlowNode for apply() | Call to the obsolete builtin function 'apply'. |
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#### UseofApply.ql
2+
3+
# Use of the builtin function `apply` is generally considered bad now that the
4+
# ability to destructure lists of arguments is possible, but we should not flag
5+
# cases where the function is merely named `apply` rather than being the actual
6+
# builtin `apply` function.
7+
8+
def useofapply():
9+
10+
def foo():
11+
pass
12+
13+
14+
15+
# Positive Cases
16+
17+
# This use of `apply` is a reference to the builtin function and so SHOULD be
18+
# caught by the query.
19+
apply(foo, [1])
20+
21+
22+
23+
# Negative Cases
24+
25+
# This use of `apply` is a reference to the locally defined function inside of
26+
# `local`, and so SHOULD NOT be caught by the query.
27+
def local():
28+
def apply(f):
29+
pass
30+
apply(foo)([1])

python/ql/test/3/query-tests/Expressions/UseofApply/UseofApply.expected

Whitespace-only changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#### UseofApply.ql
2+
3+
# Use of the builtin function `apply` is generally considered bad now that the
4+
# ability to destructure lists of arguments is possible, but we should not flag
5+
# cases where the function is merely named `apply` rather than being the actual
6+
# builtin `apply` function.
7+
8+
def useofapply():
9+
10+
def foo():
11+
pass
12+
13+
14+
15+
# Positive Cases
16+
17+
# This use of `apply` is a reference to the builtin function and so SHOULD be
18+
# caught by the query.
19+
apply(foo, [1])
20+
21+
22+
23+
# Negative Cases
24+
25+
# This use of `apply` is a reference to the locally defined function inside of
26+
# `local`, and so SHOULD NOT be caught by the query.
27+
def local():
28+
def apply(f):
29+
pass
30+
apply(foo)([1])
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Expressions/UseofApply.ql

python/ql/test/query-tests/Expressions/general/expressions_test.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,40 @@ def func():
242242

243243
def mpt_arg(d=MappingProxyType({})):
244244
return 1 in d
245+
246+
247+
248+
249+
250+
251+
252+
#### UseofApply.ql
253+
254+
# Use of the builtin function `apply` is generally considered bad now that the
255+
# ability to destructure lists of arguments is possible, but we should not flag
256+
# cases where the function is merely named `apply` rather than being the actual
257+
# builtin `apply` function.
258+
259+
def useofapply():
260+
261+
def foo():
262+
pass
263+
264+
265+
266+
# Positive Cases
267+
268+
# This use of `apply` is a reference to the builtin function and so SHOULD be
269+
# caught by the query.
270+
apply(foo, [1])
271+
272+
273+
274+
# Negative Cases
275+
276+
# This use of `apply` is a reference to the locally defined function inside of
277+
# `local`, and so SHOULD NOT be caught by the query.
278+
def local():
279+
def apply(f):
280+
pass
281+
apply(foo)([1])

0 commit comments

Comments
 (0)