From b56abdedf3d174141b635a2e74c7b7c46b05b40d Mon Sep 17 00:00:00 2001 From: Chris Adams Date: Wed, 21 Sep 2011 18:30:49 -0400 Subject: [PATCH] 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. --- flake8/mccabe.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/flake8/mccabe.py b/flake8/mccabe.py index f76d753..9491fdd 100644 --- a/flake8/mccabe.py +++ b/flake8/mccabe.py @@ -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():