Skip to content

feat: open() reads and writes utf-16, utf-32 and utf-8-sig files - #270

Merged
jonruttan merged 2 commits into
mainfrom
feat/io-utf16-utf32
Nov 28, 2026
Merged

jonruttan merged 2 commits into
mainfrom
feat/io-utf16-utf32

Conversation

@jonruttan

Copy link
Copy Markdown
Owner

What

open() now takes utf-16, utf-16-le, utf-16-be, utf-32, utf-32-le, utf-32-be and utf-8-sig for text files. This closes the gap left by #266, where open() still refused these codecs. The change is in lib/python/io.x, with small helpers in lib/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:

  • utf-16: an odd byte, or the high half of a surrogate pair
  • utf-32: a partial four-byte unit
  • utf-8 and utf-8-sig: an unfinished multi-byte sequence, as before

The BOM on reading. This matches CPython 3.14's file decoders, which are stricter than bytes.decode:

  • utf-16 and utf-32 take the byte order from the BOM and drop it. A stream with no BOM raises UnicodeDecodeError: 'utf-16' codec can't decode bytes in position 0-1: Stream does not start with BOM, and bytes.decode does not.
  • utf-8-sig drops 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.decode is unchanged: ps-decode-from is ps-decode-as with 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:

    • writing in each codec: one BOM, append, and w+ with seek(0) then a write
    • reading BOM-less, truncated and partial-BOM files under strict, replace and surrogatepass
    • a surrogate pair and a utf-8 sequence split across the 8192-byte read boundary, with universal newlines, newline='', readlines and seek

    The 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' * 8000 alone goes past the 6 GB guard, while 'a' * 800 peaks 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-float4 reported its totals this time, which is where the one extra test comes from.

Not included

  • The cost of building a long str ('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

jonruttan and others added 2 commits November 27, 2026 10:48
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>
@jonruttan
jonruttan merged commit 4321b68 into main Nov 28, 2026
12 checks passed
@jonruttan
jonruttan deleted the feat/io-utf16-utf32 branch November 28, 2026 00:11
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.

1 participant