-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathresources_test.py
More file actions
246 lines (203 loc) · 4.8 KB
/
resources_test.py
File metadata and controls
246 lines (203 loc) · 4.8 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#File not always closed
def not_close1():
f1 = open("filename")
f1.write("Error could occur")
f1.close()
def not_close2():
f2 = open("filename")
def closed3():
f3 = open("filename")
f3.close()
def closed4():
with open("filename") as f4:
f4.write("Error could occur")
def closed5():
f5 = open("filename")
try:
f5.write("Error could occur")
finally:
f5.close()
#Correctly guarded close()
def closed6():
f6 = None
try:
f6 = open("filename")
f6.write("Error could occur")
finally:
if f6:
f6.close()
def closed7():
f7 = None
try:
f7 = open("filename")
f7.write("Error could occur")
finally:
if not f7 is None:
f7.close()
#Incorrectly guarded close()
def not_closed8():
f8 = None
try:
f8 = open("filename")
f8.write("Error could occur")
finally:
if f8 is None:
f8.close()
def not_closed9():
f9 = None
try:
f9 = open("filename")
f9.write("Error could occur")
finally:
if not f9:
f9.close()
def not_closed_but_cant_tell_locally():
return open("filename")
#Closed by handling the correct exception
def closed10():
f10 = open("filename")
try:
f10.write("IOError could occur")
f10.write("IOError could occur")
f10.close()
except IOError:
f10.close()
#Not closed by handling the wrong exception
def not_closed11():
f11 = open("filename")
try:
f11.write("IOError could occur")
f11.write("IOError could occur")
f11.close()
except AttributeError:
f11.close()
def doesnt_raise():
pass
def mostly_closed12():
f12 = open("filename")
try:
f12.write("IOError could occur")
f12.write("IOError could occur")
doesnt_raise("Potential false positive here")
f12.close()
except IOError:
f12.close()
def opener_func1(name):
return open(name)
def opener_func2(name):
t1 = opener_func1(name)
return t1
def not_closed13(name):
f13 = open(name)
f13.write("Hello")
def may_not_be_closed14(name):
f14 = opener_func2(name)
f14.write("Hello")
f14.close()
def closer1(t2):
t2.close()
def closer2(t3):
closer1(t3)
def closed15():
f15 = opener_func2()
closer2(f15)
def may_not_be_closed16(name):
try:
f16 = open(name)
f16.write("Hello")
f16.close()
except IOError:
pass
def may_raise():
if random():
raise ValueError()
#Not handling all exceptions, but we'll tolerate the false negative
def not_closed17():
f17 = open("filename")
try:
f17.write("IOError could occur")
f17.write("IOError could occur")
may_raise("ValueError could occur") # FN here.
f17.close()
except IOError:
f17.close()
#ODASA-3779
#With statement will close the fp
def closed18(path):
try:
f18 = open(path)
except IOError as ex:
print(ex)
raise ex
with f18:
f18.read()
class Closed19(object):
def __enter__(self):
self.fd = open("Filename")
def __exit__(self, *args):
self.fd.close()
class FileWrapper(object):
def __init__(self, fp):
self.fp = fp
def closed20(path):
f20 = open(path)
return FileWrapper(f20)
#ODASA-3105
def run(nodes_module):
use_file = len(sys.argv) > 1
if use_file:
out = open(sys.argv[1], 'w', encoding='utf-8')
else:
out = sys.stdout
try:
out.write("spam")
finally:
if use_file:
out.close()
#ODASA-3515
class GraphVizTrapWriter(object):
def __init__(self, out):
if out is None:
self.out = sys.stdout
else:
self.out = open(out, 'w')
self.pool = GraphVizIdPool(self.out)
def __del__(self):
if self.out != sys.stdout:
self.out.close()
#Returned as part of tuple
def f(name, path):
try:
path = path.attr
file = open(path, 'rb')
except AttributeError:
# ExtensionLoader has not attribute get_filename, instead it has a
# path attribute that we can use to retrieve the module path
try:
path = path.other_attr
file = open(path, 'rb')
except AttributeError:
path = name
file = None
return file, path
#ODASA-5891
def closed21(path):
f21 = open(path, "wb")
try:
f21.write(b"foo")
may_raise()
if foo:
f21.close()
finally:
if not f21.closed:
f21.close()
def not_closed22(path):
f22 = open(path, "wb")
try:
f22.write(b"foo")
may_raise()
if foo:
f22.close()
finally:
if f22.closed: # Wrong sense
f22.close()