Skip to content

Commit 31d9774

Browse files
authored
Merge pull request #230 from pre-commit/debug_statements_non_utf8
debug statements hook works for non-utf8 files
2 parents 5c2430e + 2913408 commit 31d9774

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

pre_commit_hooks/debug_statement_hook.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def visit_ImportFrom(self, node):
3535

3636
def check_file_for_debug_statements(filename):
3737
try:
38-
ast_obj = ast.parse(open(filename).read(), filename=filename)
38+
ast_obj = ast.parse(open(filename, 'rb').read(), filename=filename)
3939
except SyntaxError:
4040
print('{} - Could not parse ast'.format(filename))
4141
print()

tests/debug_statement_hook_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import absolute_import
3+
from __future__ import unicode_literals
4+
15
import ast
26

37
import pytest
@@ -77,3 +81,9 @@ def test_returns_zero_for_passing_file():
7781
def test_syntaxerror_file():
7882
ret = debug_statement_hook([get_resource_path('cannot_parse_ast.notpy')])
7983
assert ret == 1
84+
85+
86+
def test_non_utf8_file(tmpdir):
87+
f_py = tmpdir.join('f.py')
88+
f_py.write_binary('# -*- coding: cp1252 -*-\nx = "€"\n'.encode('cp1252'))
89+
assert debug_statement_hook((f_py.strpath,)) == 0

0 commit comments

Comments
 (0)