-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathtest.py
More file actions
197 lines (138 loc) · 3.06 KB
/
test.py
File metadata and controls
197 lines (138 loc) · 3.06 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
def break_in_finally(seq, x):
for i in seq:
try:
x()
finally:
break
return 0
def return_in_finally(seq, x):
for i in seq:
try:
x()
finally:
return 1
return 0
#Break in loop in finally
#This is OK
def return_in_loop_in_finally(f, seq):
try:
f()
finally:
for i in seq:
break
#But this is not
def return_in_loop_in_finally(f, seq):
try:
f()
finally:
for i in seq:
return
def unnecessary_pass(arg):
print (arg)
pass
#Non-iterator in for loop
class NonIterator(object):
def __init__(self):
pass
for x in NonIterator():
do_something(x)
#None in for loop
def dodgy_iter(x):
if x:
s = None
else:
s = [0,1]
#Error -- Could be None
for i in s:
print(i)
#Ok Test for None
if s:
for i in s:
print(i)
if s is not None:
for i in s:
print(i)
#OK to iterate over:
class GetItem(object):
def __getitem__(self, i):
return i
for y in GetItem():
pass
class D(dict): pass
for z in D():
pass
def modification_of_locals():
x = 0
locals()['x'] = 1
l = locals()
l.update({'x':1, 'y':2})
l.pop('y')
del l['x']
l.clear()
return x
globals()['foo'] = 42 # OK
# in module-level scope `locals() == globals()`
# FP report from https://github.com/github/codeql/issues/6674
locals()['foo'] = 43 # technically OK
#C-style things
if (cond):
pass
while (cond):
pass
assert (test)
def parens(x):
return (x)
#ODASA-2038
class classproperty(object):
def __init__(self, getter):
self.getter = getter
def __get__(self, instance, instance_type):
return self.getter(instance_type)
class WithClassProperty(object):
@classproperty
def x(self):
return [0]
wcp = WithClassProperty()
for i in wcp.x:
assert x == 0
for i in WithClassProperty.x:
assert x == 0
#Should use context mamager
class CM(object):
def __enter__(self):
pass
def __exit__(self, ex, cls, tb):
pass
def write(self, data):
pass
def no_with():
f = CM()
try:
f.write("Hello ")
f.write(" World\n")
finally:
f.close()
#Assert without side-effect
def assert_ok(seq):
assert all(isinstance(element, (str, unicode)) for element in seq)
# False positive. ODASA-8042. Fixed in PR #2401.
class false_positive:
e = (x for x in [])
# isinstance guard should suppress non-iterable warning
def guarded_iteration(x):
ni = NonIterator()
if isinstance(ni, (list, tuple)):
for item in ni:
pass
def guarded_iteration_single(x):
ni = NonIterator()
if isinstance(ni, list):
for item in ni:
pass
# Negated isinstance guard: early return when NOT iterable
def guarded_iteration_negated(x):
ni = NonIterator()
if not isinstance(ni, list):
return
for item in ni: # OK: guarded by negated isinstance + early return
pass