Skip to content

Commit

Permalink
Avoid bailing out with uncaught PermissionError
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitriPapadopoulos committed Oct 18, 2022
1 parent 7e37d8b commit 7934fd9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
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

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

0 comments on commit 7934fd9

Please sign in to comment.