Skip to content

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

Open
Abdu-Ahmed wants to merge 2 commits 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 2 commits 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
@codecov
Copy link
Copy Markdown

codecov Bot commented May 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.16%. Comparing base (7317ad1) to head (aaf392c).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2547   +/-   ##
=======================================
  Coverage   99.15%   99.16%           
=======================================
  Files          41       41           
  Lines        3094     3096    +2     
  Branches      669      670    +1     
=======================================
+ Hits         3068     3070    +2     
  Misses         14       14           
  Partials       12       12           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

The sort_reexports feature requires seeking backwards in the output
stream to rewrite the __all__ section. When used with non-seekable
streams (e.g. stdout pipes), the previous code crashed with an
internal io.UnsupportedOperation error.

Per maintainer feedback, the correct fix is to raise a clear
ValueError explaining the limitation rather than silently buffering
(which would discard output).

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