Skip to content

Commit

Permalink
Replace setattr (#493)
Browse files Browse the repository at this point in the history
* add namespaces for parent attributes

* pylint formatting changes

* made bandit_parent a private attr

* updated setattr and multiple isinstance calls
  • Loading branch information
tylerwince authored and ericwb committed Jul 1, 2019
1 parent d25f3fc commit 45494c9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
13 changes: 6 additions & 7 deletions bandit/core/node_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def post_visit(self, node):

# HACK(tkelsey): this is needed to clean up post-recursion stuff that
# gets setup in the visit methods for these node types.
if isinstance(node, ast.FunctionDef) or isinstance(node, ast.ClassDef):
if isinstance(node, (ast.FunctionDef, ast.ClassDef)):
self.namespace = b_utils.namespace_path_split(self.namespace)[0]

def generic_visit(self, node):
Expand All @@ -238,20 +238,19 @@ def generic_visit(self, node):
for idx, item in enumerate(value):
if isinstance(item, ast.AST):
if idx < max_idx:
setattr(item, '_bandit_sibling', value[idx + 1])
item._bandit_sibling = value[idx + 1]
else:
setattr(item, '_bandit_sibling', None)
setattr(item, '_bandit_parent', node)
item._bandit_sibling = None
item._bandit_parent = node

if self.pre_visit(item):
self.visit(item)
self.generic_visit(item)
self.post_visit(item)

elif isinstance(value, ast.AST):
setattr(value, '_bandit_sibling', None)
setattr(value, '_bandit_parent', node)

value._bandit_sibling = None
value._bandit_parent = node
if self.pre_visit(value):
self.visit(value)
self.generic_visit(value)
Expand Down
7 changes: 4 additions & 3 deletions bandit/core/test_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ def __init__(self, name, plugin):
# this dresses up the blacklist to look like a plugin, but
# the '_checks' data comes from the blacklist information.
# the '_config' is the filtered blacklist data set.
setattr(blacklisting.blacklist, "_test_id", 'B001')
setattr(blacklisting.blacklist, "_checks", blacklist.keys())
setattr(blacklisting.blacklist, "_config", blacklist)
blacklisting.blacklist._test_id = "B001"
blacklisting.blacklist._checks = blacklist.keys()
blacklisting.blacklist._config = blacklist

return [Wrapper('blacklist', blacklisting.blacklist)]

def _load_tests(self, config, plugins):
Expand Down
4 changes: 2 additions & 2 deletions bandit/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def linerange(node):
for key in strip.keys():
if hasattr(node, key):
strip[key] = getattr(node, key)
setattr(node, key, [])
node.key = []

lines_min = 9999999999
lines_max = -1
Expand All @@ -222,7 +222,7 @@ def linerange(node):

for key in strip.keys():
if strip[key] is not None:
setattr(node, key, strip[key])
node.key = strip[key]

if lines_max > -1:
return list(range(lines_min, lines_max + 1))
Expand Down

0 comments on commit 45494c9

Please sign in to comment.