Skip to content

Commit dce121b

Browse files
authored
Merge pull request github#2916 from BekaValentine/python-objectapi-to-valueapi-callargsandothers
Python: ObjectAPI to ValueAPI: CallArgs and Others
2 parents 326522c + e07a003 commit dce121b

7 files changed

+45
-45
lines changed

python/ql/src/Classes/WrongNameForArgumentInClassInstantiation.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import Expressions.CallArgs
2020

2121
from Call call, ClassObject cls, string name, FunctionObject init
2222
where
23-
illegally_named_parameter(call, cls, name)
24-
and init = get_function_or_initializer(cls)
23+
illegally_named_parameter_objectapi(call, cls, name)
24+
and init = get_function_or_initializer_objectapi(cls)
2525
select
2626
call, "Keyword argument '" + name + "' is not a supported parameter name of $@.", init, init.getQualifiedName()
2727

python/ql/src/Classes/WrongNumberArgumentsInClassInstantiation.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import Expressions.CallArgs
1818
from Call call, ClassObject cls, string too, string should, int limit, FunctionObject init
1919
where
2020
(
21-
too_many_args(call, cls, limit) and too = "too many arguments" and should = "no more than "
21+
too_many_args_objectapi(call, cls, limit) and too = "too many arguments" and should = "no more than "
2222
or
23-
too_few_args(call, cls, limit) and too = "too few arguments" and should = "no fewer than "
24-
) and init = get_function_or_initializer(cls)
23+
too_few_args_objectapi(call, cls, limit) and too = "too few arguments" and should = "no fewer than "
24+
) and init = get_function_or_initializer_objectapi(cls)
2525
select call, "Call to $@ with " + too + "; should be " + should + limit.toString() + ".", init, init.getQualifiedName()

python/ql/src/Expressions/CallArgs.qll

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import python
22

33
import Testing.Mox
44

5-
private int varargs_length(Call call) {
5+
private int varargs_length_objectapi(Call call) {
66
not exists(call.getStarargs()) and result = 0
77
or
88
exists(TupleObject t |
@@ -14,7 +14,7 @@ private int varargs_length(Call call) {
1414
}
1515

1616
/** Gets a keyword argument that is not a keyword-only parameter. */
17-
private Keyword not_keyword_only_arg(Call call, FunctionObject func) {
17+
private Keyword not_keyword_only_arg_objectapi(Call call, FunctionObject func) {
1818
func.getACall().getNode() = call and
1919
result = call.getAKeyword() and
2020
not func.getFunction().getAKeywordOnlyArg().getId() = result.getArg()
@@ -26,54 +26,54 @@ private Keyword not_keyword_only_arg(Call call, FunctionObject func) {
2626
* plus the number of keyword arguments that do not match keyword-only arguments (if the function does not take **kwargs).
2727
*/
2828

29-
private int positional_arg_count_for_call(Call call, Object callable) {
30-
call = get_a_call(callable).getNode() and
29+
private int positional_arg_count_objectapi_for_call_objectapi(Call call, Object callable) {
30+
call = get_a_call_objectapi(callable).getNode() and
3131
exists(int positional_keywords |
32-
exists(FunctionObject func | func = get_function_or_initializer(callable) |
32+
exists(FunctionObject func | func = get_function_or_initializer_objectapi(callable) |
3333
not func.getFunction().hasKwArg() and
34-
positional_keywords = count(not_keyword_only_arg(call, func))
34+
positional_keywords = count(not_keyword_only_arg_objectapi(call, func))
3535
or
3636
func.getFunction().hasKwArg() and positional_keywords = 0
3737
)
3838
|
39-
result = count(call.getAnArg()) + varargs_length(call) + positional_keywords
39+
result = count(call.getAnArg()) + varargs_length_objectapi(call) + positional_keywords
4040
)
4141
}
4242

43-
int arg_count(Call call) {
44-
result = count(call.getAnArg()) + varargs_length(call) + count(call.getAKeyword())
43+
int arg_count_objectapi(Call call) {
44+
result = count(call.getAnArg()) + varargs_length_objectapi(call) + count(call.getAKeyword())
4545
}
4646

4747
/* Gets a call corresponding to the given class or function*/
48-
private ControlFlowNode get_a_call(Object callable) {
48+
private ControlFlowNode get_a_call_objectapi(Object callable) {
4949
result = callable.(ClassObject).getACall()
5050
or
5151
result = callable.(FunctionObject).getACall()
5252
}
5353

5454
/* Gets the function object corresponding to the given class or function*/
55-
FunctionObject get_function_or_initializer(Object func_or_cls) {
55+
FunctionObject get_function_or_initializer_objectapi(Object func_or_cls) {
5656
result = func_or_cls.(FunctionObject)
5757
or
5858
result = func_or_cls.(ClassObject).declaredAttribute("__init__")
5959
}
6060

6161

6262
/**Whether there is an illegally named parameter called `name` in the `call` to `func` */
63-
predicate illegally_named_parameter(Call call, Object func, string name) {
63+
predicate illegally_named_parameter_objectapi(Call call, Object func, string name) {
6464
not func.isC() and
6565
name = call.getANamedArgumentName() and
66-
call.getAFlowNode() = get_a_call(func) and
67-
not get_function_or_initializer(func).isLegalArgumentName(name)
66+
call.getAFlowNode() = get_a_call_objectapi(func) and
67+
not get_function_or_initializer_objectapi(func).isLegalArgumentName(name)
6868
}
6969

7070
/**Whether there are too few arguments in the `call` to `callable` where `limit` is the lowest number of legal arguments */
71-
predicate too_few_args(Call call, Object callable, int limit) {
71+
predicate too_few_args_objectapi(Call call, Object callable, int limit) {
7272
// Exclude cases where an incorrect name is used as that is covered by 'Wrong name for an argument in a call'
73-
not illegally_named_parameter(call, callable, _) and
73+
not illegally_named_parameter_objectapi(call, callable, _) and
7474
not exists(call.getStarargs()) and not exists(call.getKwargs()) and
75-
arg_count(call) < limit and
76-
exists(FunctionObject func | func = get_function_or_initializer(callable) |
75+
arg_count_objectapi(call) < limit and
76+
exists(FunctionObject func | func = get_function_or_initializer_objectapi(callable) |
7777
call = func.getAFunctionCall().getNode() and limit = func.minParameters() and
7878
/* The combination of misuse of `mox.Mox().StubOutWithMock()`
7979
* and a bug in mox's implementation of methods results in having to
@@ -84,47 +84,47 @@ predicate too_few_args(Call call, Object callable, int limit) {
8484
call = func.getAMethodCall().getNode() and limit = func.minParameters() - 1
8585
or
8686
callable instanceof ClassObject and
87-
call.getAFlowNode() = get_a_call(callable) and limit = func.minParameters() - 1
87+
call.getAFlowNode() = get_a_call_objectapi(callable) and limit = func.minParameters() - 1
8888
)
8989
}
9090

9191
/**Whether there are too many arguments in the `call` to `func` where `limit` is the highest number of legal arguments */
92-
predicate too_many_args(Call call, Object callable, int limit) {
92+
predicate too_many_args_objectapi(Call call, Object callable, int limit) {
9393
// Exclude cases where an incorrect name is used as that is covered by 'Wrong name for an argument in a call'
94-
not illegally_named_parameter(call, callable, _) and
94+
not illegally_named_parameter_objectapi(call, callable, _) and
9595
exists(FunctionObject func |
96-
func = get_function_or_initializer(callable) and
96+
func = get_function_or_initializer_objectapi(callable) and
9797
not func.getFunction().hasVarArg() and limit >= 0
9898
|
9999
call = func.getAFunctionCall().getNode() and limit = func.maxParameters()
100100
or
101101
call = func.getAMethodCall().getNode() and limit = func.maxParameters() - 1
102102
or
103103
callable instanceof ClassObject and
104-
call.getAFlowNode() = get_a_call(callable) and limit = func.maxParameters() - 1
104+
call.getAFlowNode() = get_a_call_objectapi(callable) and limit = func.maxParameters() - 1
105105
) and
106-
positional_arg_count_for_call(call, callable) > limit
106+
positional_arg_count_objectapi_for_call_objectapi(call, callable) > limit
107107
}
108108

109109
/** Holds if `call` has too many or too few arguments for `func` */
110-
predicate wrong_args(Call call, FunctionObject func, int limit, string too) {
111-
too_few_args(call, func, limit) and too = "too few"
110+
predicate wrong_args_objectapi(Call call, FunctionObject func, int limit, string too) {
111+
too_few_args_objectapi(call, func, limit) and too = "too few"
112112
or
113-
too_many_args(call, func, limit) and too = "too many"
113+
too_many_args_objectapi(call, func, limit) and too = "too many"
114114
}
115115

116116
/** Holds if `call` has correct number of arguments for `func`.
117117
* Implies nothing about whether `call` could call `func`.
118118
*/
119119
bindingset[call, func]
120-
predicate correct_args_if_called_as_method(Call call, FunctionObject func) {
121-
arg_count(call)+1 >= func.minParameters()
120+
predicate correct_args_if_called_as_method_objectapi(Call call, FunctionObject func) {
121+
arg_count_objectapi(call)+1 >= func.minParameters()
122122
and
123-
arg_count(call) < func.maxParameters()
123+
arg_count_objectapi(call) < func.maxParameters()
124124
}
125125

126126
/** Holds if `call` is a call to `overriding`, which overrides `func`. */
127-
predicate overridden_call(FunctionObject func, FunctionObject overriding, Call call) {
127+
predicate overridden_call_objectapi(FunctionObject func, FunctionObject overriding, Call call) {
128128
overriding.overrides(func) and
129129
overriding.getACall().getNode() = call
130130
}

python/ql/src/Expressions/WrongNameForArgumentInCall.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import Expressions.CallArgs
1919

2020
from Call call, FunctionObject func, string name
2121
where
22-
illegally_named_parameter(call, func, name) and
22+
illegally_named_parameter_objectapi(call, func, name) and
2323
not func.isAbstract() and
2424
not exists(FunctionObject overridden | func.overrides(overridden) and overridden.getFunction().getAnArg().(Name).getId() = name)
2525
select

python/ql/src/Expressions/WrongNumberArgumentsInCall.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ import CallArgs
1717
from Call call, FunctionObject func, string too, string should, int limit
1818
where
1919
(
20-
too_many_args(call, func, limit) and too = "too many arguments" and should = "no more than "
20+
too_many_args_objectapi(call, func, limit) and too = "too many arguments" and should = "no more than "
2121
or
22-
too_few_args(call, func, limit) and too = "too few arguments" and should = "no fewer than "
22+
too_few_args_objectapi(call, func, limit) and too = "too few arguments" and should = "no fewer than "
2323
) and
2424
not func.isAbstract() and
25-
not exists(FunctionObject overridden | func.overrides(overridden) and correct_args_if_called_as_method(call, overridden))
25+
not exists(FunctionObject overridden | func.overrides(overridden) and correct_args_if_called_as_method_objectapi(call, overridden))
2626
/* The semantics of `__new__` can be a bit subtle, so we simply exclude `__new__` methods */
2727
and not func.getName() = "__new__"
2828

python/ql/src/Functions/IncorrectlyOverriddenMethod.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import Expressions.CallArgs
1515
from Call call, FunctionObject func, FunctionObject overridden, string problem
1616
where
1717
func.overrides(overridden) and (
18-
wrong_args(call, func, _, problem) and correct_args_if_called_as_method(call, overridden)
18+
wrong_args_objectapi(call, func, _, problem) and correct_args_if_called_as_method_objectapi(call, overridden)
1919
or
2020
exists(string name |
21-
illegally_named_parameter(call, func, name) and problem = "an argument named '" + name + "'" and
21+
illegally_named_parameter_objectapi(call, func, name) and problem = "an argument named '" + name + "'" and
2222
overridden.getFunction().getAnArg().(Name).getId() = name
2323
)
2424
)

python/ql/src/Functions/IncorrectlySpecifiedOverriddenMethod.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ where
1818
not func.getName() = "__init__" and
1919
overriding.overrides(func) and
2020
call = overriding.getAMethodCall().getNode() and
21-
correct_args_if_called_as_method(call, overriding) and
21+
correct_args_if_called_as_method_objectapi(call, overriding) and
2222
(
23-
arg_count(call)+1 < func.minParameters() and problem = "too few arguments"
23+
arg_count_objectapi(call)+1 < func.minParameters() and problem = "too few arguments"
2424
or
25-
arg_count(call) >= func.maxParameters() and problem = "too many arguments"
25+
arg_count_objectapi(call) >= func.maxParameters() and problem = "too many arguments"
2626
or
2727
exists(string name | call.getAKeyword().getArg() = name and
2828
overriding.getFunction().getAnArg().(Name).getId() = name and

0 commit comments

Comments
 (0)