Skip to content

Fix --sort-reexports crash with non-seekable streams (e.g. stdin)#2547

Open
Abdu-Ahmed wants to merge 1 commit into
PyCQA:mainfrom
Abdu-Ahmed:fix/sort-reexports-non-seekable-stream-2393
Open

Fix --sort-reexports crash with non-seekable streams (e.g. stdin)#2547
Abdu-Ahmed wants to merge 1 commit into
PyCQA:mainfrom
Abdu-Ahmed:fix/sort-reexports-non-seekable-stream-2393

Conversation

@Abdu-Ahmed
Copy link
Copy Markdown

Fixes #2393

Problem

When running isort --sort-reexports - with input piped from stdin,
isort crashed with io.UnsupportedOperation: underlying stream is not seekable. This happened because core.process() calls
output_stream.seek() as part of reexport sorting, but sys.stdout
is not seekable.

Fix

In api.sort_stream(), before calling core.process(), check whether
the output stream is seekable when sort_reexports is enabled. If not,
use an internal StringIO buffer, the same pattern already used for
non-readable streams in the atomic code path.

Test

Added regression test in test_regressions.py that verifies:

  1. No exception is raised when output stream is non-seekable (sys.stdout)
  2. Sorting still produces correct output when stream is seekable

When --sort-reexports was used with stdin, isort crashed with
io.UnsupportedOperation because core.process() called seek() on
stdout, which is not seekable.

Fix: in api.sort_stream(), if sort_reexports is enabled and the
output stream is not seekable, swap it for an internal StringIO
buffer before passing to core.process().

Fixes PyCQA#2393
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Calling isort with --sort-reexports with input from stdin fails

1 participant