-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathtest.py
More file actions
435 lines (329 loc) · 6.26 KB
/
test.py
File metadata and controls
435 lines (329 loc) · 6.26 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#Edge guards.
def f():
x = None
if x is None:
x = 7
use(x)
x = unknown() if cond else None
if x is not None:
x = 7
use(x)
x = None
if x is None:
x = None
use(x)
x = None
if not x:
x = 7
use(x)
x = unknown() if cond else None
if x:
x = 7
use(x)
x = None
if not x:
x = None
use(x)
i = 0
if i == 0:
i = 1.0
use(i)
i = 0
if i != 0:
i = 1.0
use(i)
i = 0
if i == 0:
i = 0
use(i)
i = 0
if not i:
i = 1.0
use(i)
i = unknown() if cond else 0
if i:
i = 1.0
use(i)
i = 0
if not i:
i = 0
use(i)
i = 7
if i == 7:
i = 1.0
use(i)
i = 7
if i != 7:
i = 1.0
use(i)
i = 7
if i == 7:
i = 7
use(i)
b = True
if b:
b = 7
use(b)
b = unknown() if cond else True
if not b:
b = 7
use(b)
b = unknown() if cond else False
if b:
b = 7
use(b)
b = False
if not b:
b = 7
use(b)
t = type
if t is object:
t = float
use(t)
t = type
if t is not object:
t = object
use(t)
u = unknown_thing()
if u is None:
u = 7
use(u)
u = unknown_thing()
if u is not None:
u = 7
use(u)
u = unknown_thing()
if u:
u = 7
use(u)
u = unknown_thing()
if not u:
u = 7
use(u)
K = unknown_thing()
u = unknown_thing()
if u is K:
u = 7
use(u)
u = unknown_thing()
if u is not K:
u = 7
use(u)
#String and float consts.
s = "not this"
if s == "not this":
s = 1.0
use(s)
f = 0.7
if f == 0.7:
f = 0
use(f)
#Sentinel guards
SENTINEL = object()
def g(x = SENTINEL):
if x is SENTINEL:
x = 0
use(x)
def h(x = SENTINEL):
if x == SENTINEL:
x = 0
use(x)
def j(x = SENTINEL):
if x is not SENTINEL:
x = 0
use(x)
#ODASA-4056
def format_string(s, formatter='minimal'):
"""Format the given string using the given formatter."""
if not callable(formatter):
formatter = get_formatter_for_name(formatter)
use(formatter)
def guard_callable(s, f=j):
"""Format the given string using the given formatter."""
if callable(f):
f=0
use(f)
class C1(object):pass
class C2(C1):pass
def guard_subclass(s = C2):
if issubclass(s, C1):
s = type
use(s)
def guard_subclass2(s = C2):
if not issubclass(s, C1):
use(s)
s = type
else:
use(s)
use(s)
def instance_guard(s, f=1.0):
if isinstance(s, float):
s = 0
use(s)
#ODASA-4056
def format_string(s, formatter='minimal'):
"""Format the given string using the given formatter."""
if not callable(formatter):
formatter = get_formatter_for_name(formatter)
use(formatter)
func_type = type(j)
def guard_callable(s, f=j):
if isinstance(f, func_type):
f=0
use(f)
#Attribute points-to
class C(object):
def __init__(self):
self._init()
self.x = 1
def _init(self):
self.y = 2
self._init2()
def _init2(self):
self.z = 3
def method(self):
use(self.x)
if isinstance(self.y, int):
use(self.y)
if not isinstance(self.z, int):
use(self.z)
#Guarded None in nested function
def f(x=None):
def inner(arg):
if x:
use(x)
#Guards on whole scope...
class D(object):
def __init__(self, x = None):
if x is None:
x = 1
use(x)
self.x = x
def f(self):
use(self.x)
#Biased splitting & pruning
def f(cond):
if cond:
y = None
x = False
else:
y = something()
x = some_condition()
use(y) # y can be None here
if x:
use(y) # Should not infer that y is None here
#Splittings with boolean expressions:
def split_bool1(x=None,y=None):
if x and y:
raise
if not (x or y):
raise
if x:
use(y)
else:
use(y)
if y:
use(x)
else:
use(x)
def split_bool2(x,y=None,z=7):
if x and not y or x and use(y):
pass
if x and not z or x and use(z):
pass
def split_bool3(a=None, b=None):
if a or b:
if a:
use(b)
else:
use(b) # Should not infer b to be None.
#Guard on instance attribute
class E(object):
def __init__(self):
self.y = None if rand() else 4
x = None if rand() else 3
def a(self):
e = E()
#Do not mutate
if e.x:
use(e.x)
if e.y:
use(e.y)
def b(self):
possibly_mutate(self)
if self.x:
use(self.x)
if self.y:
use(self.y)
def k():
e = E()
possibly_mutate(e)
if e.x:
use(e.x)
if e.y:
use(e.y)
#Global assignment in local scope
g1 = None
def assign_global():
global g1
if not g1:
g1 = 7.0
use(g1) # Cannot be None
#Assignment in local scope, but called from module level
g2 = None
def init():
global g2
if not g2:
g2 = 2.0
init()
use(g2) # Cannot be None
#Global set in init method
g3 = None
class Ugly(object):
def __init__(self):
global g3
if not g3:
g3 = True
def meth(self):
use(g3) # Cannot be None
g4 = None
def get_g4():
if not g4:
set_g4()
use(g4) # Cannot be None
def set_g4():
set_g4_indirect()
def set_g4_indirect():
global g4
g4 = 7
#Assertions
def f(cond):
x = None if cond else thing()
assert x
use(x)
def f(cond, x = 1):
if cond:
x = 1.0
assert isinstance(x, int)
use(x)
#ODASA-5018
def f(x,y=None, z=0):
if (x and y) or (y and not z):
#y cannot be None here.
use(y)
class C(object):
def __init__(self, x=None):
self.x = x
use(self.x)
def meth(self):
if self.x is not None:
use(self.x)
return lambda : use(self.x)
else:
use(self.x)
return lambda : use(self.x)
def test_on_unknown_attr():
e = E()
y = 1
if e.attr:
use(y)