Repository navigation
Read interactive input files as UTF-8 - #433
Open
Mohammed Alkindi (MohammedAlkindi) wants to merge 2 commits into
Open
Mohammed Alkindi (MohammedAlkindi) wants to merge 2 commits into
Mohammed Alkindi (MohammedAlkindi) wants to merge 2 commits into
Conversation
Bare open() follows locale.getpreferredencoding(False), so on a Windows box with a cp1252 code page an input file either raises UnicodeDecodeError or, when its characters happen to map into cp1252, is read as mojibake with no error and passed to the request handler. The TypeScript twin reads the same files with fs.readFileSync().toString(), which is UTF-8 on every platform, so this also makes the two ports agree.
robgruen
approved these changes
Oct 6, 2026
robgruen
left a comment
Contributor
There was a problem hiding this comment.
Maybe add a café regression test to explicitly cover silent corruption.
The existing non-ASCII case raises UnicodeDecodeError under a cp1252 default encoding, so it only pins the loud failure. "café" decodes to "café" instead of raising, which is the case a non-UTF-8 read corrupts without any error.
Author
|
Added in 3e61b99: a new test writes a café line as UTF-8 under a cp1252 default encoding and asserts it reads back unchanged. cp1252 decodes those bytes without raising, so that is the case where the old read corrupted input silently. |
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.
process_requestsopens the input file with noencoding, so it followslocale.getpreferredencoding(False). On a Windows box with a cp1252 code page that fails two ways. Measured on CPython 3.13.13,getpreferredencoding= cp1252:The quiet one is the worse of the two, since that mojibake goes into the model prompt.
The TypeScript twin of this function reads the same files with
fs.readFileSync(inputFileName).toString(), which is UTF-8 on every platform, so this also makes the two ports agree rather than picking an encoding arbitrarily.One behaviour change worth stating: an input file actually encoded as cp1252, which currently reads on Windows, now raises. That converts a silently-wrong path into either correct output or a loud error. All ten tracked
input*.txtfiles are ASCII, so nothing shipped is affected.The test simulates a non-UTF-8 default rather than reading the host locale, so it exercises the same path on Linux CI. It fails on the unmodified source with the
UnicodeDecodeErrorabove.Same file and function as #248.