Skip to content

Commit 25c06e6

Browse files
committed
Remove encoding dependence
1 parent 3793bc3 commit 25c06e6

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

pre_commit/languages/pygrep.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ def _process_filename_by_line(pattern, filename):
3030
def _process_filename_at_once(pattern, filename):
3131
retv = 0
3232
with open(filename, 'rb') as f:
33-
match = pattern.search(f.read().decode('utf-8').replace('\n', ''))
33+
match = pattern.search(f.read())
3434
if match:
3535
retv = 1
36-
output.write('{}:'.format(filename))
36+
output.write('{}:{}-{}:'.format(filename, match.start(), match.end()))
3737
output.write_line(match.group())
3838
return retv
3939

@@ -59,6 +59,9 @@ def main(argv=None):
5959
args = parser.parse_args(argv)
6060

6161
flags = re.IGNORECASE if args.ignore_case else 0
62+
if args.null_data:
63+
flags = flags | re.MULTILINE | re.DOTALL
64+
6265
pattern = re.compile(args.pattern.encode(), flags)
6366

6467
retv = 0

tests/languages/pygrep_test.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,21 @@ def test_ignore_case(some_files, cap_out):
4141

4242

4343
def test_null_data(some_files, cap_out):
44-
ret = pygrep.main(('--null-data', r'foo.*bar', 'f1', 'f2', 'f3'))
44+
ret = pygrep.main(('--null-data', r'foo\nbar', 'f1', 'f2', 'f3'))
4545
out = cap_out.get()
4646
assert ret == 1
47-
assert out == 'f1:foobar\n'
47+
assert out == 'f1:0-7:foo\nbar\n'
48+
49+
50+
def test_null_data_dotall_flag_is_enabled(some_files, cap_out):
51+
ret = pygrep.main(('--null-data', r'o.*bar', 'f1', 'f2', 'f3'))
52+
out = cap_out.get()
53+
assert ret == 1
54+
assert out == 'f1:1-7:oo\nbar\n'
55+
56+
57+
def test_null_data_multiline_flag_is_enabled(some_files, cap_out):
58+
ret = pygrep.main(('--null-data', r'foo$.*bar', 'f1', 'f2', 'f3'))
59+
out = cap_out.get()
60+
assert ret == 1
61+
assert out == 'f1:0-7:foo\nbar\n'

0 commit comments

Comments
 (0)