jumanjihouse / pre-commit-hooks

git pre-commit hooks that work with http://pre-commit.com/
MIT License
113 stars 52 forks source link

UnicodeDecodeError not handled in 'require-ascii' #56

Closed m-roberts closed 4 years ago

m-roberts commented 4 years ago

I have modified it like this to work for my needs:

"""
require-ascii
"""

# http://python-future.org/compatible_idioms.html
from __future__ import print_function

import sys

status = 0

for filename in sys.argv:
    line_num = 0
    with open(filename, 'r', encoding='UTF-8') as fh:
        while True:
            line_num += 1
            try:
                line = fh.readline()
            except UnicodeDecodeError as e:
                print(f"{filename}: line {line_num} " + str(e))
                status = 1

            if not line:
                break

            col_num = 0
            for char in line:
                col_num += 1
                if ord(char) > 127:
                    print(
                        f"{filename}: line {line_num} column {col_num} " +
                        f"character \"{char}\" (decimal {ord(char)})"
                        )
                    status = 1

sys.exit(status)

This could probably be extended to provide column information too.

jumanjiman commented 4 years ago

Thank you, @m-roberts 🙌

I've updated the code and tagged 2.1.1