Fix relative imports and error handling

This deals with relative imports in visit_ImportFrom.  Also adds
try/except around ast.parse() call to catch any files with non-valid
source or syntax errors.
This commit is contained in:
Jamie Finnigan 2014-07-17 11:52:33 -07:00
parent 9e99753e43
commit 7c94d3625e
3 changed files with 13 additions and 1 deletions

View File

@ -60,7 +60,11 @@ class BanditManager():
def _execute_ast_visitor(self, fname, fdata, b_ma, b_rs, b_ts):
if fdata != None:
res = b_node_visitor.BanditNodeVisitor(fname, self.logger, b_ma, b_rs, b_ts)
res.visit(ast.parse("".join(fdata.readlines())))
try:
res.visit(ast.parse("".join(fdata.readlines())))
except SyntaxError as e:
self.logger.error("syntax error while parsing AST from file: %s" % fname)
sys.exit(2)
def _init_logger(self, debug=False):
log_level = logging.INFO

View File

@ -82,6 +82,8 @@ class BanditNodeVisitor(ast.NodeVisitor):
def visit_ImportFrom(self, node):
self.context['lineno'] = node.lineno
module = node.module
if module is None:
return self.visit_Import(node)
for nodename in node.names:
if nodename.asname:
self.context['import_aliases'][nodename.asname] = module + "." + nodename.name

View File

@ -1 +1,7 @@
from subprocess import Popen
from ..foo import sys
from . import sys
from .. import sys
from .. import subprocess
from ..subprocess import Popen