From 38bd34f828ce9685c87f7dbafc59b74ac36d8376 Mon Sep 17 00:00:00 2001 From: Kevin James Date: Tue, 15 Aug 2017 23:03:55 -0700 Subject: [PATCH] handle file reading exceptions --- pre_commit_hooks/debug_statement_hook.py | 6 ++++++ pre_commit_hooks/string_fixer.py | 10 +++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pre_commit_hooks/debug_statement_hook.py b/pre_commit_hooks/debug_statement_hook.py index c5ca387..69a498a 100644 --- a/pre_commit_hooks/debug_statement_hook.py +++ b/pre_commit_hooks/debug_statement_hook.py @@ -42,6 +42,12 @@ def check_file_for_debug_statements(filename): print('\t' + traceback.format_exc().replace('\n', '\n\t')) print() return 1 + except Exception: + print('{} - Caught exception while reading file'.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: diff --git a/pre_commit_hooks/string_fixer.py b/pre_commit_hooks/string_fixer.py index 3523e8a..e5e72b3 100644 --- a/pre_commit_hooks/string_fixer.py +++ b/pre_commit_hooks/string_fixer.py @@ -5,6 +5,7 @@ from __future__ import unicode_literals import argparse import io import tokenize +import traceback double_quote_starts = tuple(s for s in tokenize.single_quoted if '"' in s) @@ -32,7 +33,14 @@ def get_line_offsets_by_line_no(src): def fix_strings(filename): - contents = io.open(filename).read() + try: + contents = io.open(filename).read() + except Exception: + print('{} - Caught exception while reading file'.format(filename)) + print() + print('\t' + traceback.format_exc().replace('\n', '\n\t')) + print() + return 1 line_offsets = get_line_offsets_by_line_no(contents) # Basically a mutable string