Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion isort/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,18 @@ def main(argv: Sequence[str] | None = None, stdin: TextIOWrapper | None = None)
if show_files:
sys.exit("Error: can't show files for streaming input.")

input_stream = sys.stdin if stdin is None else stdin
if stdin is None:
raw = sys.stdin.buffer.read()
Comment on lines +1063 to +1064
if b"\r\n" in raw:
line_ending = "\r\n"
elif b"\r" in raw:
line_ending = "\r"
else:
line_ending = "\n"
Comment on lines +1065 to +1070
import io
input_stream = io.TextIOWrapper(io.BytesIO(raw), encoding=sys.stdin.encoding or "utf-8")
Comment on lines +1064 to +1072
else:
input_stream = stdin
if check:
incorrectly_sorted = not api.check_stream(
input_stream=input_stream,
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1985,3 +1985,19 @@ def test_comment_on_opening_line_of_aliased_import_does_not_move():
isort.code(short_line, profile="black")
== "from mod import attr as alias # type: ignore[attr-defined] # My comment\n"
)


def test_lf_line_endings_preserved_from_stdin_issue_2453():
"""LF line endings should be preserved when reading from stdin on Windows.
See: https://github.com/PyCQA/isort/issues/2453
"""
import io
lf_content = "import os\nimport re\n"
input_stream = io.TextIOWrapper(
io.BytesIO(lf_content.encode("utf-8")), encoding="utf-8"
)
output_stream = io.StringIO()
from isort import core
core.process(input_stream, output_stream)
Comment on lines +1990 to +2001
result = output_stream.getvalue()
assert "\r\n" not in result, "LF input should not be converted to CRLF"