Repository navigation
Write redirected output as UTF-8 with single CRLF CSV row endings - #982
Open
Zephyr4772 wants to merge 1 commit into
Open
Zephyr4772 wants to merge 1 commit into
Zephyr4772 wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #981
On Windows, output redirected to a file or pipe was written in the locale encoding (usually cp1252), and
--csv/--tsvrows ended in\r\r\n. As a result,sqlite-utils insertcould not read sqlite-utils' own CSV exports, and non-Latin text crashed--csv,--tableand the default JSON output withUnicodeEncodeError.Changes
cligroup callback calls_use_utf8_for_redirected_stdout(). When stdout is a redirectedTextIOWrapper(not a terminal) with a non-UTF-8 encoding, it is reconfigured to UTF-8, the encodinginsertreads by default. Terminals are left alone, and so is an explicitPYTHONIOENCODING.csv.writercall sites (tablesand the shared query output used byquery,rowsandmemory) get stdout through_csv_stdout(), which setsnewline=""as thecsvdocs require.csv.writeralready writes\r\n, so rows now end in one\r\non every platform.--asciiparagraph indocs/cli.rstno longer suggestsPYTHONUTF8=1as 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:query --csv,--tsv,rows --csv,memory --csv\r\r\n,UnicodeEncodeErroron Devanagari\r\ntables --csv --columns\r\r\n\r\n--nl,--raw-lines,dumpUnicodeEncodeError--tableUnicodeEncodeErrorquery --csv | insert - --csv--encoding=latin-1.csvfile againtests/test_cli_redirected_output.pyswaps stdout for anio.TextIOWrapperwithencoding="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 --csvline endings,--table, re-inserting an exported CSV, and leaving an explicitPYTHONIOENCODINGalone.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/