Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix uncaught exception on unreadable files #2196

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,14 +647,26 @@ def parse_file(filename, colors, summary, misspellings, exclude_lines,
if not os.path.isfile(filename):
return bad_count

text = is_text_file(filename)
try:
text = is_text_file(filename)
except PermissionError as e:
print("WARNING: %s: %s" % (e.strerror, filename),
file=sys.stderr)
return bad_count
except OSError:
return bad_count

if not text:
if not options.quiet_level & QuietLevels.BINARY_FILE:
print("WARNING: Binary file: %s" % filename, file=sys.stderr)
return bad_count
try:
lines, encoding = file_opener.open(filename)
except Exception:
except PermissionError as e:
print("WARNING: %s: %s" % (e.strerror, filename),
file=sys.stderr)
return bad_count
except OSError:
return bad_count

for i, line in enumerate(lines):
Expand Down
8 changes: 8 additions & 0 deletions codespell_lib/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ def test_basic(tmpdir, capsys):
assert stdout == stderr == ''
assert cs.main(d) == 0

# unreadable file
if sys.platform == 'linux': # cannot create unreadable file on Windows
with open(op.join(d, 'unreadable.txt'), 'w') as f:
f.write('abandonned\n')
os.chmod(f.name, 0o000)
code, _, stderr = cs.main(f.name, std=True)
assert 'WARNING:' in stderr
larsoner marked this conversation as resolved.
Show resolved Hide resolved

# empty directory
os.mkdir(op.join(d, 'test'))
assert cs.main(d) == 0
Expand Down