Skip to content

Write redirected output as UTF-8 with single CRLF CSV row endings - #982

Open
Zephyr4772 wants to merge 1 commit into
simonw:mainfrom
Zephyr4772:fix/windows-redirected-output-utf8
Open

Zephyr4772 wants to merge 1 commit into
simonw:mainfrom
Zephyr4772:fix/windows-redirected-output-utf8

Conversation

@Zephyr4772

@Zephyr4772 Zephyr4772 commented Dec 1, 2026 •

Copy link
Copy Markdown

Fixes #981

On Windows, output redirected to a file or pipe was written in the locale encoding (usually cp1252), and --csv/--tsv rows ended in \r\r\n. As a result, sqlite-utils insert could not read sqlite-utils' own CSV exports, and non-Latin text crashed --csv, --table and the default JSON output with UnicodeEncodeError.

Changes

  • The cli group callback calls _use_utf8_for_redirected_stdout(). When stdout is a redirected TextIOWrapper (not a terminal) with a non-UTF-8 encoding, it is reconfigured to UTF-8, the encoding insert reads by default. Terminals are left alone, and so is an explicit PYTHONIOENCODING.
  • Both csv.writer call sites (tables and the shared query output used by query, rows and memory) get stdout through _csv_stdout(), which sets newline="" as the csv docs require. csv.writer already writes \r\n, so rows now end in one \r\n on every platform.
  • The --ascii paragraph in docs/cli.rst no longer suggests PYTHONUTF8=1 as a workaround. It now says redirected output is UTF-8.

On Linux and macOS, redirected output is already UTF-8 and newlines aren't translated, so neither change alters the output there.

Checks

The real CLI on Windows 11 (Python 3.12, no PYTHONUTF8), output redirected to files:

command before (4.2.2) after
query --csv, --tsv, rows --csv, memory --csv cp1252, \r\r\n, UnicodeEncodeError on Devanagari UTF-8, \r\n
tables --csv --columns \r\r\n \r\n
default JSON, --nl, --raw-lines, dump cp1252 or UnicodeEncodeError UTF-8
--table UnicodeEncodeError UTF-8
query --csv | insert - --csv fails, suggests --encoding=latin-1 all rows round-trip
insert an exported .csv file again fails all rows round-trip

tests/test_cli_redirected_output.py swaps stdout for an io.TextIOWrapper with encoding="cp1252", newline="\r\n", which is how a redirected stdout behaves on Windows. That way the tests exercise the bug on every CI platform: all 8 fail without the fix and pass with it. They cover CSV, TSV and NL query output, rows --csv, tables --csv line endings, --table, re-inserting an exported CSV, and leaving an explicit PYTHONIOENCODING alone.

uv run pytest          1637 passed, 18 skipped
black --check, flake8, mypy sqlite_utils tests, pyright sqlite_utils tests: clean
ty check sqlite_utils: same 28 diagnostics as main
cog --check, codespell: clean

I used Claude Code to help write this change; I reviewed and tested it.


📚 Documentation preview 📚: https://sqlite-utils--982.org.readthedocs.build/en/982/

On Windows, output redirected to a file or pipe used the locale encoding
(usually cp1252), so CSV and JSON exports could not be read back by
sqlite-utils insert and characters outside cp1252 raised UnicodeEncodeError.
CSV and TSV rows also ended in CR CR LF, because csv.writer writes CRLF and
the text stream translated the LF again.

Redirected stdout is now reconfigured to UTF-8 unless PYTHONIOENCODING is
set, and newline translation is turned off before csv.writer is used.
Terminals and other platforms are unaffected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

Redirected output on Windows: CSV rows end in CR CR LF and text is not UTF-8, so exports can't be inserted again

1 participant