mccabe check: gracefully handle compile failures

compiler.parse() will fail with an unhandled AttributeError when called with a
module. Now a message will be printed to stderr and the mccabe check will return
0 rather than crashing.
This commit is contained in:
Chris Adams 2011-09-21 18:30:49 -04:00
parent a470004706
commit b56abdedf3

View file

@ -172,7 +172,12 @@ class PathGraphingAstVisitor(compiler.visitor.ASTVisitor):
def get_code_complexity(code, min=7, filename='stdin'):
complex = []
ast = compiler.parse(code)
try:
ast = compiler.parse(code)
except AttributeError as e:
print >>sys.stderr, "Unable to parse %s: %s" % (filename, e)
return 0
visitor = PathGraphingAstVisitor()
visitor.preorder(ast, visitor)
for graph in visitor.graphs.values():