forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions_test.py
More file actions
200 lines (144 loc) · 3.07 KB
/
functions_test.py
File metadata and controls
200 lines (144 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#Consistent Returns
__all__ = [ '' ]
def ok1(x):
if x:
return None
else:
return
def ok2(x):
if x:
return 4
else:
return "Hi"
def cr1(x):
if x:
return 4
def cr2(x):
if x:
return 4
else:
return
def ok3(x):
try:
return
finally:
do_something()
#Modification of parameter with default
def ok4(x = []):
return len(x)
def mpd(x = []):
x.append("x")
def use_implicit_return_value(arg):
x = do_nothing()
return call_non_callable(arg)
#The return in the lambda is OK as it is auto-generated
y = lambda x : do_nothing()
def do_nothing():
pass
def returns_self(self):
return self
def return_value_ignored():
ok2()
ok4()
sorted([1,2])
d = {}
def use_return_values():
x = ok2()
x = ok2()
x = ok3()
x = ok3()
x = ok4()
x = ok4()
x = y()
x = y()
x = returns_self()
x = returns_self()
x = sorted(x)
x = sorted(x)
x = ok2()
x = ok2()
x = ok3()
x = ok3()
x = ok4()
x = ok4()
x = y()
x = y()
x = returns_self()
x = returns_self()
x = sorted(x)
x = sorted(x)
def ok_to_ignore():
ok1
do_nothing()
returns_self()
ok3()
y()
class DeprecatedSliceMethods(object):
def __getslice__(self, start, stop):
pass
def __setslice__(self, start, stop, value):
pass
def __delslice__(self, start, stop):
pass
@decorator
def nested_call_implicit_return_func_ok(arg):
do_nothing()
#Harmless, so we allow it.
def use_implicit_return_value_ok(arg):
return do_nothing()
def mutli_return(arg):
if arg:
return do_something()
else:
return do_nothing()
#Modification of parameter with default
def augassign(x = []):
x += ["x"]
#ODASA 3658
from sys import exit
#Consistent returns
def ok5():
try:
may_raise()
return 0
except ValueError as e:
print(e)
exit(EXIT_ERROR)
# indirect modification of parameter with default
def aug_assign_argument(x):
x += ['x']
def mutate_argument(x):
x.append('x')
def indirect_modification(y = []):
aug_assign_argument(y)
mutate_argument(y)
def guarded_modification(z=[]):
if z:
z.append(0)
return z
def issue1143(expr, param=[]):
if not param:
return result
for i in param:
param.remove(i) # Mutation here
# Type guarding of modification of parameter with default:
def do_stuff_based_on_type(x):
if isinstance(x, str):
x = x.split()
elif isinstance(x, dict):
x.setdefault('foo', 'bar')
elif isinstance(x, list):
x.append(5)
elif isinstance(x, tuple):
x = x.unknown_method()
def str_default(x="hello world"):
do_stuff_based_on_type(x)
def dict_default(x={'baz':'quux'}):
do_stuff_based_on_type(x)
def list_default(x=[1,2,3,4]):
do_stuff_based_on_type(x)
def tuple_default(x=(1,2)):
do_stuff_based_on_type(x)
# Modification of parameter with default (safe method)
def safe_method(x=[]):
return x.count(42)