Skip to content

Commit

Permalink
fix syntax error reporting from stdin (PyCQA#357)
Browse files Browse the repository at this point in the history
In b73a3d1, there was an assumption that text is None only if there was an
encoding error with the file. However this was the case for all pythons before
3.9 when reading code from stdin.

This takes care to correctly report as much context as possible, so errors
aren't silently dropped with the unhelpful "problem decoding source" message.
  • Loading branch information
stevenkaras committed Jul 13, 2022
1 parent 44ef321 commit 1e41c40
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
9 changes: 1 addition & 8 deletions pyflakes/api.py
Expand Up @@ -55,14 +55,7 @@ def check(codeString, filename, reporter=None):
text = None
offset -= 1

# If there's an encoding problem with the file, the text is None.
if text is None:
# Avoid using msg, since for the only known case, it contains a
# bogus message that claims the encoding the file declared was
# unknown.
reporter.unexpectedError(filename, 'problem decoding source')
else:
reporter.syntaxError(filename, msg, lineno, offset, text)
reporter.syntaxError(filename, msg, lineno, offset, text)
return 1
except Exception:
reporter.unexpectedError(filename, 'problem decoding source')
Expand Down
23 changes: 16 additions & 7 deletions pyflakes/reporter.py
Expand Up @@ -51,19 +51,28 @@ def syntaxError(self, filename, msg, lineno, offset, text):
@param text: The source code containing the syntax error.
@ptype text: C{unicode}
"""
line = text.splitlines()[-1]
if text is None:
line = None
else:
line = text.splitlines()[-1]

if offset is not None:
if sys.version_info < (3, 8):
if sys.version_info < (3, 8) and text is not None:
offset = offset - (len(text) - len(line)) + 1
# some versions of python emit an offset of -1 for certain encoding errors
if offset < 0:
offset = 0
self._stderr.write('%s:%d:%d: %s\n' %
(filename, lineno, offset, msg))
else:
self._stderr.write('%s:%d: %s\n' % (filename, lineno, msg))
self._stderr.write(line)
self._stderr.write('\n')
if offset is not None:
self._stderr.write(re.sub(r'\S', ' ', line[:offset - 1]) +
"^\n")

if line is not None:
self._stderr.write(line)
self._stderr.write('\n')
if offset is not None:
self._stderr.write(re.sub(r'\S', ' ', line[:offset - 1]) +
"^\n")

def flake(self, message):
"""
Expand Down
6 changes: 5 additions & 1 deletion pyflakes/test/test_api.py
Expand Up @@ -606,7 +606,11 @@ def test_misencodedFileUTF8(self):
""" % SNOWMAN).encode('utf-8')
with self.makeTempFile(source) as sourcePath:
self.assertHasErrors(
sourcePath, [f"{sourcePath}: problem decoding source\n"])
sourcePath,
[f"""\
{sourcePath}:0:0: 'ascii' codec can't decode byte 0xe2 in position 21: ordinal not in \
range(128)
"""])

def test_misencodedFileUTF16(self):
"""
Expand Down

0 comments on commit 1e41c40

Please sign in to comment.