Skip to content

Commit

Permalink
Merge branch 'crlf_stdin' into 'master'
Browse files Browse the repository at this point in the history
ensure crlf line endings of stdin are handled properly

See merge request pycqa/flake8!461
  • Loading branch information
sigmavirus24 committed Jan 7, 2021
2 parents ff433b2 + 0bf8d2a commit 6de8252
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/flake8/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ def _stdin_get_value_py3(): # type: () -> str
fd = io.BytesIO(stdin_value)
try:
coding, _ = tokenize.detect_encoding(fd.readline)
return stdin_value.decode(coding)
fd.seek(0)
return io.TextIOWrapper(fd, coding).read()
except (LookupError, SyntaxError, UnicodeError):
return stdin_value.decode("utf-8")

Expand Down
10 changes: 10 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Tests for flake8's utils module."""
import io
import logging
import os
import sys

import mock
import pytest
Expand Down Expand Up @@ -304,3 +306,11 @@ def test_matches_filename_for_excluding_dotfiles():
logger = logging.Logger(__name__)
assert not utils.matches_filename('.', ('.*',), '', logger)
assert not utils.matches_filename('..', ('.*',), '', logger)


@pytest.mark.xfail(sys.version_info < (3,), reason='py3+ only behaviour')
def test_stdin_get_value_crlf():
"""Ensure that stdin is normalized from crlf to lf."""
stdin = io.TextIOWrapper(io.BytesIO(b'1\r\n2\r\n'), 'UTF-8')
with mock.patch.object(sys, 'stdin', stdin):
assert utils.stdin_get_value.__wrapped__() == '1\n2\n'

0 comments on commit 6de8252

Please sign in to comment.