Skip to content

Commit 8bad6fa

Browse files
Krock21ericwb
andauthored
Improve performance of linerange (#629)
* Add caching for linerange * fix pep8 * change setattr to . * change list to tuple * added more caching * fix bugs * fix bugs and setattr/getattr * Fix typo in long_set.py and add comment * Update utils.py Co-authored-by: Eric Brown <[email protected]>
1 parent 528c540 commit 8bad6fa

File tree

2 files changed

+7325
-10
lines changed

2 files changed

+7325
-10
lines changed

bandit/core/utils.py

+46-10
Original file line numberDiff line numberDiff line change
@@ -198,28 +198,64 @@ def escaped_bytes_representation(b):
198198
return b.decode("unicode_escape").encode("unicode_escape")
199199

200200

201+
def calc_linerange(node):
202+
"""Calculate linerange for subtree"""
203+
if hasattr(node, "_bandit_linerange"):
204+
return node._bandit_linerange
205+
206+
lines_min = 9999999999
207+
lines_max = -1
208+
if hasattr(node, "lineno"):
209+
lines_min = node.lineno
210+
lines_max = node.lineno
211+
for n in ast.iter_child_nodes(node):
212+
lines_minmax = calc_linerange(n)
213+
lines_min = min(lines_min, lines_minmax[0])
214+
lines_max = max(lines_max, lines_minmax[1])
215+
216+
node._bandit_linerange = (lines_min, lines_max)
217+
218+
return (lines_min, lines_max)
219+
220+
201221
def linerange(node):
202222
"""Get line number range from a node."""
203-
strip = {"body": None, "orelse": None, "handlers": None, "finalbody": None}
223+
if hasattr(node, "_bandit_linerange_stripped"):
224+
lines_minmax = node._bandit_linerange_stripped
225+
return list(range(lines_minmax[0], lines_minmax[1] + 1))
226+
227+
strip = {
228+
"body": None,
229+
"orelse": None,
230+
"handlers": None,
231+
"finalbody": None,
232+
}
204233
for key in strip.keys():
205234
if hasattr(node, key):
206235
strip[key] = getattr(node, key)
207-
node.key = []
236+
setattr(node, key, [])
208237

209238
lines_min = 9999999999
210239
lines_max = -1
211-
for n in ast.walk(node):
212-
if hasattr(n, "lineno"):
213-
lines_min = min(lines_min, n.lineno)
214-
lines_max = max(lines_max, n.lineno)
240+
if hasattr(node, "lineno"):
241+
lines_min = node.lineno
242+
lines_max = node.lineno
243+
for n in ast.iter_child_nodes(node):
244+
lines_minmax = calc_linerange(n)
245+
lines_min = min(lines_min, lines_minmax[0])
246+
lines_max = max(lines_max, lines_minmax[1])
215247

216248
for key in strip.keys():
217249
if strip[key] is not None:
218-
node.key = strip[key]
250+
setattr(node, key, strip[key])
251+
252+
if lines_max == -1:
253+
lines_min = 0
254+
lines_max = 1
255+
256+
node._bandit_linerange_stripped = (lines_min, lines_max)
219257

220-
if lines_max > -1:
221-
return list(range(lines_min, lines_max + 1))
222-
return [0, 1]
258+
return list(range(lines_min, lines_max + 1))
223259

224260

225261
def linerange_fix(node):

0 commit comments

Comments
 (0)