mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-07 20:26:54 +00:00
Merge pull request #6 from pre-commit/cannot_parse_ast
Give a better message when ast is not parseable.
This commit is contained in:
commit
ba52312ee8
3 changed files with 17 additions and 3 deletions
|
|
@ -1,9 +1,10 @@
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import ast
|
import ast
|
||||||
import collections
|
import collections
|
||||||
import sys
|
import traceback
|
||||||
|
|
||||||
from pre_commit_hooks.util import entry
|
from pre_commit_hooks.util import entry
|
||||||
|
|
||||||
|
|
@ -35,7 +36,14 @@ class ImportStatementParser(ast.NodeVisitor):
|
||||||
|
|
||||||
|
|
||||||
def check_file_for_debug_statements(filename):
|
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 = ImportStatementParser()
|
||||||
visitor.visit(ast_obj)
|
visitor.visit(ast_obj)
|
||||||
if visitor.debug_import_statements:
|
if visitor.debug_import_statements:
|
||||||
|
|
@ -67,4 +75,4 @@ def debug_statement_hook(argv):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sys.exit(debug_statement_hook())
|
exit(debug_statement_hook())
|
||||||
|
|
|
||||||
1
testing/resources/cannot_parse_ast.notpy
Normal file
1
testing/resources/cannot_parse_ast.notpy
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
if True:
|
||||||
|
|
@ -65,3 +65,8 @@ def test_returns_one_for_failing_file():
|
||||||
def test_returns_zero_for_passing_file():
|
def test_returns_zero_for_passing_file():
|
||||||
ret = debug_statement_hook([__file__])
|
ret = debug_statement_hook([__file__])
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_syntaxerror_file():
|
||||||
|
ret = debug_statement_hook([get_resource_path('cannot_parse_ast.notpy')])
|
||||||
|
assert ret == 1
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue