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:
parent
9e99753e43
commit
7c94d3625e
@ -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
|
||||
|
@ -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
|
||||
|
@ -1 +1,7 @@
|
||||
from subprocess import Popen
|
||||
|
||||
from ..foo import sys
|
||||
from . import sys
|
||||
from .. import sys
|
||||
from .. import subprocess
|
||||
from ..subprocess import Popen
|
||||
|
Loading…
x
Reference in New Issue
Block a user