-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathtest.py
More file actions
75 lines (63 loc) · 1.87 KB
/
test.py
File metadata and controls
75 lines (63 loc) · 1.87 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
def bad1():
results = []
for x in range(10):
def inner(): # $ capturedVar=x
return x
results.append(inner)
return results
a = [lambda: i for i in range(1, 4)] # $ capturedVar=i
for f in a:
print(f())
#OK:
#Using default argument
def good1():
results = []
for y in range(10):
def inner(y=y):
return y
results.append(inner)
return results
# OK: Using default argument.
def good2():
results = []
for y in range(10):
results.append(lambda y=y: y)
return results
#Factory function
l = []
for r in range(10):
def make_foo(r):
def foo():
return r
return foo
l.append(make_foo(r))
#The inner function does not escape loop.
def ok1():
result = 0
for z in range(10):
def inner():
return z
result += inner()
return result
b = [lambda: i for i in range(1, 4) for j in range(1,5)] # $ capturedVar=i
c = [lambda: j for i in range(1, 4) for j in range(1,5)] # $ capturedVar=j
s = {lambda: i for i in range(1, 4)} # $ capturedVar=i
for f in s:
print(f())
d = {i:lambda: i for i in range(1, 4)} # $ capturedVar=i
for k, f in d.items():
print(k, f())
#Generator expressions are sometimes OK, if they evaluate the iteration
#When the captured variable is used.
#So technically this is a false positive, but it is extremely fragile
#code, so I (Mark) think it is fine to report it as a violation.
g = (lambda: i for i in range(1, 4)) # $ capturedVar=i
for f in g:
print(f())
#But not if evaluated eagerly
l = list(lambda: i for i in range(1, 4)) # $ capturedVar=i
for f in l:
print(f())
# This result is MISSING since the lambda is not detected to escape the loop
def odasa4860(asset_ids):
return dict((asset_id, filter(lambda c : c.asset_id == asset_id, xxx)) for asset_id in asset_ids) # $ MISSING: capturedVar=asset_id