Repository navigation
feat: open() reads and writes utf-16, utf-32 and utf-8-sig files - #270
Merged
Merged
Conversation
A text file is decoded a piece at a time, and the new codecs need the
stream to carry what a piece ends inside: utf-16's odd byte or the high
half of a surrogate pair, utf-32's part of a unit, utf-8's sequence.
Each codec now holds those bytes for the next read.
The BOM codecs settle what the file is in at its start, as CPython's
file decoders do:
- utf-16 and utf-32 take the byte order their BOM names and drop it,
and refuse a stream with none ("Stream does not start with BOM").
- utf-8-sig drops a BOM when there is one; a file that is only the
start of one reads as nothing.
- A seek back to the first byte looks for the BOM again.
A write puts the BOM out only at the file's first byte, so a file
written in several writes, or appended to, has one; seek(0) and a
write puts it out again, over the old one.
Error positions in a file count from where reading began, which is
where CPython's read() reports them. bytes.decode is unchanged:
ps-decode-from is ps-decode-as with that offset.
D14-file-codecs covers writing, reading and a character split between
two reads, with output recorded from CPython 3.14. Its long files are
data files under tests/modules/data, since a long str built in the
program is costly here on main as well.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
D14 is taken by bisect's spec in #269. 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.
What
open()now takesutf-16,utf-16-le,utf-16-be,utf-32,utf-32-le,utf-32-beandutf-8-sigfor text files. This closes the gap left by #266, whereopen()still refused these codecs. The change is inlib/python/io.x, with small helpers inlib/python/str.x.Reading in pieces. A file is decoded one 8192-byte read at a time, so a read can end in the middle of a character. Each codec now holds back the bytes of that unfinished character until the next read:
The BOM on reading. This matches CPython 3.14's file decoders, which are stricter than
bytes.decode:utf-16andutf-32take the byte order from the BOM and drop it. A stream with no BOM raisesUnicodeDecodeError: 'utf-16' codec can't decode bytes in position 0-1: Stream does not start with BOM, andbytes.decodedoes not.utf-8-sigdrops a BOM if there is one. A file that holds only part of a BOM reads as'', as in CPython.seek(0)looks for the BOM again.The BOM on writing. The BOM is written only at the file's first byte. A file written in several
write()calls, or appended to, gets one BOM.seek(0)followed by a write writes it again, over the old one. A descriptor that can't report its position (a pipe) gets the BOM on its first write.Error positions in a file count from where reading began. That is where CPython's
read()reports them.bytes.decodeis unchanged:ps-decode-fromisps-decode-aswith an offset added.Checking
All runs are on x-lang v0.62.0.
D15-file-codecs has 3 cases with expected output recorded from CPython 3.14, all ASCII. All 3 pass. They cover:
w+withseek(0)then a writestrict,replaceandsurrogatepassnewline='',readlinesandseekThe long files are three small data files in
tests/modules/data/, made by CPython. The spec doesn't build them in the program because building a long str is expensive here: on main,s = 'a' * 8000alone goes past the 6 GB guard, while'a' * 800peaks at 559 MB.Full suite: 1283 tests, 0 failed. D5-functools was stopped once when the host hit critical memory pressure (its own peak was 760 MB). It passed 8/8 when rerun on its own.
Conformance: 644 tests, 12 failed. These are the same 12 as on main.
float-float4reported its totals this time, which is where the one extra test comes from.Not included
'a' * 8000) exists on main too and isn't addressed here.tell()on a text file still counts code points. CPython returns an opaque cookie.🤖 Generated with Claude Code