Give a better message when ast is not parseable.

This commit is contained in:
Anthony Sottile 2014-06-19 17:41:53 -07:00
parent 766631472a
commit 830ea6d8c8
3 changed files with 17 additions and 3 deletions

View File

@ -1,9 +1,10 @@
from __future__ import print_function
from __future__ import unicode_literals
import argparse
import ast
import collections
import sys
import traceback
from pre_commit_hooks.util import entry
@ -35,7 +36,14 @@ class ImportStatementParser(ast.NodeVisitor):
def check_file_for_debug_statements(filename):
ast_obj = ast.parse(open(filename).read())
try:
ast_obj = ast.parse(open(filename).read(), filename=filename)
except SyntaxError:
print('{0} - Could not parse ast'.format(filename))
print()
print('\t' + traceback.format_exc().replace('\n', '\n\t'))
print()
return 1
visitor = ImportStatementParser()
visitor.visit(ast_obj)
if visitor.debug_import_statements:
@ -67,4 +75,4 @@ def debug_statement_hook(argv):
if __name__ == '__main__':
sys.exit(debug_statement_hook())
exit(debug_statement_hook())

View File

@ -0,0 +1 @@
if True:

View File

@ -65,3 +65,8 @@ def test_returns_one_for_failing_file():
def test_returns_zero_for_passing_file():
ret = debug_statement_hook([__file__])
assert ret == 0
def test_syntaxerror_file():
ret = debug_statement_hook([get_resource_path('cannot_parse_ast.notpy')])
assert ret == 1