Skip to content

Commit 0fbfe73

Browse files
author
exarkun
committed
Merge pyflakes-permission-denied-2841
Author: exarkun Reviewer: jonathanj Fixes: #2841 Handle permission errors when trying to read source files.
1 parent 1881aa2 commit 0fbfe73

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

pyflakes/scripts/pyflakes.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@ def checkPath(filename):
4747
4848
@return: the number of warnings printed
4949
"""
50-
if os.path.exists(filename):
50+
try:
5151
return check(file(filename, 'U').read() + '\n', filename)
52-
else:
53-
print >> sys.stderr, '%s: no such file' % (filename,)
52+
except IOError, msg:
53+
print >> sys.stderr, "%s: %s" % (filename, msg.args[1])
5454
return 1
5555

56+
5657
def main():
5758
warnings = 0
5859
args = sys.argv[1:]

pyflakes/test/test_script.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def test_checkPathNonExisting(self):
4444
"""
4545
err = StringIO()
4646
count = withStderrTo(err, lambda: checkPath('extremo'))
47-
self.assertEquals(err.getvalue(), 'extremo: no such file\n')
47+
self.assertEquals(err.getvalue(), 'extremo: No such file or directory\n')
4848
self.assertEquals(count, 1)
4949

5050

@@ -113,7 +113,6 @@ def test_nonDefaultFollowsDefaultSyntaxError(self):
113113
should include the line number of the syntax error. However these
114114
exceptions do not include an offset.
115115
"""
116-
117116
source = """\
118117
def foo(bar=baz, bax):
119118
pass
@@ -137,7 +136,6 @@ def test_nonKeywordAfterKeywordSyntaxError(self):
137136
include the line number of the syntax error. However these exceptions
138137
do not include an offset.
139138
"""
140-
141139
source = """\
142140
foo(bar=baz, bax)
143141
"""
@@ -152,3 +150,18 @@ def test_nonKeywordAfterKeywordSyntaxError(self):
152150
%s:1: non-keyword arg after keyword arg
153151
foo(bar=baz, bax)
154152
""" % (sourcePath.path,))
153+
154+
155+
def test_permissionDenied(self):
156+
"""
157+
If the a source file is not readable, this is reported on standard
158+
error.
159+
"""
160+
sourcePath = FilePath(self.mktemp())
161+
sourcePath.setContent('')
162+
sourcePath.chmod(0)
163+
err = StringIO()
164+
count = withStderrTo(err, lambda: checkPath(sourcePath.path))
165+
self.assertEquals(count, 1)
166+
self.assertEquals(
167+
err.getvalue(), "%s: Permission denied\n" % (sourcePath.path,))

0 commit comments

Comments
 (0)